4

用户将触摸图像,重要的是他是否将手指放在该图像中

我尝试编写 a 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;
            }

        });

提前致谢

4

2 回答 2

5

我通过此链接找到了答案,但我将其更改为:

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;
}
于 2013-09-19T20:59:35.413 回答
1

不要忘记 MotionEvent.ACTION_CANCEL

于 2014-04-04T09:13:28.913 回答