-
-
Save IRMobydick/704340bfd4773401d2c5 to your computer and use it in GitHub Desktop.
This file contains 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
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