Tic*_*ink 2 android timer long-press
简短版本:我想要一种方法在onTouchEvent上启动基于时间的计数器,并测试在响应之前是否已经过了一定的时间,作为手动LongTouch检测.
说明:我有一个自定义的imageView,可以在两指翻转的屏幕上滑入/滑出屏幕.我想向它添加拖动事件,但这些需要比长按更快.我可以通过使用每个onTouchEvent更新一次的计数器来延迟拖动事件,并且仅触发拖动,例如10次计数,但计数器仅更新触摸事件并且手指必须移动.
如何创建一个基于时间的计数器,一个活动级别字段,每秒递增60次或某些?
我不确定你的问题,但似乎你试图在你的onTouchListener中实现一个catch长按事件,除了你需要在ACTION_UP事件发生之前执行某种逻辑?如果是这样,那就是我遇到的同样问题.我也尝试过使用System.nanoTime()但我找到了一个不那么棘手的方法.你可以使用一个计时器,你只需要在第一个ACTION_DOWN事件上安排它,并在发生任何不利事件时取消它(比如一个ACTION_UP,这意味着它不是一个长按但只是一个点击,或一个有一个位移超过一个的ACTION_MOVE一定的门槛).类似于以下内容:
layout.seyOnTouchListener(new OnTouchListener(){
private Timer longpressTimer; //won't depend on a motion event to fire
private final int longpressTimeDownBegin = 500; //0.5 s
private Point previousPoint;
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:{
longPressTimer = new Timer();
longpressTimer.schedule(new TimerTask(){
//whatever happens on a longpress
}, longpressTimeDownBegin);
return true; //the parent was also handling long clicks
}
case MotionEvent.ACTION_MOVE:{
Point currentPoint = new Point((int)event.getX(), (int)event.getY());
if(previousPoint == null){
previousPoint = currentPoint;
}
int dx = Math.abs(currentPoint.x - previousPoint.x);
int dy = Math.abs(currentPoint.y - previousPoint.y);
int s = (int) Math.sqrt(dx*dx + dy*dy);
boolean isActuallyMoving = s >= minDisToMove; //we're moving
if(isActuallyMoving){ //only restart timer over if we're actually moving (threshold needed because everyone's finger shakes a little)
cancelLongPress();
return false; //didn't trigger long press (will be treated as scroll)
}
else{ //finger shaking a little, so continue to wait for possible long press
return true; //still waiting for potential long press
}
}
default:{
cancelLongPress();
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8264 次 |
| 最近记录: |