SeekableAnimatedVectorDrawable


public class SeekableAnimatedVectorDrawable extends Drawable implements Animatable


This class animates properties of a VectorDrawableCompat with animations defined using ObjectAnimator or AnimatorSet.

SeekableAnimatedVectorDrawable is defined in the same XML format as android.graphics.drawable.AnimatedVectorDrawable.

Here are all the animatable attributes in VectorDrawableCompat:

Element Name Animatable attribute name
alpha
rotation
pivotX
pivotY
scaleX
scaleY
translateX
translateY
fillColor
pathData
strokeColor
strokeWidth
strokeAlpha
fillAlpha
trimPathStart
trimPathEnd
trimPathOffset

You can always create a SeekableAnimatedVectorDrawable object and use it as a Drawable by the Java API. In order to refer to SeekableAnimatedVectorDrawable inside an XML file, you can use app:srcCompat attribute in AppCompat library's ImageButton or ImageView.

SeekableAnimatedVectorDrawable supports the following features too:

  • Path Morphing (PathType evaluator). This is used for morphing one path into another.
  • Path Interpolation. This is used to defined a flexible interpolator (represented as a path) instead of the system defined ones like LinearInterpolator.
  • Animating 2 values in one ObjectAnimator according to one path's X value and Y value. One usage is moving one object in both X and Y dimensions along an path.

Unlike AnimatedVectorDrawableCompat, this class does not delegate to the platform android.graphics.drawable.AnimatedVectorDrawable on any API levels.

Summary

Nested types

Abstract class for animation callback.

Public methods

void
boolean
void

Removes all existing animation callbacks.

static @Nullable SeekableAnimatedVectorDrawable
create(@NonNull Context context, @DrawableRes int resId)

Create a SeekableAnimatedVectorDrawable object.

static @NonNull SeekableAnimatedVectorDrawable
createFromXmlInner(
    @NonNull Resources r,
    @NonNull XmlPullParser parser,
    @NonNull AttributeSet attrs,
    @Nullable Resources.Theme theme
)

Create a SeekableAnimatedVectorDrawable from inside an XML document using an optional Theme.

void
draw(@NonNull Canvas canvas)
@IntRange(from = 0, to = 255) int
int
@Nullable ColorFilter
@Nullable Drawable.ConstantState
@IntRange(from = 0) long

Returns the milliseconds elapsed since the start of the animation.

int
int
int

This method is deprecated.

This method is no longer used in graphics optimizations

long

Gets the total duration of the animation, accounting for animation sequences, start delay, and repeating.

void
inflate(
    @NonNull Resources res,
    @NonNull XmlPullParser parser,
    @NonNull AttributeSet attrs
)
void
inflate(
    @NonNull Resources res,
    @NonNull XmlPullParser parser,
    @NonNull AttributeSet attrs,
    @Nullable Resources.Theme theme
)
boolean
boolean

Returns whether the animation is currently in a paused state.

boolean

Returns whether the animation is running (has started and not yet ended).

boolean
@NonNull Drawable

mutate() is not supported.

void

Pauses a running animation.

void

Adds a callback to listen to the animation events.

void

Resumes a paused animation.

void
setAlpha(@IntRange(from = 0, to = 255) int alpha)
void
setAutoMirrored(boolean mirrored)
void
void
setCurrentPlayTime(@IntRange(from = 0) long playTime)

Sets the position of the animation to the specified point in time.

void
setTint(@ColorInt int tint)
void
void
boolean
setVisible(boolean visible, boolean restart)
void
void
boolean

Removes the specified animation callback.

Protected methods

void
boolean
onLevelChange(int level)
boolean
onStateChange(@NonNull int[] state)

Inherited methods

From android.graphics.drawable.Drawable
void
final Rect
final void
copyBounds(Rect bounds)
static Drawable
static Drawable
createFromResourceStream(
    Resources res,
    TypedValue value,
    InputStream is,
    String srcName
)
static Drawable
static Drawable
static Drawable
createFromXmlInner(
    Resources r,
    XmlPullParser parser,
    AttributeSet attrs
)
final Rect
Drawable.Callback
Drawable
Rect
void
int
final int
int
int
Insets
void
boolean
getPadding(Rect padding)
int[]
Region
boolean
void
boolean
boolean
final boolean
void
boolean
onLayoutDirectionChanged(int layoutDirection)
static int
resolveOpacity(int op1, int op2)
void
scheduleSelf(Runnable what, long when)
void
setBounds(int left, int top, int right, int bottom)
final void
void
void
setDither(boolean dither)

This method is deprecated.

void
setFilterBitmap(boolean filter)
void
setHotspot(float x, float y)
void
setHotspotBounds(int left, int top, int right, int bottom)
final boolean
setLayoutDirection(int layoutDirection)
final boolean
setLevel(int level)
boolean
setState(int[] stateSet)
void
void

Public methods

applyTheme

public void applyTheme(@NonNull Resources.Theme t)

canApplyTheme

public boolean canApplyTheme()

clearAnimationCallbacks

Added in 1.0.0
public void clearAnimationCallbacks()

Removes all existing animation callbacks.

create

Added in 1.0.0
public static @Nullable SeekableAnimatedVectorDrawable create(@NonNull Context context, @DrawableRes int resId)

Create a SeekableAnimatedVectorDrawable object.

Parameters
@NonNull Context context

the context for creating the animators.

@DrawableRes int resId

