Android的:如何检测如果使用触摸并拖动了按钮区域?拖动、按钮、区域、Android

由网友(心有猛虎细嗅蔷薇)分享简介:在Android上,我们如何可以检测是否按钮并拖动用户触摸了这个按钮的区域?In Android, how can we detect if user touches on button and drag out of region of this button?推荐答案检查MotionEvent.MOVE_OUT...

在Android上,我们如何可以检测是否按钮并拖动用户触摸了这个按钮的区域?

In Android, how can we detect if user touches on button and drag out of region of this button?

推荐答案

检查MotionEvent.MOVE_OUTSIDE:检查MotionEvent.MOVE:

Check the MotionEvent.MOVE_OUTSIDE: Check the MotionEvent.MOVE:

private Rect rect;    // Variable rect to hold the bounds of the view

public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        // Construct a rect of the view's bounds
        rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());

    }
    if(event.getAction() == MotionEvent.ACTION_MOVE){
        if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
            // User moved outside bounds
        }
    }
    return false;
}

请注意:如果你想针对Android 4.0的,新的可能性的整个世界打开: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER

NOTE: If you want to target Android 4.0, a whole world of new possibilities opens: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER

阅读全文

相关推荐

最新文章