47

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);
    }

    @Override
    protected void onDraw( Canvas canvas )
    {
        super.onDraw( canvas );

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

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}

Do you have any clues ?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Guillaume Ménant
  • 481
  • 1
  • 5
  • 3
  • is it a Bitmap? Bitmap does have getWidth()and getHeight() functions. – bhups Oct 04 '10 at 13:14
  • Yes, it's a bitmap but I don't want the real size of the bitmap but the display size which is different because the image is larger than the screen. Thanks for your help ! – Guillaume Ménant Oct 04 '10 at 14:41
  • 1
    Can't you simply use View.getWidth() and View.getHeight() ? – Kevin Gaudin Oct 17 '10 at 23:53
  • 3
    Despite all your solutions, I've not found what I'm looking for right now... With all these methods, I'm getting the REAL size of my original bitmap (858*552) but not the size of this bitmap on the screen :-/ The screen is 480*320 so I have to get bounds smaller than these values but it's not working yet. Perhaps I'm missing something ! – Guillaume Ménant Oct 28 '10 at 08:23

15 Answers15

106

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.

Namely:

  • 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) {
        super(context);
    }

    @Override
    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];
        getImageMatrix().getValues(f);

        // 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()

btalb
  • 6,937
  • 11
  • 36
  • 44
  • Hi, I tried this solution, but it gave me the original size of the image instead of the scaled size on the device. I also tried to add the scaleType="matrix", but it did not help. Is it a problem, that I use the ImageView within a Fragment which is weighted with another Fragment? Hope you can help me. – Michael Nov 23 '13 at 10:34
  • 1
    Nevermind, I fixed to use your integration with the right constructor: `public SizeAwareImageView(Context context, AttributeSet attrs){ super(context, attrs); }` [This post](http://stackoverflow.com/a/18624930/1694500) helped me to fix this! In the layout xml I used the following Code: `` – Michael Nov 23 '13 at 11:07
  • Yeah, this implementation was merely a bare bones proof of concept showing the absolute minimum needed to get it to do what the question asked. – btalb Nov 23 '13 at 13:21
  • Apparently this solution does not work on Android 2.1 (or small screen) – EdgarT Mar 19 '14 at 19:28
  • According to the docs this solution should work. The only reason it wouldn't is if the return of `getImageMatrix()` is incorrect (which I doubt seeing as it is since API 1). The solution you link is a work around at best (this one uses the matrix used to get the displayed image vs the size of the `ImageView`). The latter makes all sorts of potentially incorrect assumptions about how your `ImageView` is displaying the underlying image – btalb Mar 20 '14 at 01:57
  • This is so strange as for a 1920x1080 image I got original size of 3840x2160 (double it)....?! – Alex Apr 22 '16 at 08:20
  • I think this only works is the image resource is set in XML layout code, otherwise getDrawable returns null – r3flss ExlUtr Dec 08 '16 at 14:06
  • I confirm this works as expected. Even after setting the image programmatically (you just have to wait for the measurement to complete and the image to be loaded). I've used the tree observer for the view. – Ionut Negru Jun 06 '17 at 09:52
  • @BT I am getting problem in calculating height of Image in Imageview. How can we get that – Pragya Mendiratta Mar 13 '18 at 06:24
  • getInstrinsicWidth will affect by ImageView ScaleType, it can't represent the real dimension of the image. – Loyea Jul 16 '22 at 06:18
28

I extended B T's answer to produce a static method from it, and to include image left and top positions into the ImageView :

/**
 * Returns the bitmap position inside an imageView.
 * @param imageView source ImageView
 * @return 0: left, 1: top, 2: width, 3: height
 */
public static int[] getBitmapPositionInsideImageView(ImageView imageView) {
    int[] ret = new int[4];

    if (imageView == null || imageView.getDrawable() == null)
        return ret;

    // Get image dimensions
    // Get image matrix values and place them in an array
    float[] f = new float[9];
    imageView.getImageMatrix().getValues(f);

    // 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 = imageView.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);

    ret[2] = actW;
    ret[3] = actH;

    // Get image position
    // We assume that the image is centered into ImageView
    int imgViewW = imageView.getWidth();
    int imgViewH = imageView.getHeight();

    int top = (int) (imgViewH - actH)/2;
    int left = (int) (imgViewW - actW)/2;

    ret[0] = left;
    ret[1] = top;

    return ret;
}
Quentin S.
  • 1,462
  • 20
  • 18
  • Wouldn't returning of `Rect` instead of `int[]` be more convenient and more readable? – fdermishin Nov 15 '17 at 08:11
  • @fdermishin : the `React` class contains `bottom, left, right, top` attributes while we're returning `width, height, left, top` - it would be misleading (source: https://developer.android.com/reference/android/graphics/Rect.html) – Quentin S. Nov 15 '17 at 16:48
  • You should call this method inside `onDrawListener { }` of target `ImageView` to get the correct values. – Kshitij Patil Nov 15 '21 at 20:27
  • Its returning position of bitmap before it is being set into imageview, not after it has been set. – Divya Gupta Jan 06 '23 at 08:40
11

I found that WarrenFaith's suggestion of using setAdjustViewBounds worked, but I had to change the ImageView's layout_width/layout_height to 'wrap_content' (with 'match_parent', setAdjustViewBounds did nothing). To get the height/width/gravity behaviour I wanted, I had to wrap the ImageView in a FrameLayout:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        />
</FrameLayout>

After doing this, the ImageView's dimensions (as returned by getWidth() and getHeight()) matched the display size of the image.

Squatting Bear
  • 1,644
  • 14
  • 12
6

Try

ImageView.getDrawable().getIntrinsicHeight()
ImageView.getDrawable().getIntrinsicWidth()
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • 3
    It is worth noting that this answer will return the dimensions of the bitmap being drawn not the actual displayed dimensions or 'display size'. – btalb Apr 04 '13 at 01:22
1

I'm just passing by but hope it still helps I'm going under the assumption your talking about bitmaps in your imageView

what you want to understand is the difference between

bmpBack.getWidth() -> this gives you the size of your bitmap and bmpBack.getScaledWidth(canvas); -> this will give you the size of the bitmap as displayed on the screen.

I never used ImageView because the relative display was driving me mad so in the end I just override the onDraw and did my canvas very similarly to opengl.

I think this is your problem

cheers

Jason

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
1

You can use imageview's viewtreeobserver and addonDrawListener.

ViewTreeObserver vto = imageView.getViewTreeObserver();
    vto.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
        @Override
        public void onDraw() {

            float[] f = new float[9];
            imageView.getImageMatrix().getValues(f);

            // 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 = imageView. 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);

        }
    });
