Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing IMAGEVIEW on image scale android

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.

like image 451
user756212 Avatar asked Jul 11 '11 14:07

user756212


2 Answers

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).

like image 105
rharter Avatar answered Oct 21 '22 18:10

rharter


Set "Adjust View Bounds" true, and then set the scale type to "fitXY" finally adjut layout_width and height. You will see that the image resizes.

like image 42
Humberto Castañeda Avatar answered Oct 21 '22 18:10

Humberto Castañeda