Created
February 17, 2017 10:48
-
-
Save defHLT/1e31d1efb133a2332e6545d43c2ea2f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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