how to get a view from an event coordinates in android?

前端 未结 3 1796
鱼传尺愫
鱼传尺愫 2020-12-01 03:36

I want to intercept the touch events on my parent view with onInterceptTouchEvent (MotionEvent ev).

From there I want to know which view was clicked in

3条回答
  •  一向
    一向 (楼主)
    2020-12-01 04:30

    Just to make method of htafoya simpler:

    /**
    * Determines if given points are inside view
    * @param x - x coordinate of point
    * @param y - y coordinate of point
    * @param view - view object to compare
    * @return true if the points are within view bounds, false otherwise
    */
    private boolean isPointInsideView(float x, float y, View view) {
        int location[] = new int[2];
        view.getLocationOnScreen(location);
        int viewX = location[0];
        int viewY = location[1];
    
        // point is inside view bounds
        return ((x > viewX && x < (viewX + view.getWidth())) &&
                (y > viewY && y < (viewY + view.getHeight())));
    }
    

提交回复
热议问题