Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find real position of an ImageView

I would like to find the real position of an ImageView drawable. Currently it returns 0, because the ImageView is 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.

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

enter image description here

like image 860
pepe-pa Avatar asked Feb 26 '14 00:02

pepe-pa


People also ask

How to get coordinates of ImageView in android?

use ImageView#getImageMatrix() to get the Matrix that is used for image drawing on the ImageView , from the docs: "This (Matrix) is applied to the view's drawable when it is drawn."

What is ImageView code?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics.

Can ImageView be clickable?

You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView .

Can ImageView display video?

ImageView is a control which display image on android and support RGB image, so setting the data of ImageView every period can generate dynamic video.


2 Answers

You have to wait until the Views have been measured. You can use an 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 ImageView fills 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_parent and set the ImageView's height to wrap_content, and set the ImageView's layout_gavity to center_vertical.

like image 181
Mark Buikema Avatar answered Oct 12 '22 23:10

Mark Buikema


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:

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

like image 38
Sergio Borné Avatar answered Oct 13 '22 01:10

Sergio Borné