Java Android中的自定义触摸处理
我需要访问ACTION\u MOVE事件之后发生的ACTION\u UP,SimpleOnGestureListener不直接支持,因此我尝试在活动的onTouchEvent方法中实现自定义触摸处理,以便更熟悉它 该活动是指闹钟的警报屏幕在闹钟响起时出现。 我需要处理5种手势。它们是:Java Android中的自定义触摸处理,java,android,user-interface,touch-event,Java,Android,User Interface,Touch Event,我需要访问ACTION\u MOVE事件之后发生的ACTION\u UP,SimpleOnGestureListener不直接支持,因此我尝试在活动的onTouchEvent方法中实现自定义触摸处理,以便更熟悉它 该活动是指闹钟的警报屏幕在闹钟响起时出现。 我需要处理5种手势。它们是: Single tap - Visual feedback Double tap - Snooze the alarm Swipe along the Y access - visual feedback base
Single tap - Visual feedback
Double tap - Snooze the alarm
Swipe along the Y access - visual feedback based on distance swiped.
ACTION_UP after an ACTION_MOVE with a distance of less than 50% of the screen's Y axis - visual feedback.
ACTION_UP after an ACTION_MOVE with a distance of greater than 50% of the screen's Y axis - dismiss the alarm.
我编写的函数不能正常工作,我很确定这是因为每次调用onTouchEvent时都会设置starteEvent变量,而只应在刷卡或双击的第一次点击时设置
这是我的密码:
private final int DOUBLE_TAP_TIMEOUT = 500; // ms
private boolean isScrolling = false; // flag used to detect the ACTION_UP after a scroll
private boolean possibleDoubleTap = false; // flag used to detect a double tap
private MotionEvent startEvent; // the event that starts the gesture we are trying to detect
private float yPercentScrolled; // the percent the along the (Y axis / 2) that has been scrolled.
@Override
public boolean onTouchEvent(MotionEvent event){
switch (event.getActionMasked()){
case(MotionEvent.ACTION_DOWN):
Log.d("MOTION", "DOWN");
// reset the startEvent given the proper conditions
if(
(!isScrolling && !possibleDoubleTap)
|| (possibleDoubleTap && event.getEventTime() - startEvent.getEventTime() <= DOUBLE_TAP_TIMEOUT)
){
startEvent = event;
}
return true;
case(MotionEvent.ACTION_UP):
Log.d("MOTION", "UP");
if(isScrolling){
isScrolling = false;
if(yPercentScrolled >= 1f){
dismissAlarm();
}
else {
layout.setBackgroundColor(bgColor);
startEvent = null;
possibleDoubleTap = false;
isScrolling = false;
}
}
else if(possibleDoubleTap){
// if we have a double tap
if(event.getEventTime() - startEvent.getEventTime() <= DOUBLE_TAP_TIMEOUT){
snoozeAlarm();
return true;
}
// if we don't have a double tap, do nothing
}
else if(!possibleDoubleTap){
possibleDoubleTap = true;
layout.setBackgroundColor(Color.parseColor("#000000"));
}
return true;
case(MotionEvent.ACTION_MOVE):
Log.d("MOTION", "MOVE");
if(!isScrolling){
isScrolling = true;
}
else {
yPercentScrolled = handleScroll(event, startEvent);
}
return true;
default:
Log.d("MOTION", "other:" + String.valueOf(event.getActionMasked()));
return super.onTouchEvent(event);
}
}
你知道为什么StarteEvent在不应该重置的时候被重置了吗?并没有运动:当它被重置时,在logcat中下降,所以这个问题让我很困惑。这也是startEvent被分配给null以外的其他对象的唯一位置
谢谢。似乎startEvent=event正在将引用分配给对象事件,而不是对象本身。因此,当对象事件的值发生变化时。startEvent的值也会更改
尝试获取并存储案例中所需的值,即X、Y和EventTime,而不是整个事件对象。我实际上刚刚通过调试器,意识到情况就是这样。我使用了代码startEvent=MotionEvent.obtainevent;克隆对象,以便传递MotionEvent,而不是几个变量。我认为我的onTouchEvent现在工作正常,但我还没有测试每个输入组合。谢谢