Get the displayed size of an image inside an ImageView

21,933

Solution 1

the following will work:

ih=imageView.getMeasuredHeight();//height of imageView
iw=imageView.getMeasuredWidth();//width of imageView
iH=imageView.getDrawable().getIntrinsicHeight();//original height of underlying image
iW=imageView.getDrawable().getIntrinsicWidth();//original width of underlying image

if (ih/iH<=iw/iW) iw=iW*ih/iH;//rescaled width of image within ImageView
else ih= iH*iw/iW;//rescaled height of image within ImageView

(iw x ih) now represents the actual rescaled (width x height) for the image within the view (in other words the displayed size of the image)


EDIT: I think a nicer way to write the above answer (and one that works with ints) :

final int actualHeight, actualWidth;
final int imageViewHeight = imageView.getHeight(), imageViewWidth = imageView.getWidth();
final int bitmapHeight = ..., bitmapWidth = ...;
if (imageViewHeight * bitmapWidth <= imageViewWidth * bitmapHeight) {
    actualWidth = bitmapWidth * imageViewHeight / bitmapHeight;
    actualHeight = imageViewHeight;
} else {
    actualHeight = bitmapHeight * imageViewWidth / bitmapWidth;
    actualWidth = imageViewWidth;
}

return new Point(actualWidth,actualHeight);

Solution 2

Here is a helper function to get the bounds of image in an imageView.

/**
 * Helper method to get the bounds of image inside the imageView.
 *
 * @param imageView the imageView.
 * @return bounding rectangle of the image.
 */
public static RectF getImageBounds(ImageView imageView) {
    RectF bounds = new RectF();
    Drawable drawable = imageView.getDrawable();
    if (drawable != null) {
        imageView.getImageMatrix().mapRect(bounds, new RectF(drawable.getBounds()));
    }
    return bounds;
}
Share:
21,933
Freewind
Author by

Freewind

A programmer ([email protected])

Updated on July 09, 2022

Comments

  • Freewind
    Freewind almost 2 years

    The code is simple:

    <ImageView android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:src="@drawable/cat"/>
    

    Notice the ImageView used fill_parent for width and height.

    The image cat is a small image and it will be zoomed in to fit the ImageView, and keep the width/height ratio at the same time.

    My question is how to get the displayed size of the image? I tried:

    imageView.getDrawable().getIntrinsicHeight()
    

    But which it the original height of the image cat.

    I tried:

    imageView.getDrawable().getBounds()
    

    But which returns Rect(0,0,0,0).

  • abbath
    abbath over 10 years
    don't forget about dividing int with int will result int. :)
  • Pravesh
    Pravesh about 8 years
    How to get the bitmapHeight and bitmapWidth. I mean from where we can get the bitmap and how.
  • Pragya Mendiratta
    Pragya Mendiratta about 6 years
    @androiddeveloper: I am not getting correct height of Image in ImageView
  • Pragya Mendiratta
    Pragya Mendiratta about 6 years
    @androiddeveloper How to get bitmapHeight and bitmapWidth as per your edited answer?
  • android developer
    android developer about 6 years
    @PragyaMendiratta It's a part of the input. You have a bitmap you wish to set to the ImageView, so you can get its size, using getWidth and getHeight : developer.android.com/reference/android/graphics/… developer.android.com/reference/android/graphics/…
  • Nirmal Prajapat
    Nirmal Prajapat about 6 years
    hi @androiddeveloper,what if the image size is 200X50 and imageView with 100X100,then what will be the image's dimension by using the method u just suggested, and one more thing the imageView is set to adjustViewBound=true??
  • android developer
    android developer about 6 years
    @NirmalPrajapat I wrote it a long time ago. I don't remember. Try it.
  • Pragya Mendiratta
    Pragya Mendiratta about 6 years
    getWidth and getHeight will give Imageview height and width ?@androiddeveloper
  • Abandoned Cart
    Abandoned Cart about 4 years
    Actually, I am coming from an OCR library that returns the placements of words relative to the original image and I have to position them on a scaled ImageView. Your origin story was nice, though.
  • newbie
    newbie over 2 years
    Oh thanks, God. You doing great.