Satpal Yadav
  • 81
  • 1
  • 12
0

try overriding onMeasure(int widthMeasureSpec, int heightMeasureSpec) instead of onSizeChanged.

bhups
  • 14,345
  • 8
  • 49
  • 57
0

If I get you correctly you need your ImageView dimension, in order to scale your image accordingly. I did this with a custom class, where I override the onMeasure() call to get width and height.

class LandImageView extends ImageView{ 
public LandImageView (Context context, AttributeSet attrs) {
  super (context, attrs);
 } 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
 {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  final int width = MeasureSpec.getSize(widthMeasureSpec);
  final int height = MeasureSpec.getSize(heightMeasureSpec);

  Log.v("", String.format("w %d h %d", width, height));
                // width and height are the dimensions for your image
                // you should remember the values to scale your image later on

  this.setMeasuredDimension(width, height );
 }}

In onMeasure you get the width and height for your image so that it fits your view.

You can use the LandImageView in your Layout like this:

<my.package.LandImageView ... >
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
0

Try loading your resource using BitmapFactory.decodeResource (Resources res, int id, BitmapFactory.Options opts) with the BitmapFactory.Options() class. BitmapFactory.Options() has a flag which called "inJustDecodeBounds" and gives only the resource dimensions.

Hope that helped.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Uri May
  • 365
  • 2
  • 7
0
public class images extends Activity {
ImageView i1;
LinearLayout l1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     i1=(ImageView)findViewById(R.id.iv);
     l1 = new LinearLayout(this);
    ImageView i = new ImageView(this);
    ImageView i1 = new ImageView(this);
    i.setImageResource(R.drawable.  imagename.........);

    l1.addView(i);
    l1.addView(i1);
    setContentView(l1);
    }
}

first add the images in ur resource folder....... and in xml file create a image view....

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
user493244
  • 909
  • 7
  • 19
0

I thinks you need the size of the image visible int the screen, for that you simply need to do this:

@Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        initialWidth = this.getMeasuredWidth();
        initialHeight = this.getMeasuredHeight();
    }

