I am trying to rotate this image by using a thread. what should I do?
public class Start extends Activity {
View base;
Bitmap rotate, base1, rotate1;
ImageView rotator;
float angle, pivX, pivY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
base = findViewById(R.id.base);
rotator = (ImageView) findViewById(R.id.rotator);
pivX = (rotator.getLeft()) / 2;
pivY = (rotator.getTop()) / 2;
Thread thread = new Thread() {
@Override
public void run() {
for (angle = 0; angle < 50; angle++) {
Matrix matrix = new Matrix();
rotator.setScaleType(ScaleType.MATRIX); // required
matrix.postRotate((float) angle, pivX, pivY);
rotator.setImageMatrix(matrix);
if (angle == 40) {`enter code here`
angle = 0;
return;
}
}
}
};
thread.start();
}
}
In Kotlin:
rotate_indefinitely.xml
Check this out ..
Your Java class ..
your xml file ..
And one more thing .. you need to define anim folder in your res folder.
now place rotate.xml file in anim folder.
rotate.xml file
That's it. you are good to go.
use this code for rotating a button
rotate.xml
put this file in res->anim->rotate.xml
I know it's probably late but here's how I do a rotate animation on java code: