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.
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."
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.
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 .
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With