找到ImageView的真实位置

时间:2014-02-26 00:02:09

标签: java android position imageview

我想找到ImageView drawable的真实位置。目前它返回0,因为ImageView通过相对布局调整大小。但是图像视图中的可绘制内容并未填充相对布局中的所有内容。它填满整个宽度,并且垂直居中,顶部和底部有空白区域。

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout_main"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center"
    >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/drawable"/>

</RelativeLayout>

请参阅随附的屏幕截图。我正在寻找红色矩形的x和y以及它的宽度,高度。

enter image description here

7 个答案:

答案 0 :(得分:10)

您必须等到视图被测量。您可以使用OnGlobalLayoutListener

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    public void onGlobalLayout() {
        int height = imageView.getHeight();
        int width = imageView.getWidth();
        int x = imageView.getLeft();
        int y = imageView.getTop();

        // don't forget to remove the listener to prevent being called again 
        // by future layout events:
        imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

因为ImageView填满整个屏幕,所以它可能会将设备的宽度设为宽度,窗口高度设为高度。要在ImageView内获取图像的实际宽度和高度,请将RelativeLayout的高度设置为match_parent,并将ImageView的高度设置为wrap_content },并将ImageView的{​​{1}}设置为layout_gavity

答案 1 :(得分:3)

我想我有解决方案:

您必须覆盖您所在活动的onWindowFocusChanged方法 确保观点被夸大了:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    int array[] = new int[2];
    image.getLocationOnScreen(array);
}

当这完成时,在阵列变量中你会有正确的坐标,我希望。

PS:图像是你的ImageView。

答案 2 :(得分:2)

试试这个: drawLeft X 值, drawTop Y 值。

ImageView image = (ImageView)findViewById(R.id.imageview);
Rect rt = image.getDrawable().getBounds();

int drawLeft = rt.left;
int drawTop = rt.top;
int drawRight = rt.right;
int drawBottom = rt.bottom;

你可以得到 height = drawBottom - drawTop width = drawRight - drawLeft

希望这会对你有所帮助。

答案 3 :(得分:1)

试试这个:

int[] imageCordinates = new int[2];
imageView.getLocationOnScreen(imageCordinates);

int x = imageCordinates[0];
int y = imageCordinates[1];

//此代码会随时返回您在屏幕上的图像坐标

答案 4 :(得分:0)

您必须调用getTranslationX()getTranslationY()方法才能获得视图的真实坐标。如果这些方法返回0值,请在您感兴趣的视图中附加一个单击处理程序,并在onClick内调用上面的方法以及getWidth和getHeight。

答案 5 :(得分:0)

问题是:你想达到什么目的? (我的意思是,你为什么需要把空的空间放在前面?)

您的ImageView已调整大小以填充父级,这就是您从重新投放中获得0的原因。所以你的ImageView应该和照片一样大。将Imageview的高度更改为wrap_content,并告诉您的图片尽可能调整大小。如果我没有犯错,那么RelativeLayout应该是绿色的,你应该在中心看到你的图像(如果图像小于parent_width,你应该看到左边的红色背景和右)。

 <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/relative_layout_main"
   android:layout_width="wrap_content"
   android:layout_height="fill_parent"
   android:gravity="center"
   android:background="@android:color/holo_green_light" //green background for debugging
 >

<ImageView
    android:background="@android:color/holo_red_light" //red background for debugging
    android:layout_width="match_parent"
    android:layout_height="wrap_content"     //change hieght to content
    android:layout_centerInParent="true"     //center in parent
    android:scaleType="fitCenter"            //scale image to fit center
    android:src="@drawable/drawable"/>

现在你应该得到你的ImageView职位@Neha建议

答案 6 :(得分:0)

尝试添加按钮并单击按钮。在onclick()方法中获取imageView的高度和宽度。每次你因为你没有对屏幕做任何事情而得到零。至少你已经传递了一些事件并处理事件。

相关问题