Class AnimationDrawable

All Implemented Interfaces:
Animatable, Drawable.Callback, Runnable

public class AnimationDrawable extends DrawableContainer implements Runnable, Animatable
An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
  • Constructor Details

    • AnimationDrawable

      public AnimationDrawable()
  • Method Details

    • setVisible

      public boolean setVisible(boolean visible, boolean restart)
      Sets whether this AnimationDrawable is visible.

      When the drawable becomes invisible, it will pause its animation. A subsequent change to visible with restart set to true will restart the animation from the first frame. If restart is false, the drawable will resume from the most recent frame. If the drawable has already reached the last frame, it will then loop back to the first frame, unless it's a one shot drawable (set through setOneShot(boolean)), in which case, it will stay on the last frame.

      Overrides:
      setVisible in class DrawableContainer
      Parameters:
      visible - true if visible, false otherwise
      restart - when visible, true to force the animation to restart from the first frame
      Returns:
      true if the new visibility is different than its previous state
    • start

      public void start()
      Starts the animation from the first frame, looping if necessary. This method has no effect if the animation is running.

      You may want to call this from Fragment.onViewCreated(icyllis.modernui.view.View, icyllis.modernui.util.DataSet)

      Specified by:
      start in interface Animatable
      See Also:
    • stop

      public void stop()
      Stops the animation at the current frame. This method has no effect if the animation is not running.
      Specified by:
      stop in interface Animatable
      See Also:
    • isRunning

      public boolean isRunning()
      Indicates whether the animation is currently running or not.
      Specified by:
      isRunning in interface Animatable
      Returns:
      true if the animation is running, false otherwise
    • run

      public void run()
      This method exists for implementation purpose only and should not be called directly. Invoke start() instead.
      Specified by:
      run in interface Runnable
      See Also:
    • unscheduleSelf

      public void unscheduleSelf(@NonNull Runnable what)
      Description copied from class: Drawable
      Use the current Drawable.Callback implementation to have this Drawable unscheduled. Does nothing if there is no Callback attached to the Drawable.
      Overrides:
      unscheduleSelf in class Drawable
      Parameters:
      what - The runnable that you no longer want called.
      See Also:
    • getNumberOfFrames

      public int getNumberOfFrames()
      Returns:
      The number of frames in the animation
    • getFrame

      public Drawable getFrame(int index)
      Returns:
      The Drawable at the specified frame index
    • getDuration

      public int getDuration(int i)
      Returns:
      The duration in milliseconds of the frame at the specified index
    • isOneShot

      public boolean isOneShot()
      Returns:
      True of the animation will play once, false otherwise
    • setOneShot

      public void setOneShot(boolean oneShot)
      Sets whether the animation should play once or repeat.
      Parameters:
      oneShot - Pass true if the animation should only play once
    • addFrame

      public void addFrame(@NonNull Drawable frame, int duration)
      Adds a frame to the animation
      Parameters:
      frame - The frame to add
      duration - How long in milliseconds the frame should appear
    • mutate

      @NonNull public Drawable mutate()
      Description copied from class: Drawable
      Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification.

      Calling this method on a mutable Drawable will have no effect.

      Overrides:
      mutate in class DrawableContainer
      Returns:
      This drawable.
      See Also:
    • setConstantState

      protected void setConstantState(@NonNull DrawableContainer.DrawableContainerState state)
      Overrides:
      setConstantState in class DrawableContainer