Skip to content

Instantly share code, notes, and snippets.

@shauvik
Forked from balachandarlinks/ImageFlipper.java
Last active August 29, 2015 14:23
Show Gist options
  • Save shauvik/0420445bcb47800da2a6 to your computer and use it in GitHub Desktop.
Save shauvik/0420445bcb47800da2a6 to your computer and use it in GitHub Desktop.
public class ImageFlipper{
private static final int START_ANGLE = 90;
private static final int END_ANGLE = 270;
private int animationDuration = 800;
private ObjectAnimator flipObjectAnimator;
public ImageFlipper(final View targetView, final int[] drawableResources){
((ImageView)targetView).setImageResource(drawableResources[0]);
flipObjectAnimator = ObjectAnimator.ofFloat(targetView, "rotationY", START_ANGLE, END_ANGLE);
flipObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);
flipObjectAnimator.setDuration(animationDuration);
flipObjectAnimator.addListener(new Animator.AnimatorListener(){
int currentImageIndex = 0;
int maxImageIndex = drawableResources.length - 1;
@Override
public void onAnimationStart(Animator animation){}
@Override
public void onAnimationEnd(Animator animation){}
@Override
public void onAnimationCancel(Animator animation){}
@Override
public void onAnimationRepeat(Animator animation){
currentImageIndex = currentImageIndex == maxImageIndex ? 0 : currentImageIndex + 1;
((ImageView)targetView).setImageResource(drawableResources[currentImageIndex]);
}
});
}
public void setDuration(int animationDuration){
this.animationDuration = animationDuration;
}
public void start(){
flipObjectAnimator.start();
}
public void cancel(){
flipObjectAnimator.cancel();
}
}
# Usage:
ImageView myImageView = (ImageView)findViewById(R.id.image_view);
int[] drawables = { R.drawable.image_1, R.drawable.image_2, R.drawable.image_3 };
ImageFlipper imageFlipper = new ImageFlipper(myImageView, drawables);
imageFilpper.start();
# Example:
https://www.youtube.com/watch?v=DnBXjLQuCWk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment