Animate a view in a circle in Android

Move ImageView around the circle with animation

CodingwithSaud
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.

  1. Path
  2. RectF
  3. PathMeasure
  4. ValueAnimator for animation
  5. 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();
}

--

--