the resource ID for SeekableAnimatedVectorDrawable object.

Returns
@Nullable SeekableAnimatedVectorDrawable

a new SeekableAnimatedVectorDrawable or null if parsing error is found.

createFromXmlInner

Added in 1.0.0
public static @NonNull SeekableAnimatedVectorDrawable createFromXmlInner(
    @NonNull Resources r,
    @NonNull XmlPullParser parser,
    @NonNull AttributeSet attrs,
    @Nullable Resources.Theme theme
)

Create a SeekableAnimatedVectorDrawable from inside an XML document using an optional Theme. Called on a parser positioned at a tag in an XML document, tries to create a Drawable from that tag. Returns null if the tag is not a valid drawable.

draw

Added in 1.0.0
public void draw(@NonNull Canvas canvas)

getAlpha

public @IntRange(from = 0, to = 255) int getAlpha()

getChangingConfigurations

public int getChangingConfigurations()

getColorFilter

public @Nullable ColorFilter getColorFilter()

getConstantState

public @Nullable Drawable.ConstantState getConstantState()

getCurrentPlayTime

Added in 1.0.0
public @IntRange(from = 0) long getCurrentPlayTime()

Returns the milliseconds elapsed since the start of the animation.

For ongoing animations, this method returns the current progress of the animation in terms of play time. For an animation that has not yet been started: if the animation has been seeked to a certain time via setCurrentPlayTime, the seeked play time will be returned; otherwise, this method will return 0.

Returns
@IntRange(from = 0) long

the current position in time of the animation in milliseconds

getIntrinsicHeight

public int getIntrinsicHeight()

getIntrinsicWidth

public int getIntrinsicWidth()

getOpacity

Added in 1.0.0
Deprecated in 1.0.0
public int getOpacity()
Returns
int

The opacity class of the Drawable.

getTotalDuration

Added in 1.0.0
public long getTotalDuration()

Gets the total duration of the animation, accounting for animation sequences, start delay, and repeating. Return DURATION_INFINITE if the duration is infinite.

Returns
long

Total time the animation takes to finish, starting from the time start is called. DURATION_INFINITE if the animation or any of the child animations repeats infinite times.

inflate

public void inflate(
    @NonNull Resources res,
    @NonNull XmlPullParser parser,
    @NonNull AttributeSet attrs
)

inflate

public void inflate(
    @NonNull Resources res,
    @NonNull XmlPullParser parser,
    @NonNull AttributeSet attrs,
    @Nullable Resources.Theme theme
)

isAutoMirrored

public boolean isAutoMirrored()

isPaused

Added in 1.0.0
public boolean isPaused()

Returns whether the animation is currently in a paused state.

Returns
boolean

true if the animation is paused.

isRunning

Added in 1.0.0
public boolean isRunning()

Returns whether the animation is running (has started and not yet ended).

Returns
boolean

true if the animation is running.

isStateful

public boolean isStateful()

mutate

public @NonNull Drawable mutate()

mutate() is not supported. This method simply returns this.

pause

Added in 1.0.0
public void pause()

Pauses a running animation. This method should only be called on the same thread on which the animation was started. If the animation has not yet been started or has since ended, then the call is ignored. Paused animations can be resumed by calling resume.

registerAnimationCallback

Added in 1.0.0
public void registerAnimationCallback(
    @NonNull SeekableAnimatedVectorDrawable.AnimationCallback callback
)

Adds a callback to listen to the animation events.

Parameters
@NonNull SeekableAnimatedVectorDrawable.AnimationCallback callback

Callback to add.

resume

Added in 1.0.0
public void resume()

Resumes a paused animation. The animation resumes from where it left off when it was paused. This method should only be called on the same thread on which the animation was started. Calls will be ignored if this SeekableAnimatedVectorDrawable is not currently paused.

setAlpha

Added in 1.0.0
public void setAlpha(@IntRange(from = 0, to = 255) int alpha)

setAutoMirrored

public void setAutoMirrored(boolean mirrored)

setColorFilter

Added in 1.0.0
public void setColorFilter(@Nullable ColorFilter colorFilter)

setCurrentPlayTime

Added in 1.0.0
public void setCurrentPlayTime(@IntRange(from = 0) long playTime)

Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point.

Parameters
@IntRange(from = 0) long playTime

The time, in milliseconds, to which the animation is advanced or rewound. Unless the animation is reversing, the playtime is considered the time since the end of the start delay of the AnimatorSet in a forward playing direction.

setTint

public void setTint(@ColorInt int tint)

setTintList

public void setTintList(@Nullable ColorStateList tint)

setTintMode

public void setTintMode(@Nullable PorterDuff.Mode tintMode)

setVisible

public boolean setVisible(boolean visible, boolean restart)

start

Added in 1.0.0
public void start()

stop

Added in 1.0.0
public void stop()

unregisterAnimationCallback

Added in 1.0.0
public boolean unregisterAnimationCallback(
    @NonNull SeekableAnimatedVectorDrawable.AnimationCallback callback
)

Removes the specified animation callback.

Parameters
@NonNull SeekableAnimatedVectorDrawable.AnimationCallback callback

Callback to remove.

Returns
boolean

false if callback didn't exist in the call back list, or true if callback has been removed successfully.

Protected methods

onBoundsChange

protected void onBoundsChange(@NonNull Rect bounds)

onLevelChange

protected boolean onLevelChange(int level)

onStateChange

protected boolean onStateChange(@NonNull int[] state)