Skip to content

Instantly share code, notes, and snippets.

@balachandarlinks
Last active June 15, 2019 21:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save balachandarlinks/b4e93305ea7a1a7199e9 to your computer and use it in GitHub Desktop.
Save balachandarlinks/b4e93305ea7a1a7199e9 to your computer and use it in GitHub Desktop.
ImageFlipper helps you to run a flip animation with a series of images on any ImageView.
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