I have strange behavior with ImageView (scale type = Matrix).
I have some parent view (FrameLayout actually, marked grey on picture). On the top of this FrameLayout my ImageView is located with MATCH_PARENT attributes.
So I expect ImageView should fill parent view. I have Bitmaps with different sizes to set in different cases in my ImageView (with MATRIX ScaleType). Sometimes it is smaller than ParentView, sometimes bigger. I want to adjust bitmap to parent bounds using imageView.setScaleY(scaleFactor) and it totally works if scaleFactor is > 1. But if Bitmap is actually bigger and scaleFactor is < 1, ImageView behaves pretty strange: ImageView's Drawable is scaled correctly and try to fill all parent view, but for some reason ImageView bounds is scaled down (in comparison to MATCH_PARENT). The result is illustrated in picture below (with scale factor ~ 0.5).
So looks like ImageView size is scaled down with Bitmap and I cannot prevent it with using imageView.setMinimumWidth/Height or LayoutParams.
Any ideas?
P.S. I cannot scale ImageView using Matrix transformation (because it is used for different purposes in my app - zoom/move/rotate)
It's hard for me to answer without seeing your code, but I will make a guess.
I think you are confusing scaling the bitmap within the ImageView
and scaling the ImageView
widget itself. These are two different things.
So you put your ImageView
inside a FrameLayout
with MATCH_PARENT
for width and height. Now your ImageView
should be the same size as your FrameLayout
. All well and good.
The ImageView
is a view. All views can be scaled (and translated and rotated).
When you say imageView.setScaleX()
and imageView.setScaleY()
, you are changing the bounds of the widget. Doesn't matter what the layout parameters are, you are changing the size of the view.
Now, ImageView
is special because it has its own way of scaling the source image without changing its bounds. This is using the scaleType
attribute. So with an ImageView
you can change the size of the image without changing the bounds of the ImageView
.
I wrote a pinch/zoom view and used the scaleType="matrix"
which is what I think you're doing. So here's how I solved the problem you seem to be having:
The bitmap comes in with certain dimensions. The ImageView
has certain dimensions once it's been laid out. So let's say the bitmap and the ImageView
have the same dimensions. The starting scale for your matrix would be 1. Very simple.
So what if the dimensions are different? You would have to compute a starting scale. Here's how I did it: I created one Rect
(src) with the bitmap dimensions, and another Rect
(dest) with the ImageView
dimensions. Then I called matrix.setRectToRect(src, dest, ScaleToFit.START)
. This method sets the scale factor on the matrix so that when you apply the matrix on the bitmap (for example ImageView.setImageMatrix()
), the bitmap is correctly resized to the dimensions of the ImageView
.
Because you haven't posted any code it's hard for me to know how applicable this is, but hopefully something I posted here will put you in the right direction.