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.
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
.
Answer:
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.
Answer:
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.
Answer:
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
Answer:
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.
Answer:
The question is: What do you want to achieve? (I mean, why do you need to get the empty space to the front?)
Your ImageView
is resized to fill the parent, that’s why you get 0
from the recommondations. So your ImageView
should be as big as the picture. Change the height of your Imageview
to wrap_content
and tell your image to resize as big as possible. If I didn’t make a mistake, your RelativeLayout
should 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).
<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 ImageView
position as @Neha suggested
Answer:
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.