【问题标题】:Android Bounding RectanglesAndroid 边界矩形
【发布时间】:2014-07-25 06:05:38
【问题描述】:

我正在尝试围绕 ImageView 创建一个边界矩形以进行碰撞检测。不幸的是,我只能找到精灵的代码而不是 ImageViews。有没有办法为 ImageView 制作一个超出图像的矩形?其中一个 ImageViews 在动画中移动,而另一个则保持静止。

【问题讨论】:

    标签: android imageview bounding


    【解决方案1】:

    将 imageViews 用于移动动画并不是最好的主意。它占用了太多资源。但是,您可以将 imageView 的宽度和高度设置为 WRAP_CONTENT 并使用 ImageView 的大小作为边界矩形

    在您的 xml 布局中,将 ImageView 的 android:layout_height 和 android:layout_width 设置为“wrap_content”。当您需要 ImageView 的边界矩形时:

    ...
    int[] l = new int[2];
    imageView.getLocationOnScreen(l);
    int x = l[0];
    int y = l[1];
    int w = imageView.getWidth();
    int h = imageView.getHeight();
    ...
    

    x, y, w ,h - 屏幕坐标中所需的边界矩形

    【讨论】: