how to rotate an image continuously?

android, android-animation, android-imageview

Solution

use this code for rotating a button

btn_rotate = (Button)findViewById(R.id.btn_rotate);
rotation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
rotation.setFillAfter(true);
btn_rotate.startAnimation(rotation);

rotate.xml

put this file in res->anim->rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<rotate
    android:duration="500"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:startOffset="0"
    android:toDegrees="360" />
</set>

Problem

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(); } } ```

Original source