Android OnTouch 向上动作不会随着扫动而触发
Android OnTouch action up not doesn't fire with sweeping
我在我的 gridview 中的图像视图上设置了一个 OnTouchListener,因此每次单击它时都会触发 ACTION DOWN 并且我会更改其单击版本的图像背景,因此它会模拟单击和 ACTION UP 我 return旧的
当我点击它时一切正常,但是当我稍微滑动它超出范围时,UP 事件不会发生并且图像保持不变。这对我来说似乎是合乎逻辑的,因为我没有释放触摸,而是将它滑出 imageview。我试图限制背景更改仅在单击位于视图边界内时发生,但没有任何效果,这是我的触摸侦听器事件:
imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Log.i("IMAGE", "motion event: " + imageView.toString());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
((ImageView) view).setImageResource(R.drawable.test01);
Log.i("IMAGE", "motion event: " + "Action down Inside");
//imageView.setBackgroundResource(0);
Log.i("IMAGE", "motion event: " + "Action down");
return true;
case MotionEvent.ACTION_UP:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
((ImageView)view).setImageResource(mThumbIds[position]);
Log.i("IMAGE", "motion event: " + "Action up");
return true;
}
return false;
}
});
这是我尝试其他方法的另一个版本:
imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Log.i("IMAGE", "motion event: " + imageView.toString());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("IMAGE", "xC: " + (int)motionEvent.getX() + " yC:" + (int)motionEvent.getX());
if(isViewContains(view, (int)motionEvent.getX(), (int)motionEvent.getY())) {
((ImageView) view).setImageResource(R.drawable.test01);
Log.i("IMAGE", "motion event: " + "Action down Inside");
}
//imageView.setBackgroundResource(0);
Log.i("IMAGE", "motion event: " + "Action down");
return true;
case MotionEvent.ACTION_UP:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
((ImageView)view).setImageResource(mThumbIds[position]);
Log.i("IMAGE", "motion event: " + "Action up");
return true;
}
return true;
}
});
这是我正在使用的功能
private boolean isViewContains(View view, int rx, int ry) {
int[] l = new int[2];
view.getLocationOnScreen(l);
Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
Log.i("IMAGE", "x: " + rx + " y:" + ry);
if(rect.contains(view.getLeft()+ rx, view.getTop() + ry)) {
Log.i("IMAGE", "contains");
}
return rect.contains(view.getLeft()+ rx, view.getTop() + ry);
}
你有什么提示或建议吗?我要的是不管我怎么刷或者点原图到return.
在onTouch(View view, MotionEvent motionEvent)
中尝试MotionEvent.ACTION_CANCEL
我在我的 gridview 中的图像视图上设置了一个 OnTouchListener,因此每次单击它时都会触发 ACTION DOWN 并且我会更改其单击版本的图像背景,因此它会模拟单击和 ACTION UP 我 return旧的
当我点击它时一切正常,但是当我稍微滑动它超出范围时,UP 事件不会发生并且图像保持不变。这对我来说似乎是合乎逻辑的,因为我没有释放触摸,而是将它滑出 imageview。我试图限制背景更改仅在单击位于视图边界内时发生,但没有任何效果,这是我的触摸侦听器事件:
imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Log.i("IMAGE", "motion event: " + imageView.toString());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
((ImageView) view).setImageResource(R.drawable.test01);
Log.i("IMAGE", "motion event: " + "Action down Inside");
//imageView.setBackgroundResource(0);
Log.i("IMAGE", "motion event: " + "Action down");
return true;
case MotionEvent.ACTION_UP:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
((ImageView)view).setImageResource(mThumbIds[position]);
Log.i("IMAGE", "motion event: " + "Action up");
return true;
}
return false;
}
});
这是我尝试其他方法的另一个版本:
imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// Log.i("IMAGE", "motion event: " + imageView.toString());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i("IMAGE", "xC: " + (int)motionEvent.getX() + " yC:" + (int)motionEvent.getX());
if(isViewContains(view, (int)motionEvent.getX(), (int)motionEvent.getY())) {
((ImageView) view).setImageResource(R.drawable.test01);
Log.i("IMAGE", "motion event: " + "Action down Inside");
}
//imageView.setBackgroundResource(0);
Log.i("IMAGE", "motion event: " + "Action down");
return true;
case MotionEvent.ACTION_UP:
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
((ImageView)view).setImageResource(mThumbIds[position]);
Log.i("IMAGE", "motion event: " + "Action up");
return true;
}
return true;
}
});
这是我正在使用的功能
private boolean isViewContains(View view, int rx, int ry) {
int[] l = new int[2];
view.getLocationOnScreen(l);
Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
Log.i("IMAGE", "x: " + rx + " y:" + ry);
if(rect.contains(view.getLeft()+ rx, view.getTop() + ry)) {
Log.i("IMAGE", "contains");
}
return rect.contains(view.getLeft()+ rx, view.getTop() + ry);
}
你有什么提示或建议吗?我要的是不管我怎么刷或者点原图到return.
在onTouch(View view, MotionEvent motionEvent)
中尝试MotionEvent.ACTION_CANCEL