Thursday, February 21, 2019

Rotate a View


We want a view like image, button rotate many times, use this codes.
RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(500);             
rotate.setInterpolator(new LinearInterpolator());
rotate.setRepeatMode(Animation.RELATIVE_TO_SELF);
rotate.setRepeatCount(11);       
im.startAnimation(rotate);    
Change value of line rotate.setDuration(500); to rotate faster or slower.
To make it rotate forever.
rotate.setRepeatCount(Animation.INFINITE);

To make an icon rotate in certain radius.
RotateAnimation rotate = new RotateAnimation(0, 360,0,200);
Radius will be 100dp.
To make it rotate itself.
RotateAnimation rotate = new RotateAnimation(0, 360);
You must set width, height  to wrapcontent for image in xml file.
If we want to have clock pendulum moving, create an icon has the rope start at top left corner like this.

Use this lines of code.
RotateAnimation rotate = new RotateAnimation(-20, 30);
rotate.setDuration(1500);
rotate.setInterpolator(new LinearInterpolator());
rotate.setFillAfter(true);
rotate.setRepeatMode(Animation.REVERSE);       
rotate.setRepeatCount(Animation.INFINITE);
Run to see it moving like clock pendulum.

No comments:

Post a Comment