Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Created July 21, 2015 13:16
Show Gist options
  • Save QuadFlask/a2a7683650d2f9925794 to your computer and use it in GitHub Desktop.
Save QuadFlask/a2a7683650d2f9925794 to your computer and use it in GitHub Desktop.
Simple slide object animator
private static final Interpolator INTERPOLATOR = new LinearInterpolator();
private static int DURATION = 300;
private Property<View, Float> translateProperty = new Property<View, Float>(Float.class, "translateX") {
@Override
public Float get(View view) {
return view.getTranslationX();
}
@Override
public void set(View view1, Float value) {
view1.setTranslationX(value * view1.getWidth());
}
};
public void slideParallaxToLeft(List<View> views, int startIndexView) {
for (int i = 0; i < views.size(); i++) {
View view = views.get(i);
long delay = Math.abs(i - startIndexView) * 50;
ObjectAnimator objectAnimator = slideToLeft(view);
objectAnimator.setStartDelay(delay);
objectAnimator.start();
}
}
public void slideParallaxToOriginal(List<View> views, int startIndexView) {
for (int i = 0; i < views.size(); i++) {
View view = views.get(i);
long delay = Math.abs(i - startIndexView) * 50;
ObjectAnimator objectAnimator = slideToOriginal(view);
objectAnimator.setStartDelay(delay);
objectAnimator.start();
}
}
public ObjectAnimator slideToOriginal(final View view) {
return slide(view, -0.8f, 0);
}
public ObjectAnimator slideToLeft(final View view) {
return slide(view, 0, -0.8f);
}
private ObjectAnimator slide(final View view, float initial, float target) {
final ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, translateProperty, initial, target);
objectAnimator.setInterpolator(INTERPOLATOR);
objectAnimator.setDuration(DURATION);
objectAnimator.setRepeatMode(ValueAnimator.RESTART);
// objectAnimator.addListener(new Animator.AnimatorListener() {
// @Override
// public void onAnimationStart(Animator animation) {
// view.invalidate();
// }
//
// @Override
// public void onAnimationEnd(Animator animation) {
// view.invalidate();
// objectAnimator.cancel();
// }
//
// @Override
// public void onAnimationCancel(Animator animation) {
// objectAnimator.cancel();
// }
//
// @Override
// public void onAnimationRepeat(Animator animation) {
// objectAnimator.cancel();
// }
// });
return objectAnimator;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment