Forked from defHLT/move-android-view-along-path.java
Created
February 17, 2019 19:08
-
-
Save m-cakir/1c0e32911361becbaa2c8335ab4f51be 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