Skip to content

Instantly share code, notes, and snippets.

@defHLT
Created February 17, 2017 10:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save defHLT/1e31d1efb133a2332e6545d43c2ea2f7 to your computer and use it in GitHub Desktop.
Save defHLT/1e31d1efb133a2332e6545d43c2ea2f7 to your computer and use it in GitHub Desktop.
final View target = findViewById(R.id.target);
// Create path you would like your view to follow
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);
// Animating from 0 to our path length
ValueAnimator animator = ValueAnimator.ofFloat(0, pm.getLength());
// This will hold current position
final float[] pos = new float[2];
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// Getting current length on the path
float v = (float) animation.getAnimatedValue();
// Getting current position..
pm.getPosTan(v, pos, null);
// ..and applying it
target.setTranslationX(pos[0]);
target.setTranslationY(pos[1]);
}
});
animator.setDuration(3000);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment