Android:无法识别OnTouch,MotionEvent.ACTION_MOVE? [英] Android: OnTouch, MotionEvent.ACTION_MOVE is not recognized?
问题描述
这是我的代码,我想检测手指何时落在屏幕上,因此当我触摸屏幕时会检测到ACTION_DOWN
,但是当我用手指滑下屏幕时,无法识别ACTION_MOVE
,也没有ACTION_UP
你知道为什么吗?
Here is my code, I want to detect when my finger goes down the screen so when I touch the screen I detect the ACTION_DOWN
but when I go down the screen with my finger, ACTION_MOVE
is not recognized, neither ACTION_UP
Do you know why?
float x=0;
protected void onCreate(Bundle savedInstanceState) {
do things
ImageView image2 = (ImageView) findViewById(R.id.imageView3);
image2.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN) {
x=arg1.getX();
}
else {
if (arg1.getAction()==MotionEvent.ACTION_MOVE){
if (arg1.getX()>x) {
do things
}
}
else {
if (arg1.getAction()==MotionEvent.ACTION_UP){
do things
}
}
}
}
推荐答案
如果您的onTouch()
方法返回false
作为对初始ACTION_DOWN
MotionEvent
的响应,它将不会收到任何属于该事件的后续事件这个特定的手势.而是将那些触摸事件呈现给层次结构中的父级.
If your onTouch()
method returns false
in response to the initial ACTION_DOWN
MotionEvent
, it will not receive any of the subsequent events that belong to this particular gesture. Instead those touch events will be presented to the parent in the hierarchy.
换句话说,如果在手势开始时(ACTION_DOWN
)从onTouch()
返回false
,则表示该方法不再希望看到任何手势,并且手势的事件应转到父View
.
To phrase that another way, if you return false
from onTouch()
during the start of a gesture (the ACTION_DOWN
), it signals that the method no longer wants to see any more of the gesture, and that the gesture's events should go to the parent View
.
markproxy在下面的注释中指出,如果MotionEvent
不是ACTION_DOWN
,例如ACTION_MOVE
,则返回false
不会阻止 当前手势中的MotionEvent
呈现给View
.
As markproxy points out in the comments below, returning false
when the MotionEvent
is anything other than an ACTION_DOWN
, such as an ACTION_MOVE
for example, will not prevent subsequent MotionEvent
s in the current gesture being presented to the View
.
这篇关于Android:无法识别OnTouch,MotionEvent.ACTION_MOVE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!