Skip to content

Instantly share code, notes, and snippets.

@AlonsoFloo
Created September 19, 2018 09:49
Show Gist options
  • Save AlonsoFloo/6cbd9060cbbdaa630b2fe7f61ea56f5c to your computer and use it in GitHub Desktop.
Save AlonsoFloo/6cbd9060cbbdaa630b2fe7f61ea56f5c to your computer and use it in GitHub Desktop.
Android Custom ImageView for animating frame by frame some bitmap or ressources
/**
* <h1>FrameAnimatedImageView</h1>
* View class to handle several images into an {@link android.widget.ImageView}
* <p>
*
* @author Florian ALONSO
* @see BitmapDrawable
* @see AnimationDrawable
* @see ContextCompat
*/
public class FrameAnimatedImageView extends android.support.v7.widget.AppCompatImageView {
private AnimationDrawable animationDrawable;
public FrameAnimatedImageView(Context context) {
super(context);
}
public FrameAnimatedImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public FrameAnimatedImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* Start an animation with the list of resources given
*
* @param resourcesList the list of images to display in the specified order
*/
public void startAnimatedResources(final @DrawableRes int... resourcesList) {
this.startAnimatedResources(777, resourcesList);
}
/**
* Start an animation with the list of resources given with a defined time of animation in ms
*
* @param resourcesList the list of images to display in the specified order
* @param loopDurationMs the total time of the animation required in ms
*/
public void startAnimatedResources(final int loopDurationMs, final @DrawableRes int... resourcesList) {
final BitmapDrawable[] drawablesList = new BitmapDrawable[resourcesList.length];
for (int i = 0; i < resourcesList.length; i++) {
Drawable drawable = ContextCompat.getDrawable(getContext(), resourcesList[i]);
if (drawable != null && drawable instanceof BitmapDrawable) {
drawablesList[i] = (BitmapDrawable) drawable;
}
}
this.startAnimatedDrawable(drawablesList, loopDurationMs);
}
/**
* Start an animation with the list of images given with a defined time of animation in ms
*
* @param drawablesList the list of images to display in the specified order
*/
public void startAnimatedDrawable(final BitmapDrawable[] drawablesList) {
this.startAnimatedDrawable(drawablesList, getContext().getResources().getInteger(R.integer.animation_duration_framed_image));
}
/**
* Start an animation with the list of images given with a defined time of animation in ms
*
* @param drawablesList the list of images to display in the specified order
* @param loopDurationMs the total time of the animation required in ms
*/
public void startAnimatedDrawable(final BitmapDrawable[] drawablesList, final int loopDurationMs) {
if (animationDrawable != null) {
stopAnimatedFrames();
}
if (drawablesList.length == 0) {
return;
}
animationDrawable = new AnimationDrawable();
int singleImageDurationMs = loopDurationMs / drawablesList.length;
for (BitmapDrawable bitmapDrawable : drawablesList) {
animationDrawable.addFrame(bitmapDrawable, singleImageDurationMs);
}
animationDrawable.setOneShot(false);
this.setImageDrawable(animationDrawable);
animationDrawable.start();
}
/**
* Stops all animations in the view
*/
public void stopAnimatedFrames() {
if (animationDrawable != null) {
animationDrawable.stop();
animationDrawable = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment