3

enter image description hereenter image description hereI have set an image in imageview. It is showing in center of the screen as I have make its parent layout (Relative layout) to match parent. Imageview height and width is also set to match parent.

I want to get the height and width of Image in imageview.

I have used :

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

but by this we are getting height and width of Imageview .

Here is my xml:

<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/imag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:background="@android:color/black"
        android:src="@drawable/image2" />
</RelativeLayout>

3 Answers3

1
  1. Using ImageView.getDrawable().getInstrinsicWidth() and getIntrinsicHeight() will both return the original dimensions.

  2. Getting the Drawable through ImageView.getDrawable() and casting it to a BitmapDrawable, then using BitmapDrawable.getBitmap().getWidth() and getHeight() also returns the original image and its dimensions.

try below code :

ImageView.getDrawable().getIntrinsicHeight()//original height of underlying image
ImageView.getDrawable().getIntrinsicWidth()//original width of underlying image
imageView.getMeasuredHeight();//height of imageView
imageView.getMeasuredWidth();//width of imageView

reference : Trying to get the display size of an image in an ImageView

Ali
  • 3,346
  • 4
  • 21
  • 56
0

Try this :-

Bitmap b = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int w = b.getWidth(); 
int h = b.getHeight(); // height of your image

P.S :- h IS HEIGHT OF IMAGE ( Original Image Height )

Update

If you want to get the exact height of the image inside the imageView try this :-

int height = imageView.getDrawable().getIntrinsicHeight();
int height_in_dp = Math.round(height / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); 

height_in_dp is the height of the image in dp !!

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0
Bitmap b =  ((BitmapDrawable) imag.getDrawable()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();
Yahya Mukhtar
  • 474
  • 5
  • 13