Resizing IMAGEVIEW on image scale android

user756212 picture user756212 · Jul 11, 2011 · Viewed 9.1k times · Source

I have an image inside an imageview in android.

I am scaling the image using scaletype(MATRIX) via code similar to this:

                       mtrx.postScale(scale, scale);
                 imageView.setImageMatrix(mtrx);
            imageView.setScaleType(ScaleType.MATRIX);
            imageView.invalidate();

Now, this does properly resize the image contained in the ImageView, which is nice, but what I need is to reposition the image to location 0,0 within the imageview and then resize the imageView itself to the size of the scaled image.

So far every Idea I have come up with to attempt to reuse the same imageView doesn't seem to work, or I am just doing something wrong.

Is my only way to accomplish this to destroy the imageview, resize the bitmap, create a new imageview containing the new bitmap created at scale and adding the view back the layout?

I'd really like NOT to do that.. I am bumping up against the 16 Meg heap limit as it is with things. How do I tell the matrix to move the image to position 0,0 in the imageView (the imageView is initially created with scaletype center, after it has scaled the image smaller? And how do I then tell the imageView, hey you are now only as big as the scaled Image?

Thanks in advance.

Answer

rharter picture rharter · Aug 20, 2011

Setting your image view to "Adjust View Bounds" appears to do the trick.

Here's what the docs have to say:

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

You can do this by either using android:adjustViewBounds="true" or in code with imageView.setAdjustViewBounds(true).