Android MotionEvent ACTION_MOVE的效率.如何提高性能? [英] Android MotionEvent ACTION_MOVE efficiency. How to improve performance?
问题描述
我目前正在开发允许自由绘图的应用程序.
I am currently working on an app that allows for free-drawing.
我当前使用的方法如下:
The current method I am using is as follows:
currentLine是一个列表,其中保留了 ACTION_MOVE
返回的所有点的历史记录.
currentLine is a list that keeps a history of all points that ACTION_MOVE
returns.
public boolean onTouchEvent (MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
Point p = new Point(event.getX(),event.getY());
currentLine.addPoint(p);
invalidate();
break;
}
return true;
}
然后我将这些要点放在我班的 onDraw
方法中.
I then take these points and draw them in the onDraw
method of my class.
@Override
protected void onDraw(Canvas c) {
super.onDraw(c);
//Draw Background Color
c.drawColor(Color.BLUE);
//Setup Paint
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setColor(COLOR.WHITE);
//iterate through points
if(currentLine.size()>0){
for(int x = 0;x<currentLine.size();x++){
c.drawCircle(currentLine.get(x).getX(), currentLine.get(x).getY(), 3, p);
}
}
}
这种方法效果很好,没有滞后或其他任何情况.
And this method works great, with no lag or anything.
除了,它没有足够所需的点数.
Except, it does not get enough of the points that it needs to.
例如,如果我要在整个屏幕上快速拖动手指,则可能只会绘制整个事件的15个点.
For example, if I am to drag my finger quickly across the entire screen, it may only draw 15 points of the whole event.
如何提高MotionEvent的性能/速度?我如何获得更多积分?还是我应该做些其他事情?
How can I improve the performance/speed of the MotionEvent? How can I get more points? Or is there something else I should be doing?
----编辑----
我设法自己解决了.
我没有使用 drawCircle
,而是切换到 drawLine
.
Instead of using drawCircle
, I switched to drawLine
.
示例:
if(points.size()>0){
for(int x = 0;x<points.size()-1;x++){
c.drawLine(points.get(x).getX(), points.get(x).getY(), points.get(x+1).getX(), points.get(x+1).getY(), p);
}
}
这会产生实线,这正是我想要的.
This produces solid lines, which is what I wanted.
但是,出于知识的考虑,我仍然想知道如何加快MotionEvents的速度.
一个详细的答案,将不胜感激
A detailed answer would be appreciated
推荐答案
显然,瓶颈是绘制方法.
The bottleneck is the drawing method, obviously.
如果您使用的是Android 3.0+,请在GPU上绘制所有这些疯狂的东西.添加属性
If you are working on android 3.0+, draw all those crazy things on the GPU. Add the attribute
android:hardwareAccelerated="true"
到清单中的< application>
标记.这将令人难以置信的增加绘图时间.
to the <application>
tag in your manifest. This will unbelievably increase drawing time.
此外,如果只需要更新一点,请尝试不要重绘整个内容.调用invalidate(Rectdirty)而不是invalidate().
Additionally, try to not redraw the whole thing if only a little needs to be updated. Invoke invalidate(Rect dirty) instead of invalidate().
这篇关于Android MotionEvent ACTION_MOVE的效率.如何提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!