Pragya Mendiratta
Pragya Mendiratta

Reputation: 707

How to get height of Image in Imageview?

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>

Upvotes: 3

Views: 2113

Answers (3)

Santanu Sur
Santanu Sur

Reputation: 11487

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

Upvotes: 0

MohammadAli
MohammadAli

Reputation: 3466

  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

Upvotes: 1

Yahya Mukhtar
Yahya Mukhtar

Reputation: 474

Bitmap b =  ((BitmapDrawable) imag.getDrawable()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();

Upvotes: 0

Related Questions