AnimatedVectorDrawableCompat
public
class
AnimatedVectorDrawableCompat
extends Drawable
implements
Animatable2Compat
For API 24 and above, this class is delegating to the framework's AnimatedVectorDrawable
.
For older API version, this class uses ObjectAnimator
and
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 |
<vector> |
alpha |
<group> |
rotation |
pivotX |
pivotY |
scaleX |
scaleY |
translateX |
translateY |
<path> |
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
|
applyTheme(Resources.Theme t)
|
boolean
|
canApplyTheme()
|
void
|
clearAnimationCallbacks()
Removes all existing animation callbacks.
|
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().
|
void
|
clearColorFilter()
|
static
AnimatedVectorDrawableCompat
|
create(Context context, 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
Resources.Theme .
|
void
|
draw(Canvas canvas)
|
int
|
getAlpha()
|
int
|
getChangingConfigurations()
|
ColorFilter
|
getColorFilter()
|
Drawable.ConstantState
|
getConstantState()
Note that we don't support constant state when SDK < 24.
|
Drawable
|
getCurrent()
|
int
|
getIntrinsicHeight()
|
int
|
getIntrinsicWidth()
|
int
|
getMinimumHeight()
|
int
|
getMinimumWidth()
|
int
|
getOpacity()
|
boolean
|
getPadding(Rect padding)
|
int[]
|
getState()
|
Region
|
getTransparentRegion()
|
void
|
inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)
|
void
|
inflate(Resources res, XmlPullParser parser, AttributeSet attrs)
|
boolean
|
isAutoMirrored()
|
boolean
|
isRunning()
|
boolean
|
isStateful()
|
void
|
jumpToCurrentState()
|
Drawable
|
mutate()
mutate() will be effective only if the getConstantState() is returning non-null.
|
void
|
registerAnimationCallback(Animatable2Compat.AnimationCallback callback)
Adds a callback to listen to the animation events.
|
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().
|
void
|
setAlpha(int alpha)
|
void
|
setAutoMirrored(boolean mirrored)
|
void
|
setChangingConfigurations(int configs)
|
void
|
setColorFilter(int color, PorterDuff.Mode mode)
|
void
|
setColorFilter(ColorFilter colorFilter)
|
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
|
setTintList(ColorStateList tint)
|
void
|
setTintMode(PorterDuff.Mode tintMode)
|
boolean
|
setVisible(boolean visible, boolean restart)
|
void
|
start()
|
void
|
stop()
|
boolean
|
unregisterAnimationCallback(Animatable2Compat.AnimationCallback callback)
Removes the specified animation callback.
|
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().
|
Inherited methods |
From class
android.graphics.drawable.Drawable
void
|
applyTheme(Resources.Theme arg0)
|
boolean
|
canApplyTheme()
|
void
|
clearColorFilter()
|
final
Rect
|
copyBounds()
|
final
void
|
copyBounds(Rect arg0)
|
static
Drawable
|
createFromPath(String arg0)
|
static
Drawable
|
createFromResourceStream(Resources arg0, TypedValue arg1, InputStream arg2, String arg3, BitmapFactory.Options arg4)
|
static
Drawable
|
createFromResourceStream(Resources arg0, TypedValue arg1, InputStream arg2, String arg3)
|
static
Drawable
|
createFromStream(InputStream arg0, String arg1)
|
static
Drawable
|
createFromXml(Resources arg0, XmlPullParser arg1)
|
static
Drawable
|
createFromXml(Resources arg0, XmlPullParser arg1, Resources.Theme arg2)
|
static
Drawable
|
createFromXmlInner(Resources arg0, XmlPullParser arg1, AttributeSet arg2, Resources.Theme arg3)
|
static
Drawable
|
createFromXmlInner(Resources arg0, XmlPullParser arg1, AttributeSet arg2)
|
abstract
void
|
draw(Canvas arg0)
|
int
|
getAlpha()
|
final
Rect
|
getBounds()
|
Drawable.Callback
|
getCallback()
|
int
|
getChangingConfigurations()
|
ColorFilter
|
getColorFilter()
|
Drawable.ConstantState
|
getConstantState()
|
Drawable
|
getCurrent()
|
Rect
|
getDirtyBounds()
|
void
|
getHotspotBounds(Rect arg0)
|
int
|
getIntrinsicHeight()
|
int
|
getIntrinsicWidth()
|
int
|
getLayoutDirection()
|
final
int
|
getLevel()
|
int
|
getMinimumHeight()
|
int
|
getMinimumWidth()
|
abstract
int
|
getOpacity()
|
Insets
|
getOpticalInsets()
|
void
|
getOutline(Outline arg0)
|
boolean
|
getPadding(Rect arg0)
|
int[]
|
getState()
|
Region
|
getTransparentRegion()
|
void
|
inflate(Resources arg0, XmlPullParser arg1, AttributeSet arg2, Resources.Theme arg3)
|
void
|
inflate(Resources arg0, XmlPullParser arg1, AttributeSet arg2)
|
void
|
invalidateSelf()
|
boolean
|
isAutoMirrored()
|
boolean
|
isFilterBitmap()
|
boolean
|
isProjected()
|
boolean
|
isStateful()
|
final
boolean
|
isVisible()
|
void
|
jumpToCurrentState()
|
Drawable
|
mutate()
|
void
|
onBoundsChange(Rect arg0)
|
boolean
|
onLayoutDirectionChanged(int arg0)
|
boolean
|
onLevelChange(int arg0)
|
boolean
|
onStateChange(int[] arg0)
|
static
int
|
resolveOpacity(int arg0, int arg1)
|
void
|
scheduleSelf(Runnable arg0, long arg1)
|
abstract
void
|
setAlpha(int arg0)
|
void
|
setAutoMirrored(boolean arg0)
|
void
|
setBounds(int arg0, int arg1, int arg2, int arg3)
|
void
|
setBounds(Rect arg0)
|
final
void
|
setCallback(Drawable.Callback arg0)
|
void
|
setChangingConfigurations(int arg0)
|
void
|
setColorFilter(int arg0, PorterDuff.Mode arg1)
|
abstract
void
|
setColorFilter(ColorFilter arg0)
|
void
|
setDither(boolean arg0)
|
void
|
setFilterBitmap(boolean arg0)
|
void
|
setHotspot(float arg0, float arg1)
|
void
|
setHotspotBounds(int arg0, int arg1, int arg2, int arg3)
|
final
boolean
|
setLayoutDirection(int arg0)
|
final
boolean
|
setLevel(int arg0)
|
boolean
|
setState(int[] arg0)
|
void
|
setTint(int arg0)
|
void
|
setTintBlendMode(BlendMode arg0)
|
void
|
setTintList(ColorStateList arg0)
|
void
|
setTintMode(PorterDuff.Mode arg0)
|
boolean
|
setVisible(boolean arg0, boolean arg1)
|
void
|
unscheduleSelf(Runnable arg0)
|
|
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
|
|
Public methods
applyTheme
public void applyTheme (Resources.Theme t)
Parameters |
t |
Resources.Theme |
canApplyTheme
public boolean canApplyTheme ()
clearAnimationCallbacks
public void clearAnimationCallbacks ()
Removes all existing animation callbacks.
clearAnimationCallbacks
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
public static AnimatedVectorDrawableCompat create (Context context,
int resId)
Create a AnimatedVectorDrawableCompat object.
Parameters |
context |
Context : the context for creating the animators. |
resId |
int : the resource ID for AnimatedVectorDrawableCompat object. |
createFromXmlInner
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
Resources.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.
Parameters |
context |
Context |
r |
Resources |
parser |
XmlPullParser |
attrs |
AttributeSet |
theme |
Resources.Theme |
Throws |
XmlPullParserException |
|
IOException |
|
draw
public void draw (Canvas canvas)
getAlpha
public int getAlpha ()
getChangingConfigurations
public int getChangingConfigurations ()
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
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)
Parameters |
res |
Resources |
parser |
XmlPullParser |
attrs |
AttributeSet |
theme |
Resources.Theme |
Throws |
XmlPullParserException |
|
IOException |
|
inflate
public void inflate (Resources res,
XmlPullParser parser,
AttributeSet attrs)
Parameters |
res |
Resources |
parser |
XmlPullParser |
attrs |
AttributeSet |
Throws |
XmlPullParserException |
|
IOException |
|
isAutoMirrored
public boolean isAutoMirrored ()
isRunning
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
public void registerAnimationCallback (Animatable2Compat.AnimationCallback callback)
Adds a callback to listen to the animation events.
Parameters |
callback |
Animatable2Compat.AnimationCallback : Callback to add.
|
registerAnimationCallback
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.
Parameters |
dr |
Drawable |
callback |
Animatable2Compat.AnimationCallback |
setAlpha
public void setAlpha (int alpha)
setAutoMirrored
public void setAutoMirrored (boolean mirrored)
Parameters |
mirrored |
boolean |
setChangingConfigurations
public void setChangingConfigurations (int configs)
setColorFilter
public void setColorFilter (int color,
PorterDuff.Mode mode)
Parameters |
color |
int |
mode |
PorterDuff.Mode |
setColorFilter
public void setColorFilter (ColorFilter colorFilter)
Parameters |
colorFilter |
ColorFilter |
setFilterBitmap
public void setFilterBitmap (boolean filter)
Parameters |
filter |
boolean |
setHotspot
public void setHotspot (float x,
float y)
Parameters |
x |
float |
y |
float |
setHotspotBounds
public void setHotspotBounds (int left,
int top,
int right,
int bottom)
Parameters |
left |
int |
top |
int |
right |
int |
bottom |
int |
setState
public boolean setState (int[] stateSet)
setTint
public void setTint (int tint)
setTintList
public void setTintList (ColorStateList tint)
Parameters |
tint |
ColorStateList |
setTintMode
public void setTintMode (PorterDuff.Mode tintMode)
Parameters |
tintMode |
PorterDuff.Mode |
setVisible
public boolean setVisible (boolean visible,
boolean restart)
Parameters |
visible |
boolean |
restart |
boolean |
start
public void start ()
unregisterAnimationCallback
public boolean unregisterAnimationCallback (Animatable2Compat.AnimationCallback callback)
Removes the specified animation callback.
Parameters |
callback |
Animatable2Compat.AnimationCallback : 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
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.
Parameters |
dr |
Drawable |
callback |
Animatable2Compat.AnimationCallback |
Protected methods
onBoundsChange
protected void onBoundsChange (Rect bounds)
onLevelChange
protected boolean onLevelChange (int level)
onStateChange
protected boolean onStateChange (int[] state)