双击以在Android中恢复画布

时间:2013-03-08 13:03:47

标签: java android android-canvas gesture

我正在寻找一些代码来实现双击画布恢复的地方。

目前,我有:

    @Override
    public void onTouchEvent(MotionEvent event) {

        float x = event.getX();
        float y = event.getY();
        SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;

        try {
            canvas = holder.lockCanvas();
            if (canvas != null) {
                // Enter it all here
                canvas.restore();


            }
        } finally {
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        }
        super.onTouchEvent(event);
    }

然而,此刻它会读取任何手势并恢复屏幕。我只想让它以双击手势恢复。

我试图让这个工作:Android: How to detect double-tap?但到目前为止我失败了。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

好的,这很笨拙,但请尝试以下方法:

private boolean singleTapped = false;
private long lastTapTime = 0;
private long doubleTapInterval = 1000;

@Override
public void onTouchEvent(MotionEvent event) {
    if (!singleTapped) {
        singleTapped = true;
        lastTapTime = System.currentTimeMillis();
        super.onTouchEvent (event);
        return;
    }

    long tapInterval = System.currentTimeMillis() - lastTapTime;
    if (tapInterval <= doubleTapInterval) {
        float x = event.getX();
        float y = event.getY();
        SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;

        try {
            canvas = holder.lockCanvas();
            if (canvas != null) {
            // Enter it all here
            canvas.restore();


            }
        } finally {
            if (canvas != null)
            holder.unlockCanvasAndPost(canvas);
        }
    }

    super.onTouchEvent(event);
}