Nul*_*ion 0 android ontouchlistener touch-event
在不停止主 UI 线程的情况下检查屏幕是否被触摸 2 秒或更长时间的最佳且更优化的策略是什么?
我已经检查了一些示例代码,但我不确定哪种是实现它的最佳方法,而且我还需要在不停止主 UI 线程的情况下完成它。
谢谢
OnTouchListener你可以这样实现:
public abstract class TouchTimer implements View.OnTouchListener {
private long touchStart = 0l;
private long touchEnd = 0l;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.touchStart = System.currentTimeMillis();
return true;
case MotionEvent.ACTION_UP:
this.touchEnd = System.currentTimeMillis();
long touchTime = this.touchEnd - this.touchStart;
onTouchEnded(touchTime);
return true;
case MotionEvent.ACTION_MOVE:
return true;
default:
return false;
}
}
protected abstract void onTouchEnded(long touchTimeInMillis);
}
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它:
view.setOnTouchListener(new TouchTimer() {
@Override
protected void onTouchEnded(long touchTimeInMillis) {
// touchTimeInMillis contains the time the touch lasted in milliseconds
}
});
Run Code Online (Sandbox Code Playgroud)
onTouchEnded()一旦触摸结束,就会调用该方法。
| 归档时间: |
|
| 查看次数: |
2056 次 |
| 最近记录: |