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();
}
}
css file, stylesheet, or <style> tags, you can use the CSS class name in any of your image tags. To rotate an image by another measure of degrees, change the "180" in the CSS code and <img> tag to the degree you desire.
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);
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>
I know it's probably late but here's how I do a rotate animation on java code:
RotateAnimation rotate = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
);
rotate.setDuration(900);
rotate.setRepeatCount(Animation.INFINITE);
itemImage.startAnimation(rotate);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With