Android MotionEvent ACTION_MOVE效率.如何提高性能?

MrZ*_*der 5 performance android touch-event

我目前正在开发一个允许自由绘图的应用程序.

我目前使用的方法如下:

currentLine是一个列表,用于保存ACTION_MOVE返回的所有点的历史记录.

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;

}
Run Code Online (Sandbox Code Playgroud)

然后我把这些观点onDraw用我班上的方法画出来.

@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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这种方法效果很好,没有任何延迟或任何问题.

除此之外,它没有得到它需要的足够的分数.

例如,如果我要在整个屏幕上快速拖动我的手指,它可能只会绘制整个事件的15个点.

如何提高MotionEvent的性能/速度?我怎样才能获得更多积分?或者我还应该做些什么呢?

- - 编辑 - -

我自己设法解决了这个问题.

而不是使用drawCircle,我切换到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);
        }
}
Run Code Online (Sandbox Code Playgroud)

这会产生实线,这就是我想要的.

但是,为了知识,我仍然想知道如何加速MotionEvents.

一个详细的答案将不胜感激

Sam*_*eer 7

您还应该通过使用event.getHistoricalX/Y()函数获得更多积分


poi*_*oae 7

显然,瓶颈是绘图方法.

如果您正在使用android 3.0+,请在GPU上绘制所有这些疯狂的东西.添加属性

android:hardwareAccelerated="true"
Run Code Online (Sandbox Code Playgroud)

<application>清单中的标签.这将令人难以置信地增加绘图时间.

此外,如果只需要更新一点,请尽量不重绘整个事物.调用invalidate(Rect dirty)而不是invalidate().

  • 给出最好的答案. (2认同)