When trying to get the display size of an image in an ImageView

I’m trying to get the real size of an image displayed in an image view. Actually my image is larger than the screen and the imageview is resizing the image to diplay it. I’m looking for this new size.

I’ve tried to override the onDraw method of the ImageView in a custom view but I’m not getting the correct height and width…

public class LandImageView extends ImageView
    public LandImageView( Context context )
        super( context );

    public LandImageView(Context context, AttributeSet attrs)
        super(context, attrs);

    public LandImageView(Context context, AttributeSet attrs, int defStyle)
        super(context, attrs, defStyle);

    protected void onDraw( Canvas canvas )
        super.onDraw( canvas );

        int test = this.getWidth();
        int test2 = this.getHeight();

    protected void onSizeChanged(int w, int h, int oldw, int oldh)
        super.onSizeChanged(w, h, oldw, oldh);

Do you have any clues ?

None of the answers here actually answer the question:

From a Bitmap
of any size displayed by an ImageView
, find the actual dimensions of the displayed image
as opposed to the dimensions of the supplied Bitmap


  • Using ImageView.getDrawable().getInstrinsicWidth()
    and getIntrinsicHeight()
    will both return the original dimensions.
  • 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.

The only way to get the actual
dimensions of the displayed image is by extracting and using the transformation Matrix
used to display the image as it is shown. This must be done after the measuring stage and the example here shows it called in an Override
of onMeasure()
for a custom ImageView

public class SizeAwareImageView extends ImageView {

    public SizeAwareImageView(Context context) {

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Get image matrix values and place them in an array
        float[] f = new float[9];

        // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
        final float scaleX = f[Matrix.MSCALE_X];
        final float scaleY = f[Matrix.MSCALE_Y];

        // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
        final Drawable d = getDrawable();
        final int origW = d.getIntrinsicWidth();
        final int origH = d.getIntrinsicHeight();

        // Calculate the actual dimensions
        final int actW = Math.round(origW * scaleX);
        final int actH = Math.round(origH * scaleY);

        Log.e("DBG", "["+origW+","+origH+"] -> ["+actW+","+actH+"] & scales: x="+scaleX+" y="+scaleY);


Note:To get the image transformation Matrix
from code in general (like in an Activity
), the function is ImageView.getImageMatrix()
– e.g. myImageView.getImageMatrix()

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » When trying to get the display size of an image in an ImageView

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录