Animate a view in a circle in Android
Move ImageView around the circle with animation
Feb 13, 2021
In this article I have created a method which will move ImageView with animation in a circle.
In this method I have used.
- Path
- RectF
- PathMeasure
- ValueAnimator for animation
- ImageView
You can take any view instead of ImageView, it depends on your requirements.
public void moveImageInCircle(){
Path p = new Path();
RectF circle = new RectF(0, 0, 400f, 400f);
p.arcTo(circle, 0, 180);
p.arcTo(circle, 180, 180);
final PathMeasure pm = new PathMeasure(p, false);
ValueAnimator animator = ValueAnimator.ofFloat(0, pm.getLength());
final float[] pos = new float[2];
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float v = (float) animation.getAnimatedValue();
pm.getPosTan(v, pos, null);
imageView.setTranslationX(pos[0]);
imageView.setTranslationY(pos[1]);
}
});
animator.setDuration(3000);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.start();
}