5

How can I have an ImageView update it's width/height when rotated?

If I have the below ImageView and debug, the width is 827 and height is 543. After rotation, the width/height remain the same causing issues with my animation.

The image is correctly rotated on screen, but I would expect ImageView dimensions to be updated (width = 543, height = 827).

<ImageView
    android:id="@+id/imageView2"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_centerInParent="false"
    android:src="@drawable/img"
    android:adjustViewBounds="true"
    android:padding="0dp"
    android:background="@android:color/transparent"
    android:visibility="visible"
    android:layout_centerVertical="false" />

    _img = FindViewById<ImageView>(Resource.Id.imageView2); //width = 827, height = 543
    _img.Rotation = -90;
// width is still 827 and height is still 543 
user2966445
  • 1,267
  • 16
  • 39
  • 1
    If you're rotating view itself then it's quite normal that width and height remain same, although after 90 degree rotation of a view width in reality is height and height is width. If you have trouble with other animations after this, it would mean you're using wrong kind of math (a.k.a you're not using rotation value in your calculations). – JuliusScript Jun 04 '16 at 17:32
  • If width stays the same, where in the ImageView is the Bitmap located? Is it based on gravity setting? – user2966445 Jun 04 '16 at 17:50
  • Not sure I get your question now, is image displayed fully (rotated, but fully visible)? Since you're rotating view it self content is still in x=0, y=0, rotation=0. – JuliusScript Jun 04 '16 at 17:56
  • If the width of the ImageView doesn't change after a rotation (_img.Rotation = -90), what happens to the extra space inside the ImageView (since the Bitmap itself now has a width of 543, but the ImageView still has width of 827)? – user2966445 Jun 04 '16 at 18:04
  • Bitmap should still have 827 width, imagine view has its own coordinate plane (with x,y,z axis) which exists in another coordinate plane (layout), bitmap exists in imageview coordinate plane which is rotated, if you were to draw it on piece of paper bitmap width would still be on x axis starting at 0 ending at 827. – JuliusScript Jun 04 '16 at 18:29
  • OK, that sort of makes sense. What I'm trying to do is animate an ImageView from off-screen, make it appear from the left and disappear again. The image is already rotated -90 and I'm setting the starting point (x) = -1 * _img.Width (offscreen). The endpoint of the animation is 0 (since I want the image to appear at the left edge of the screen). But, since the image is rotated, the image too far to the right, causing a gap between the ImageView and RelativeLayout container. Does this make sense? – user2966445 Jun 04 '16 at 19:26
  • If rotation is always -90 then you could try (x) = -1 * _img.Height. Unless rotation changes between 0 and -90? – JuliusScript Jun 04 '16 at 20:48
  • I think you should use matrix. I hope [this](http://stackoverflow.com/questions/8981845/android-rotate-image-in-imageview-by-an-angle) can help you. – momvart Jun 06 '16 at 15:45
  • @user2912553 Yes, I tried using Matrix rotation and it worked along with using the ImageView height to calculate correct position. But, I was able to avoid having to use Matrix rotation by making the ImageView source/bitmap a square :-) – user2966445 Jun 06 '16 at 16:34

0 Answers0