public class VectorDrawableCompat extends Drawable
VectorDrawable
.
For older API version, this class lets you create a drawable based on an XML vector graphic.
You can always create a VectorDrawableCompat object and use it as a Drawable by the Java API.
In order to refer to VectorDrawableCompat inside a XML file, you can use app:srcCompat attribute
in AppCompat library's ImageButton or ImageView.
Note: To optimize for the re-drawing performance, one bitmap cache is created
for each VectorDrawableCompat. Therefore, referring to the same VectorDrawableCompat means
sharing the same bitmap cache. If these references don't agree upon on the same size, the bitmap
will be recreated and redrawn every time size is changed. In other words, if a VectorDrawable is
used for different sizes, it is more efficient to create multiple VectorDrawables, one for each
size.
VectorDrawableCompat can be defined in an XML file with the <vector>
element.
The VectorDrawableCompat has the following elements:
<vector>
android:name
android:width
android:height
android:viewportWidth
android:viewportHeight
android:tint
android:tintMode
android:autoMirrored
android:alpha
<group>
android:name
android:rotation
android:pivotX
android:pivotY
android:scaleX
android:scaleY
android:translateX
android:translateY
<path>
android:name
android:pathData
android:fillColor
android:strokeColor
android:strokeWidth
android:strokeAlpha
android:fillAlpha
android:trimPathStart
android:trimPathEnd
android:trimPathOffset
android:strokeLineCap
android:strokeLineJoin
android:strokeMiterLimit
<clip-path>
android:name
android:pathData
Drawable.Callback, Drawable.ConstantState
Modifier and Type | Method and Description |
---|---|
void |
applyTheme(Resources.Theme t)
Applies the specified theme to this Drawable and its children.
|
boolean |
canApplyTheme() |
void |
clearColorFilter()
Removes the color filter for this drawable.
|
static VectorDrawableCompat |
create(Resources res,
int resId,
Resources.Theme theme)
Create a VectorDrawableCompat object.
|
static VectorDrawableCompat |
createFromXmlInner(Resources r,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)
Create a VectorDrawableCompat from inside an XML document using an optional
Resources.Theme . |
void |
draw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such
as alpha (set via setAlpha) and color filter (set via setColorFilter).
|
int |
getAlpha()
Gets the current alpha value for the drawable. 0 means fully transparent,
255 means fully opaque.
|
int |
getChangingConfigurations()
Return a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
|
ColorFilter |
getColorFilter()
Returns the current color filter, or
null if none set. |
Drawable.ConstantState |
getConstantState()
Return a
Drawable.ConstantState instance that holds the shared state of this Drawable. |
Drawable |
getCurrent() |
int |
getIntrinsicHeight()
Returns the drawable's intrinsic height.
|
int |
getIntrinsicWidth()
Returns the drawable's intrinsic width.
|
int |
getMinimumHeight()
Returns the minimum height suggested by this Drawable.
|
int |
getMinimumWidth()
Returns the minimum width suggested by this Drawable.
|
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
boolean |
getPadding(Rect padding)
Return in padding the insets suggested by this Drawable for placing
content inside the drawable's bounds.
|
float |
getPixelSize()
The size of a pixel when scaled from the intrinsic dimension to the viewport dimension.
|
int[] |
getState()
Describes the current state, as a union of primitve states, such as
android.R.attr#state_focused ,
android.R.attr#state_selected , etc. |
Region |
getTransparentRegion()
Returns a Region representing the part of the Drawable that is completely
transparent.
|
void |
inflate(Resources res,
XmlPullParser parser,
AttributeSet attrs)
Inflate this Drawable from an XML resource.
|
void |
inflate(Resources res,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme.
|
void |
invalidateSelf()
Use the current
Drawable.Callback implementation to have this Drawable
redrawn. |
boolean |
isAutoMirrored()
Tells if this Drawable will be automatically mirrored when its layout direction is RTL
right-to-left.
|
boolean |
isStateful()
Indicates whether this drawable will change its appearance based on
state.
|
void |
jumpToCurrentState()
If this Drawable does transition animations between states, ask that
it immediately jump to the current state and skip any active animations.
|
Drawable |
mutate()
Make this drawable mutable.
|
protected void |
onBoundsChange(Rect bounds)
Override this in your subclass to change appearance if you vary based on
the bounds.
|
protected boolean |
onLevelChange(int level)
Override this in your subclass to change appearance if you vary based
on level.
|
protected boolean |
onStateChange(int[] stateSet)
Override this in your subclass to change appearance if you recognize the
specified state.
|
void |
scheduleSelf(Runnable what,
long when)
Use the current
Drawable.Callback implementation to have this Drawable
scheduled. |
void |
setAlpha(int alpha)
Specify an alpha value for the drawable. 0 means fully transparent, and
255 means fully opaque.
|
void |
setAutoMirrored(boolean mirrored)
Set whether this Drawable is automatically mirrored when its layout direction is RTL
(right-to left).
|
void |
setChangingConfigurations(int configs)
Set a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
|
void |
setColorFilter(ColorFilter colorFilter)
Specify an optional color filter for the drawable.
|
void |
setColorFilter(int color,
PorterDuff.Mode mode)
Specify a color and Porter-Duff mode to be the color filter for this
drawable.
|
void |
setFilterBitmap(boolean filter)
Set to true to have the drawable filter its bitmaps with bilinear
sampling when they are scaled or rotated.
|
void |
setHotspot(float x,
float y)
Specifies the hotspot's location within the drawable.
|
void |
setHotspotBounds(int left,
int top,
int right,
int bottom)
Sets the bounds to which the hotspot is constrained, if they should be
different from the drawable bounds.
|
boolean |
setState(int[] stateSet)
Specify a set of states for the drawable.
|
void |
setTint(int tint)
Specifies tint color for this drawable.
|
void |
setTintList(ColorStateList tint)
Specifies tint color for this drawable as a color state list.
|
void |
setTintMode(PorterDuff.Mode tintMode)
Specifies a tint blending mode for this drawable.
|
boolean |
setVisible(boolean visible,
boolean restart)
Set whether this Drawable is visible.
|
void |
unscheduleSelf(Runnable what)
Use the current
Drawable.Callback implementation to have this Drawable
unscheduled. |
clearMutated, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, getBounds, getCallback, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getOpticalInsets, getOutline, isFilterBitmap, isProjected, isVisible, onLayoutDirectionChanged, parseTintMode, resolveOpacity, setBounds, setBounds, setCallback, setDither, setLayoutDirection, setLevel, setXfermode
public Drawable mutate()
Drawable
mutate
in class Drawable
Drawable.ConstantState
,
Drawable.getConstantState()
public Drawable.ConstantState getConstantState()
Drawable
Drawable.ConstantState
instance that holds the shared state of this Drawable.getConstantState
in class Drawable
Drawable.ConstantState
,
Drawable.mutate()
public void draw(Canvas canvas)
Drawable
public int getAlpha()
Drawable
public void setAlpha(int alpha)
Drawable
public void setColorFilter(ColorFilter colorFilter)
Drawable
If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.
Pass null
to remove any existing color filter.
Note: Setting a non-null
color
filter disables tint
.
setColorFilter
in class Drawable
colorFilter
- The color filter to apply, or null
to remove the
existing color filterpublic void setTint(int tint)
Drawable
A Drawable's drawing content will be blended together with its tint
before it is drawn to the screen. This functions similarly to
Drawable.setColorFilter(int, PorterDuff.Mode)
.
To clear the tint, pass null
to
Drawable.setTintList(ColorStateList)
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTint
in interface TintAwareDrawable
setTint
in class Drawable
tint
- Color to use for tinting this drawableDrawable.setTintList(ColorStateList)
,
Drawable.setTintMode(PorterDuff.Mode)
public void setTintList(ColorStateList tint)
Drawable
A Drawable's drawing content will be blended together with its tint
before it is drawn to the screen. This functions similarly to
Drawable.setColorFilter(int, PorterDuff.Mode)
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintList
in interface TintAwareDrawable
setTintList
in class Drawable
tint
- Color state list to use for tinting this drawable, or
null
to clear the tintDrawable.setTint(int)
,
Drawable.setTintMode(PorterDuff.Mode)
public void setTintMode(PorterDuff.Mode tintMode)
Drawable
Defines how this drawable's tint color should be blended into the drawable
before it is drawn to screen. Default tint mode is PorterDuff.Mode#SRC_IN
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintMode
in interface TintAwareDrawable
setTintMode
in class Drawable
tintMode
- A Porter-Duff blending modeDrawable.setTint(int)
,
Drawable.setTintList(ColorStateList)
public boolean isStateful()
Drawable
isStateful
in class Drawable
Drawable.setState(int[])
protected boolean onStateChange(int[] stateSet)
Drawable
onStateChange
in class Drawable
public int getOpacity()
Drawable
PixelFormat
:
PixelFormat.UNKNOWN
,
PixelFormat.TRANSLUCENT
,
PixelFormat.TRANSPARENT
, or
PixelFormat.OPAQUE
.
An OPAQUE drawable is one that draws all all content within its bounds, completely covering anything behind the drawable. A TRANSPARENT drawable is one that draws nothing within its bounds, allowing everything behind it to show through. A TRANSLUCENT drawable is a drawable in any other state, where the drawable will draw some, but not all, of the content within its bounds and at least some content behind the drawable will be visible. If the visibility of the drawable's contents cannot be determined, the safest/best return value is TRANSLUCENT.
Generally a Drawable should be as conservative as possible with the
value it returns. For example, if it contains multiple child drawables
and only shows one of them at a time, if only one of the children is
TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be
returned. You can use the method Drawable.resolveOpacity(int, int)
to perform a
standard reduction of two opacities to the appropriate single output.
Note that the returned value does not necessarily take into account a
custom alpha or color filter that has been applied by the client through
the Drawable.setAlpha(int)
or Drawable.setColorFilter(android.graphics.ColorFilter)
methods. Some subclasses,
such as BitmapDrawable
, ColorDrawable
, and GradientDrawable
,
do account for the value of Drawable.setAlpha(int)
, but the general behavior is dependent
upon the implementation of the subclass.
getOpacity
in class Drawable
PixelFormat
public int getIntrinsicWidth()
Drawable
Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.
getIntrinsicWidth
in class Drawable
public int getIntrinsicHeight()
Drawable
Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.
getIntrinsicHeight
in class Drawable
public boolean canApplyTheme()
canApplyTheme
in class Drawable
public boolean isAutoMirrored()
Drawable
LayoutDirection
.isAutoMirrored
in class Drawable
public void setAutoMirrored(boolean mirrored)
Drawable
LayoutDirection
.setAutoMirrored
in class Drawable
mirrored
- Set to true if the Drawable should be mirrored, false if not.public float getPixelSize()
@Nullable public static VectorDrawableCompat create(@NonNull Resources res, @DrawableRes int resId, @Nullable Resources.Theme theme)
res
- the resources.resId
- the resource ID for VectorDrawableCompat object.theme
- the theme of this vector drawable, it can be null.public static VectorDrawableCompat createFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
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.XmlPullParserException
IOException
public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
XmlPullParserException
IOException
#inflate(Resources, XmlPullParser, AttributeSet, Theme)
public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
res
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
protected void onBoundsChange(Rect bounds)
Drawable
public int getChangingConfigurations()
Drawable
Drawable.setChangingConfigurations(int)
or 0 by default. Subclasses
may extend this to or in the changing configurations of any other
drawables they hold.getChangingConfigurations
in class Drawable
ActivityInfo
.ActivityInfo
public void invalidateSelf()
Drawable
Drawable.Callback
implementation to have this Drawable
redrawn. Does nothing if there is no Callback attached to the
Drawable.public void scheduleSelf(Runnable what, long when)
Drawable
Drawable.Callback
implementation to have this Drawable
scheduled. Does nothing if there is no Callback attached to the
Drawable.scheduleSelf
in class Drawable
what
- The action being scheduled.when
- The time (in milliseconds) to run.Drawable.Callback.scheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable, long)
public boolean setVisible(boolean visible, boolean restart)
Drawable
setVisible
in class Drawable
visible
- Set to true if visible, false if not.restart
- You can supply true here to force the drawable to behave
as if it has just become visible, even if it had last
been set visible. Used for example to force animations
to restart.public void unscheduleSelf(Runnable what)
Drawable
Drawable.Callback
implementation to have this Drawable
unscheduled. Does nothing if there is no Callback attached to the
Drawable.unscheduleSelf
in class Drawable
what
- The runnable that you no longer want called.Drawable.Callback.unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable)
public void setColorFilter(int color, PorterDuff.Mode mode)
Drawable
Convenience for Drawable.setColorFilter(ColorFilter)
which constructs a
PorterDuffColorFilter
.
Note: Setting a color filter disables
tint
.
setColorFilter
in class Drawable
public ColorFilter getColorFilter()
Drawable
null
if none set.getColorFilter
in class Drawable
null
if none setprotected boolean onLevelChange(int level)
Drawable
onLevelChange
in class Drawable
public void setHotspot(float x, float y)
Drawable
setHotspot
in class Drawable
x
- The X coordinate of the center of the hotspoty
- The Y coordinate of the center of the hotspotpublic void setHotspotBounds(int left, int top, int right, int bottom)
Drawable
setHotspotBounds
in class Drawable
left
- position in pixels of the left boundtop
- position in pixels of the top boundright
- position in pixels of the right boundbottom
- position in pixels of the bottom boundDrawable.getHotspotBounds(android.graphics.Rect)
public void setFilterBitmap(boolean filter)
Drawable
This can improve appearance when bitmaps are rotated. If the drawable does not use bitmaps, this call is ignored.
setFilterBitmap
in class Drawable
Drawable.isFilterBitmap()
,
Paint.setFilterBitmap(boolean);
public void jumpToCurrentState()
Drawable
jumpToCurrentState
in class Drawable
public void applyTheme(Resources.Theme t)
Drawable
applyTheme
in class Drawable
t
- the theme to applypublic void clearColorFilter()
Drawable
clearColorFilter
in class Drawable
public Drawable getCurrent()
getCurrent
in class Drawable
StateListDrawable
and LevelListDrawable
this will be the child drawable
currently in use.public int getMinimumWidth()
Drawable
getMinimumWidth
in class Drawable
public int getMinimumHeight()
Drawable
getMinimumHeight
in class Drawable
public boolean getPadding(Rect padding)
Drawable
getPadding
in class Drawable
public int[] getState()
Drawable
android.R.attr#state_focused
,
android.R.attr#state_selected
, etc.
Some drawables may modify their imagery based on the selected state.public Region getTransparentRegion()
Drawable
getTransparentRegion
in class Drawable
public void setChangingConfigurations(int configs)
Drawable
setChangingConfigurations
in class Drawable
configs
- A mask of the changing configuration parameters, as
defined by ActivityInfo
.ActivityInfo
public boolean setState(int[] stateSet)
Drawable
android.R.attr#state_focused
,
android.R.attr#state_pressed
].
If the new state you are supplying causes the appearance of the
Drawable to change, then it is responsible for calling
Drawable.invalidateSelf()
in order to have itself redrawn, and
true will be returned from this function.
Note: The Drawable holds a reference on to stateSet until a new state array is given to it, so you must not modify this array during that time.