1

I need to rotate an ImageView using an animation, so that the width and height of the ImageView also change.

I have tried using ViewPropertyAnimator, using View.animate but this just rotates the image, and not the actual view itself.

I need to use this so I can work out the x and y coords of the view after it has been rotated, and also the width and height of it.

I have made a test app to just rotate a card image, and I have taken pictures with the layouts visible to show what I mean.

As you can see in the code, I've also tried to force change the width and height in a runnable after the animation has finished, but this doesn't work. Nor does using scaleX/scaleY to try and change things.

Before rotation

After rotation

public class MyActivity extends Activity implements View.OnClickListener{

RelativeLayout temp;

ImageView card;
int rotation = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    card = (ImageView) findViewById(R.id.card);
    card.setOnClickListener(this);
}

@Override
public void onClick(final View v) {


    final Runnable e1 = new Runnable() {
        public void run() {
            Toast.makeText(MyActivity.this, "After1 w="+v.getMeasuredWidth()+" h="+v.getMeasuredHeight(), Toast.LENGTH_SHORT).show();
            int w = v.getMeasuredWidth();
            int h = v.getMeasuredHeight();
            v.getLayoutParams().width = h;
            v.getLayoutParams().height = w;
            v.setLayoutParams(v.getLayoutParams());
            Toast.makeText(MyActivity.this, "After2 w="+v.getMeasuredWidth()+" h="+v.getMeasuredHeight(), Toast.LENGTH_SHORT).show();

        }
    };

    int[] cardCoords = new int[2];
    v.getLocationOnScreen(cardCoords);
    Toast.makeText(MyActivity.this, "x="+cardCoords[0]+" y="+cardCoords[1], Toast.LENGTH_SHORT).show();
    Toast.makeText(this, "Before w="+v.getMeasuredWidth()+" h="+v.getMeasuredHeight(), Toast.LENGTH_SHORT).show();
    rotation = rotation + 90;

    v.animate().
            setDuration(1000).
            rotationBy(90);
            //scaleX((float) v.getMeasuredHeight() / (float) v.getMeasuredWidth()).
            //scaleY((float) v.getMeasuredHeight() / (float) v.getMeasuredWidth()).
            //.withEndAction(e1);

}

}

Russ Wheeler
  • 2,590
  • 5
  • 30
  • 57

1 Answers1

0

You should be looking to rotate a view, not the image then.

Rotating a view in Android

Community
  • 1
  • 1
Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
  • Which part of that SO post? I tried @Pete answer and that did the same thing as my code, and I'm confused as to how to use the onDraw? Won't that display it at the 90 degree angle as soon as the view is added? I can already add a new view with diff width height, I need to change the width and height of this view, during the animation. – Russ Wheeler Oct 21 '14 at 21:30
  • Change the height and width after the animation, then. – Vic Vuci Oct 21 '14 at 21:56
  • You can see that I try that in the runnable after, however this squashes the image. If there is a way I can adjust the width and height and leave the image appearing the same then this is the solution (all be it a bit hacky) Know how I can do that? – Russ Wheeler Oct 21 '14 at 22:01