Android中的区域,基本触控

Android中的区域,基本触控,android,ontouchlistener,region,Android,Ontouchlistener,Region,我在一篇文章中发现,将特定的x,y坐标设置为触摸监听器太具体了,而且只有一个位置太小,无法拾取触摸输入 @ligi在我的问题下的评论中提到了地区。我仔细研究了一下,意识到这正是我所需要的,我只是不知道如何实现它 以下是特定x,y坐标的TouchListener代码: public boolean onTouchEvent(MotionEvent ev) { if (!mDragging) { return false; } int x = (int) e

我在一篇文章中发现,将特定的x,y坐标设置为触摸监听器太具体了,而且只有一个位置太小,无法拾取触摸输入

@ligi在我的问题下的评论中提到了
地区
。我仔细研究了一下,意识到这正是我所需要的,我只是不知道如何实现它

以下是特定x,y坐标的TouchListener代码:

public boolean onTouchEvent(MotionEvent ev) {
    if (!mDragging) {
        return false;
    }

    int x = (int) ev.getX();
    int y = (int) ev.getY();

    if (x == 100 && y == 200) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.e(TAG, "ACTION DOWN");
            break;

        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "ACTION MOVE");
            break;

        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ACTION UP");
            break;
        }
    }
有人知道如何建立一个区域吗?比如说,如果用户手指移动到该区域,请执行某些操作。我在其他地方找不到任何基本的实现

如果描述不清楚,这里有一个图像:


我想你会在Android中使用rect。您需要指定矩形,并查看矩形是否包含您的值。在下面的示例中,我列出了一个矩形列表,因此有多个热点。理想情况下,如果只需要一个特定的矩形区域,可以使用一个。.contains内部方法允许您指定x、y坐标可能包含的区域

 List<Rect> retangles;
还是一个人

  Rect rect = new Rect(0, 0, 200, 100);

  if(rect.contains(x,y))
    System.out.println("Touched Rectangle, do what you need to do.");

你可以这样做

public boolean onTouchEvent(MotionEvent ev) {

    int x = (int) ev.getX();
    int y = (int) ev.getY();

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "ACTION DOWN OR MOVE");

            // check if the event is in region 1
            if(x >= "left_x_region1" && x <= "right_x_region1"
                && y >= "top_x_region1" && y <= "bottom_x_region1")
                // do something for region1

            // check if the event is in region 2
            else if(x >= "left_x_region2" && x <= "right_x_region2"
                && y >= "top_x_region2" && y <= "bottom_x_region2")
                // do something for region1

            // continue for other cases
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ACTION UP");
            break;
    }
}
public boolean onTouchEvent(运动事件ev){
int x=(int)ev.getX();
int y=(int)ev.getY();
开关(ev.getAction()){
case MotionEvent.ACTION\u DOWN:
case MotionEvent.ACTION\u移动:
Log.e(标记“动作向下或移动”);
//检查事件是否在区域1中

如果(x>=“左x地区1”和&x=“顶部x地区1”和&y=“左x地区2”和&x=“顶部x地区2”&&y@AmitApollo和zgc7009非常感谢您的回答。我将试用它们,看看它们是如何工作的。稍后我将发布我的进度。再次感谢!@zgc7009请参阅上面的评论您是受欢迎的Gurfufle。您也可以使用Region。Region有一个名为getBounds()的内在函数哪个返回类型是Rect!谢谢@zgc7009。我在我的项目中得到了这个,看起来这正是我需要的!
public boolean onTouchEvent(MotionEvent ev) {

    int x = (int) ev.getX();
    int y = (int) ev.getY();

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            Log.e(TAG, "ACTION DOWN OR MOVE");

            // check if the event is in region 1
            if(x >= "left_x_region1" && x <= "right_x_region1"
                && y >= "top_x_region1" && y <= "bottom_x_region1")
                // do something for region1

            // check if the event is in region 2
            else if(x >= "left_x_region2" && x <= "right_x_region2"
                && y >= "top_x_region2" && y <= "bottom_x_region2")
                // do something for region1

            // continue for other cases
            break;
        case MotionEvent.ACTION_UP:
            Log.e(TAG, "ACTION UP");
            break;
    }
}
List<Rect> regions = new ArrayList<Rect>();
regions.add(new Rect("left_x_region1", "top_y_region1", "right_x_region1", "bottom_y_region1");
regions.add(new Rect("left_x_region2", "top_y_region2", "right_x_region2", "bottom_y_region2");
// continue for each region
for(int i = 0; i < regions.size(); i++){
    if(regions.get(i).contains(x, y)){
       // do stuff for region i
       return;
    }
}