AnimatedVectorDrawableCompat

public class AnimatedVectorDrawableCompat extends Drawable implements Animatable2Compat


For API 24 and above, this class is delegating to the framework's . For older API version, this class uses android.animation.ObjectAnimator and android.animation.AnimatorSet to animate the properties of a VectorDrawableCompat to create an animated drawable.

AnimatedVectorDrawableCompat are defined in the same XML format as 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 AnimatedVectorDrawableCompat object and use it as a Drawable by the Java API. In order to refer to AnimatedVectorDrawableCompat inside a XML file, you can use app:srcCompat attribute in AppCompat library's ImageButton or ImageView. Note that the animation in AnimatedVectorDrawableCompat now can support the following features:
  • 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.

Summary

Public methods

void
boolean
void

Removes all existing animation callbacks.

static void

Utility function to clear animation callbacks from Drawable, when the drawable is created from XML and referred in Java code, e.g: ImageView.getDrawable().

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

Create a AnimatedVectorDrawableCompat object.

static AnimatedVectorDrawableCompat
createFromXmlInner(
    Context context,
    Resources r,
    XmlPullParser parser,
    AttributeSet attrs,
    Resources.Theme theme
)

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

void
draw(Canvas canvas)
int
int
ColorFilter
Drawable.ConstantState

Note that we don't support constant state when SDK <24.

Drawable
int
int
int
int
int

This method is deprecated.

boolean
getPadding(Rect padding)
int[]
Region
void
inflate(Resources res, XmlPullParser parser, AttributeSet attrs)
void
inflate(
    Resources res,
    XmlPullParser parser,
    AttributeSet attrs,
    Resources.Theme theme
)
boolean
boolean
boolean
void
Drawable

mutate() will be effective only if the getConstantState() is returning non-null.

void

Adds a callback to listen to the animation events.

static void

Utility function to register callback to Drawable, when the drawable is created from XML and referred in Java code, e.g: ImageView.getDrawable().

void
setAlpha(int alpha)
void
setAutoMirrored(boolean mirrored)
void
void
void
setColorFilter(int color, PorterDuff.Mode mode)

This method is deprecated.

void
setFilterBitmap(boolean filter)
void
setHotspot(float x, float y)
void
setHotspotBounds(int left, int top, int right, int bottom)
boolean
setState(int[] stateSet)
void
setTint(int tint)
void
void
boolean
setVisible(boolean visible, boolean restart)
void
void
boolean

Removes the specified animation callback.

static boolean

Utility function to unregister animation callback from Drawable, when the drawable is created from XML and referred in Java code, e.g: ImageView.getDrawable().

Protected methods

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

Inherited methods

From android.graphics.drawable.Drawable
void
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
void
boolean
onLayoutDirectionChanged(int layoutDirection)
boolean
onLevelChange(int level)
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(Resources.Theme t)

canApplyTheme

public boolean canApplyTheme()

clearAnimationCallbacks

Added in 1.2.0-rc01
public void clearAnimationCallbacks()

Removes all existing animation callbacks.

clearAnimationCallbacks

Added in 1.0.0
public static void clearAnimationCallbacks(Drawable dr)

Utility function to clear animation callbacks from Drawable, when the drawable is created from XML and referred in Java code, e.g: ImageView.getDrawable(). From API 24 on, the drawable is treated as an AnimatedVectorDrawable. Otherwise, it is treated as AnimatedVectorDrawableCompat.

clearColorFilter

public void clearColorFilter()

create

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

Create a AnimatedVectorDrawableCompat object.

Parameters
@NonNull Context context

the context for creating the animators.

@DrawableRes int resId

the resource ID for AnimatedVectorDrawableCompat object.

Returns
@Nullable AnimatedVectorDrawableCompat

a new AnimatedVectorDrawableCompat or null if parsing error is found.

createFromXmlInner

Added in 1.0.0
public static AnimatedVectorDrawableCompat createFromXmlInner(
    Context context,
    Resources r,
    XmlPullParser parser,
    AttributeSet attrs,
    Resources.Theme theme
)

Create a AnimatedVectorDrawableCompat 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(Canvas canvas)

getAlpha

public int getAlpha()

getChangingConfigurations

public int getChangingConfigurations()

getColorFilter

public ColorFilter getColorFilter()

getConstantState

public Drawable.ConstantState getConstantState()

Note that we don't support constant state when SDK <24. Make sure you check the return value before using it.

getCurrent

public Drawable getCurrent()

getIntrinsicHeight

public int getIntrinsicHeight()

getIntrinsicWidth

public int getIntrinsicWidth()

getMinimumHeight

public int getMinimumHeight()

getMinimumWidth

public int getMinimumWidth()

getOpacity

Added in 1.0.0
public int getOpacity()

getPadding

public boolean getPadding(Rect padding)

getState

public int[] getState()

getTransparentRegion

public Region getTransparentRegion()

inflate

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

isAutoMirrored

public boolean isAutoMirrored()

isRunning

Added in 1.0.0
public boolean isRunning()

isStateful

public boolean isStateful()

jumpToCurrentState

public void jumpToCurrentState()

mutate

public Drawable mutate()

mutate() will be effective only if the getConstantState() is returning non-null. Otherwise, it just return the current object without modification.

registerAnimationCallback

Added in 1.2.0-rc01
public void registerAnimationCallback(
    @NonNull Animatable2Compat.AnimationCallback callback
)

Adds a callback to listen to the animation events.

Parameters
@NonNull Animatable2Compat.AnimationCallback callback

Callback to add.

registerAnimationCallback

Added in 1.0.0
public static void registerAnimationCallback(
    Drawable dr,
    Animatable2Compat.AnimationCallback callback
)

Utility function to register callback to Drawable, when the drawable is created from XML and referred in Java code, e.g: ImageView.getDrawable(). From API 24 on, the drawable is treated as an AnimatedVectorDrawable. Otherwise, it is treated as AnimatedVectorDrawableCompat.

setAlpha

Added in 1.0.0
public void setAlpha(int alpha)

setAutoMirrored

public void setAutoMirrored(boolean mirrored)

setChangingConfigurations

public void setChangingConfigurations(int configs)

setColorFilter

Added in 1.0.0
public void setColorFilter(ColorFilter colorFilter)

setColorFilter

Added in 1.0.0
public void setColorFilter(int color, PorterDuff.Mode mode)

setFilterBitmap

public void setFilterBitmap(boolean filter)

setHotspot

public void setHotspot(float x, float y)

setHotspotBounds

public void setHotspotBounds(int left, int top, int right, int bottom)

setState

public boolean setState(int[] stateSet)

setTint

public void setTint(int tint)

setTintList

public void setTintList(ColorStateList tint)

setTintMode

public void setTintMode(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.2.0-rc01
public boolean unregisterAnimationCallback(
    @NonNull Animatable2Compat.AnimationCallback callback
)

Removes the specified animation callback.

Parameters
@NonNull Animatable2Compat.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.

unregisterAnimationCallback

Added in 1.0.0
public static boolean unregisterAnimationCallback(
    Drawable dr,
    Animatable2Compat.AnimationCallback callback
)

Utility function to unregister animation callback from Drawable, when the drawable is created from XML and referred in Java code, e.g: ImageView.getDrawable(). From API 24 on, the drawable is treated as an AnimatedVectorDrawable. Otherwise, it is treated as AnimatedVectorDrawableCompat.

Protected methods

onBoundsChange

protected void onBoundsChange(Rect bounds)

onLevelChange

protected boolean onLevelChange(int level)

onStateChange

protected boolean onStateChange(int[] state)