gpt4 book ai didi

java - Android 中双击恢复 Canvas

转载 作者:行者123 更新时间:2023-12-01 04:49:08 25 4
gpt4 key购买 nike

我正在寻找一些代码来工作,双击 Canvas 即可恢复。

目前,我有:

    @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?但到目前为止我还是失败了。

感谢您的帮助:)

最佳答案

好吧,这很笨拙,但请尝试以下操作:

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

关于java - Android 中双击恢复 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15294660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com