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