[英]Check if MotionEvent.ACTION_UP is out of the imageview
用戶將觸摸圖像,如果他將手指放在該圖像中是非常重要的
我嘗試寫一個onTouchListner()
,之后使用swich case
但我不知道如何繼續
image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
});
提前致謝
我通過此鏈接找到了答案,但我將其更改為:
private Rect rect; // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
// User moved outside bounds
}
break;
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
}
return false;
}
不要忘記MotionEvent.ACTION_CANCEL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.