And do take a look at the documentation of the methods like: getWidht(), getHeight(), getMeasuredWidth(), getMeasuredHeight(), etc. You will get to know what it does to give you that size.

EDIT: If you want actual width and height of the image being loaded into the imageView. You may want to change the method calls from getMeasuredWidth() to getIntrinsicWidth() and getMeasuredHeight() to getIntrinsicHeight() like this:

  Drawable drawable = getDrawable();
  actualWidth = drawable.getIntrinsicWidth();
  actualHeight = drawable.getIntrinsicHeight();
Ashutosh Tiwari
  • 424
  • 3
  • 13
0

I was looking for a solution to set the dimension of the image view to the scaled image to prevent empty space on top/bottom or left/right (cause the dimension of the view doesn't changed to fit the scaled image).

What I found to do the trick was using the method mImageView.setAdjustViewBounds(true); which results in the correct layout dimension. I don't have the scaled image dimension but I got the result I was looking for... just if someone needs it...

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
0

Simply can be used this code (in activity):

@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
    super.onWindowFocusChanged(hasWindowFocus);
    final ImageView imageView = findViewById(R.id.imageView);
    int width = imageView.getWidth(), height = imageView.getHeight();
}

In case the picture reaches the ends of the imageView.

IQ.feature
  • 600
  • 6
  • 16
0

this is my way to get the actual coordinates of the image displayed in the image view. Create a class with this code of mine

class GetIntrinsicImageview(
private val context: Context) {
data class Coordinator(
    val top: Float,
    val left: Float,
    val right: Float,
    val bottom: Float
)

fun getCoordinator(iv: ImageView, bitmap: Bitmap): Coordinator {
    val drawable = iv.drawable
    val imageIntrinsicWidth = drawable.intrinsicWidth
    val imageIntrinsicHeight = drawable.intrinsicHeight

    val imageViewAspectRatio = iv.width.toFloat() / iv.height.toFloat()
    val bitmapAspectRatio = bitmap.width.toFloat() / bitmap.height.toFloat()

    val imageViewRect = Rect()
    iv.getGlobalVisibleRect(imageViewRect)

    val widthImageView = iv.width
    val heightImageView = iv.height

    val imageViewLeft = imageViewRect.left
    val imageViewTop = imageViewRect.top

    val imageViewRight = imageViewRect.right
    val imageViewBottom = imageViewRect.bottom


    var topBitmap = 0f
    var bottomBitmap = 0f
    var leftBitmap = 0f
    var rightBitmap = 0f
    if (imageViewAspectRatio > bitmapAspectRatio) {
        val heightBitmap = heightImageView
        val widthBitmap =
            (heightBitmap.toFloat() / imageIntrinsicHeight.toFloat()) * imageIntrinsicWidth

        val y = (widthImageView.toFloat() - widthBitmap.toFloat()) / 2
        topBitmap = imageViewTop.toFloat()
        bottomBitmap = imageViewBottom.toFloat()
        leftBitmap = imageViewLeft + y
        rightBitmap = leftBitmap + widthBitmap
    } else {

       
        val widthBitmap = widthImageView
        val heightBitmap =
            (widthBitmap.toFloat() / imageIntrinsicWidth.toFloat()) * imageIntrinsicHeight

        
        val x = (heightImageView.toFloat() - heightBitmap.toFloat()) / 2

        topBitmap = imageViewTop + x
        bottomBitmap = topBitmap + heightBitmap
        rightBitmap = imageViewRight.toFloat()
        leftBitmap = imageViewLeft.toFloat()
    }
    val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")
    var statusBarHeight =
        if (resourceId > 0) context.resources.getDimensionPixelSize(resourceId) else 0

    topBitmap -= statusBarHeight
    bottomBitmap -= statusBarHeight

    return Coordinator(topBitmap, leftBitmap, rightBitmap, bottomBitmap)
}

}

To use it in activity or fragment, or anywhere

   binding.btLog.setOnClickListener {
        val intrinsic = GetIntrinsicImageview(this)
        val coordinator = intrinsic.getCoordinator(binding.iv, mBitmap)
        val left = coordinator.left
        val top = coordinator.top
        val right = coordinator.right
        val bottom = coordinator.bottom
    }

you need to enter that bitmap and imageview, the returned result will be top, bottom, right and left

ninh312
  • 39
  • 4
-1

How about ImageView.getBackground() or ImageView.getDrawable(), then Drawable.getBounds()?

Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26