I'm having a problem with displaying my image.
I have an Image I want to display full screen. So I have this Imageview with match_parent and 20dp padding.
It looks good but when I apply rotation on it, it seems that the bounds of the view doesn't change and the image can get clipped out of the screen ! Totally don't want that to happen! How do I rescale the image so that the image also fits in the ImageView when its 90 degrees rotated.
This is my XML WITH rotation in it.
EDIT:
How to fix the bounds of the Image so the Text is aligned just above the image?
according to a research leading to this topic i wonder if @Sarge Borsch answer could work in your case.
Try setting
If centerInside is not correct because you want display in center, maybe try to position the imageview instead of the image inside.
Another suggestion: your imageview is set on "wrap_content" and i don't know exactly the order of everything but maybe the problem comes because it rotates after calculating dimensions (because of wrap_content). I think it is a possibility because the screenshoot you put shows that the image is not even fitting the width. TL;DR : try to fix the imageview size (padding on activity + match_parent) instead of wrap content, in combination of "adjustViewBounds".
The rotation is not taken into account when measuring the view and calculating the scale ratio. A possible solution is to do it yourself :
adjustViewBounds
must be true :A nice explanation of the calculation, courtesy of Cheticamp :
UPDATE: Now trying to adjust the bounds. There is no difference between
wrap_content
andmatch_parent
(both grow as much as possible, based on the image aspect). You should instead usemaxWidth
and / ormaxHeight
, or put it in aLinearLayout
with a 0 size and a weight.It is also not animatable, adjusting bounds while animating requires a layout pass for each frame, which is very inefficient. See the other answer for a version usable with
View.animate()
The attribute
android:rotation
refers to the View that is the ImageView, not its contents.If you want to rotate the contents, either set a new BItmap as content, or override
onDraw()
and rotate the canvasAnother version of the RotatedImageView which rotation can be animated with a
ViewPropertyAnimator
. The idea is the same, but the scaling is done inonDraw()
instead ofonMeasure()
, so it does not need a layout pass each time. In order to make the animation work, I had to hijack the update listener. If you want to use your own listener, don't forget toinvalidate()
the view inonAnimationUpdate()
.Use example :