-1

enter image description here

I need this kind of smooth rotation animation. I have used RotateAnimation for that. I need it in both directions ClockWise and AntiClokWise but my code doesn't give output what i expect from this gif image.

Here is my code

public class MainActivity extends AppCompatActivity {

ImageView imgView;
private double mCurrAngle = 0;
private double mPrevAngle = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imgView = findViewById(R.id.imgView);

    imgView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            final float xc = imgView.getWidth() / 2;
            final float yc = imgView.getHeight() / 2;

            final float x = event.getX();
            final float y = event.getY();
            double angrad = Math.atan2(x - xc, yc - y);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    imgView.clearAnimation();
                    mCurrAngle = Math.toDegrees(angrad);
                    animate(mPrevAngle, mCurrAngle, 2000);
                    break;
                }
                case MotionEvent.ACTION_MOVE: {
                    mPrevAngle = mCurrAngle;
                    mCurrAngle = Math.toDegrees(angrad);
                    animate(mPrevAngle, mCurrAngle, 2000);
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    mPrevAngle = mCurrAngle = 0;
                    break;
                }
            }
            return false;
        }
    });

}

private void animate(double fromDegrees, double toDegrees, long durationMillis) {
    final RotateAnimation rotate = new RotateAnimation((float) fromDegrees, (float) toDegrees,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(durationMillis);
    rotate.setInterpolator(new LinearInterpolator());
    rotate.setFillEnabled(true);
    rotate.setFillAfter(true);
    imgView.startAnimation(rotate);
}

So anyone please review this question? Thanks for advance help!

Piyush
  • 395
  • 5
  • 25

1 Answers1

0
 public void rotate(View view){
        ImageView image = (ImageView)findViewById(R.id.imageView);
        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.rotate);
        image.startAnimation(animation);
    }

code rotate.xml

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

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000" >
    </rotate>

    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:startOffset="5000"
        android:fromDegrees="360"
        android:toDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000" >
    </rotate>

</set>

please check the code i think it will help you....

ACHU
  • 48
  • 9