Find real position of an ImageView

13,634

Solution 1

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.

Solution 2

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.

Solution 3

Try this: Here drawLeft is X value and drawTop is Y value.

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 - drawTop and width = drawRight - drawLeft .

Hope this will help you.

Solution 4

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

Share:
13,634
pepe-pa
Author by

pepe-pa

Updated on June 17, 2022

Comments

  • pepe-pa
    pepe-pa almost 2 years

    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

  • pepe-pa
    pepe-pa about 10 years
    Thanks for Your answer I will check it ASAP and reply how it works
  • pepe-pa
    pepe-pa about 10 years
    Thanks for Your answer I will check it ASAP and reply how it works
  • pepe-pa
    pepe-pa about 10 years
    Your answer still returns 0,0
  • pepe-pa
    pepe-pa about 10 years
    Sorry it returns bad values left is properly because it is 0, but top has bad value
  • karvoynistas
    karvoynistas about 10 years
    and what about the getWidth and hetHeight methods? What are they returning if you call them inside the listener?
  • Mark Buikema
    Mark Buikema about 10 years
    What does top return?
  • pepe-pa
    pepe-pa about 10 years
    nearly 200 for 3 images but it should be over 400
  • pepe-pa
    pepe-pa about 10 years
    getWidth and getHeight after @Mark Buikema comment returns properly values, but still I need x and y
  • Tas Morf
    Tas Morf about 10 years
    If you want the top of the drawable inside the ImageView, you could call: imageView.getDrawable().getBounds().top to get relative top, or imageView.getTop() + imageView.getDrawable().getBounds().top for absolute top, after you've waited for the view to be laid out of course.
  • pepe-pa
    pepe-pa about 10 years
    Thank you for your reply, but I need this position to add other elements, not for onClick method