Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get actual image width/height from ImageView?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:src="@drawable/image2" />

    <View
        android:id="@+id/view_clickable_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#aa000000" />


</RelativeLayout>

I want to make a small part of image in Imageview(myImage) clickable after getting coordinates. Its working fine for me. Now I want to get the functionality of pinch zoom on this image. As I zoom in, clickable area along with the image will also zoom and accordingly.

My problem is that, I want to get height of image in ImageView (excluding black spaces in top and bottom. As ImageView is set to match parent, Image is showing in center of the screen in landscape mode). EveryTime I am getting height of imageview.

My code for getting height of image and imageview:

 int imagWidth = imageView.getWidth();
        int h = imageView.getHeight();


        int w = imageView.getDrawable().getIntrinsicWidth();
        int imagHeight = imageView.getDrawable().getIntrinsicHeight();

        Log.e("height", "Image height" + imagHeight + "\n" + "Imageview height" + h);

So as per calculation: Image height= 882 Imageview height = 672

like image 518
Pragya Mendiratta Avatar asked Nov 29 '25 16:11

Pragya Mendiratta


2 Answers

Work for me

    imgView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Ensure you call it only once :
                imgView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                // Here you can get the size :)
                width = imgView.getWidth();
                height = imgView.getHeight();
            }
        });
like image 68
Tung Tran Avatar answered Dec 02 '25 06:12

Tung Tran


To get the height of an image in the image view you can simply do:

int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();

This will return the 'intrinsic' values of height and width. (these values are the values of the image you are loading into the image view as is without any scaling. you can adjust these values to match any scaling that you may have applied) To adjust values for scaling (given you know its in landscape mode) the following should work :

int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();  
double scale = ((double)imgeView.getWidth())/width
int final_height = Math.toIntExact(Math.round(scale * height));

now final_height is the height of the displayed image.

like image 30
Karan Shishoo Avatar answered Dec 02 '25 07:12

Karan Shishoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!