Find Real ImageView Position

I would like to find a real ImageViewdrawable position . It currently returns 0 as the size ImageViewchanges in relative layout. But the image available inside the image does not fill in the relative layout. It fills the entire width and is centered vertically with empty space at the top and bottom.

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

See attached screenshot. I am looking for x and y of a red rectangle and its width, its height.

enter image description here

+4
source share
7 answers

You need to wait until the views are measured. You can use 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);
    }
});

ImageView , , , . ImageView, RelativeLayout match_parent ImageView wrap_content ImageView layout_gavity center_vertical.

+6

, :

onWindowFocus , :

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

, varaible , .

PS: - ImageView.

+3

: drawLeft X, drawTop - Y.

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;

height = drawBottom-drawTop width = drawRight-drawLeft.

, .

+2

getTranslationX() getTranslationY(), . 0 , onClick , getWidth getHeight.

0

: ? ( , ?)

ImageView , 0 . , ImageView , . ImageView wrap_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"/>

ImageView, @Neha

0

Try adding a button and click the button. Inside the onclick () method, get the height and width of the imageView. Every time you get zero, because you did nothing with the screen. At least you go through some kind of event and handle the event.

0
source

try the following:

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

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

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

0
source

Source: https://habr.com/ru/post/1528868/


All Articles