Java 查找 ImageView 的真实位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22028926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:08:54  来源:igfitidea点击:

Find real position of an ImageView

javaandroidpositionimageview

提问by pepe-pa

I would like to find the real position of an ImageViewdrawable. Currently it returns 0, because the ImageViewis resized by relative layout. But the drawable inside the image view is not fill all in the relative layout. It fills the full width, and it is centered vertically with empty space at the top and bottom of it.

我想找到一个ImageViewdrawable的真实位置。当前它返回 0,因为它ImageView是由相对布局调整大小的。但是image view里面的drawable并没有全部填充到相对布局中。它填满整个宽度,并垂直居中,顶部和底部有空白空间。

    <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>

Please see the attached screenshot. I am searching for x and y of red rectangle and the width, height of it.

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

enter image description here

在此处输入图片说明

采纳答案by Mark Buikema

You have to wait until the Views have been measured. You can use an OnGlobalLayoutListener.

您必须等到视图测量完毕。您可以使用一个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);
    }
});

Because your ImageViewfills the entire screen, it will probably give the device's width as width, and the window height as height. To get the actual width and height of the image inside the ImageView, set the RelativeLayout's height to match_parentand set the ImageView's height to wrap_content, and set the ImageView's layout_gavityto center_vertical.

因为您ImageView填满了整个屏幕,所以它可能会将设备的宽度作为宽度,将窗口高度作为高度。为了得到里面的图像的实际宽度和高度ImageView,设定RelativeLayout的高度match_parent和设置ImageView的高度wrap_content,并设置ImageViewlayout_gavitycenter_vertical

回答by karvoynistas

You have to call the getTranslationX()and getTranslationY()methods to get the real coordinates of your view. and If these methods return you 0 values, attach a click handler in the view that you interested , and inside the onClick call the methods above and also getWidth, and getHeight.

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

回答by longi

The question is: What do you want to achieve? (I mean, why do you need to get the empty space to the front?)

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

Your ImageViewis resized to fill the parent, that's why you get 0from the recommondations. So your ImageViewshould be as big as the picture. Change the height of your Imageviewto wrap_contentand tell your image to resize as big as possible. If I didn't make a mistake, your RelativeLayoutshould be green and you should see your Image in the center (if Image is smaller than parent_width, than you should see the red background to the left and right).

ImageView已调整大小以填充父级,这就是您0从推荐中获得的原因。所以你ImageView应该和图片一样大。改变你的身高Imageviewwrap_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"/>

And now you should get your ImageViewposition as @Neha suggested

现在你应该ImageView按照@Neha 的建议得到你的位置

回答by Shriram

Try to add a button and click the button. Inside onclick() method get the imageView's height and width. Everytime you are getting zero because you haven't done anything with the screen. Atleast you have pass some event and handle event.

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

回答by Zusee Weekin

Try this: Here drawLeftis Xvalue and drawTopis Yvalue.

试试这个:这里drawLeftX值, drawTopY值。

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;

You can get the height = drawBottom - drawTopand width = drawRight - drawLeft.

您可以获得height = drawBottom - drawTopwidth = drawRight - drawLeft

Hope this will help you.

希望这会帮助你。

回答by Sergio Borné

I think i have the solution:

我想我有解决方案:

You have to override the method onWindowFocusChanged of the activity where you are to be sure that the views have been inflated:

您必须覆盖活动的 onWindowFocusChanged 方法,以确保视图已被放大:

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

When this finishes, in array varaible you will have the correct coordinates, I hope.

完成后,我希望在数组变量中您将获得正确的坐标。

PS: image is your ImageView.

PS:图像是你的ImageView。

回答by Vijju

try this :

尝试这个 :

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

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

// this code will return you coordinates of image on screen whenever you want

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