Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rotate an image continuously?

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();
    }
}
like image 468
user2152295 Avatar asked Jan 29 '14 11:01

user2152295


People also ask

How do I rotate an image in CSS?

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.


2 Answers

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>
like image 55
Rajan Avatar answered Nov 10 '22 00:11

Rajan


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);
like image 34
MetaSnarf Avatar answered Nov 09 '22 23:11

MetaSnarf