如何让一个视图从Android的事件坐标? [英] how to get a view from an event coordinates in android?

查看:96
本文介绍了如何让一个视图从Android的事件坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拦截的触摸事件上我与onInterceptTouchEvent(MotionEvent EV)父视图。

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 order to do other things, is there any way to know which view was clicked from that motion event received?

在此先感谢

推荐答案

那么,谁想要知道我做了什么......我不能。我做了一个解决办法,只是知道我的具体看法组件被点击,所以我只能用这个目的:

Well for anyone who wants to know what I did ... i couldn't. I did a workaround to just know if my specific view component was clicked, so I could only end with this:

   if(isPointInsideView(ev.getRawX(), ev.getRawY(), myViewComponent)){
    doSomething()
   }

和方法:

/**
 * 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
 */
public static 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
    if(( x > viewX && x < (viewX + view.getWidth())) &&
            ( y > viewY && y < (viewY + view.getHeight()))){
        return true;
    } else {
        return false;
    }
}

不过这仅适用于布局中已知的意见可以作为参数传递,我还是不能让被点击的观点只是知道坐标。您可以搜索布局的所有意见,但。

However this only works for known views in the layout that you can pass as parameter, I still can't get the clicked view just by knowing the coordinates. You may search for all views in the layout though.

这篇关于如何让一个视图从Android的事件坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