Android开边界的视图的矩形矩形、视图、边界、Android

2023-09-05 08:47:33 作者:止于回忆の记忆

我实施拖拽的Andr​​oid应用程序。为了知道是否下降发生在放置目标里面,我需要知道放置目标视图的边框。然后我会看看是否getRawX / Y()在MotionEvent这个矩形内跌倒的时候,我得到了ACTION_UP行动。

i'm implementing a drag and drop for an android application. in order to know if the drop happens inside the drop target, i need to know the bounding rectangle of the drop target view. i would then see if the getRawX/Y() in the MotionEvent fall within this rect when i get the ACTION_UP action.

我知道我可以调用getLeft /右/上/上放置目标视图底(),但这些相对于父的容器。看来我需要知道真实或原始值,这样我就可以比较它们的原始X,Y在MotionEvent。

i realize i can call getLeft/Right/Top/Bottom() on the drop target view, but these are relative to the parent's container. it seems i need to know the "real" or raw values so i can compare them to the raw x, y in the MotionEvent.

推荐答案

回答我的问题......是的, View.getLocationOnScreen()的伎俩。例如,

answering my own question ... yes, View.getLocationOnScreen() did the trick. for example,

private boolean isViewContains(View view, int rx, int ry) {
    int[] l = new int[2];
    view.getLocationOnScreen(l);
    int x = l[0];
    int y = l[1];
    int w = view.getWidth();
    int h = view.getHeight();

    if (rx < x || rx > x + w || ry < y || ry > y + h) {
        return false;
    }
    return true;
}