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!