VectorDrawableCompat
public
class
VectorDrawableCompat
extends Drawable
For API 24 and above, this class delegates to the framework's 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>
- Used to define a vector drawable
android:name
- Defines the name of this vector drawable.
android:width
- Used to define the intrinsic width of the drawable.
This support all the dimension units, normally specified with dp.
android:height
- Used to define the intrinsic height the drawable.
This support all the dimension units, normally specified with dp.
android:viewportWidth
- Used to define the width of the viewport space. Viewport is basically
the virtual canvas where the paths are drawn on.
android:viewportHeight
- Used to define the height of the viewport space. Viewport is basically
the virtual canvas where the paths are drawn on.
android:tint
- The color to apply to the drawable as a tint. By default, no tint is applied.
android:tintMode
- The Porter-Duff blending mode for the tint color. Default is src_in.
android:autoMirrored
- Indicates if the drawable needs to be mirrored when its layout direction is
RTL (right-to-left). Default is false.
android:alpha
- The opacity of this drawable. Default is 1.
<group>
- Defines a group of paths or subgroups, plus transformation information.
The transformations are defined in the same coordinates as the viewport.
And the transformations are applied in the order of scale, rotate then translate.
android:name
- Defines the name of the group.
android:rotation
- The degrees of rotation of the group. Default is 0.
android:pivotX
- The X coordinate of the pivot for the scale and rotation of the group.
This is defined in the viewport space. Default is 0.
android:pivotY
- The Y coordinate of the pivot for the scale and rotation of the group.
This is defined in the viewport space. Default is 0.
android:scaleX
- The amount of scale on the X Coordinate. Default is 1.
android:scaleY
- The amount of scale on the Y coordinate. Default is 1.
android:translateX
- The amount of translation on the X coordinate.
This is defined in the viewport space. Default is 0.
android:translateY
- The amount of translation on the Y coordinate.
This is defined in the viewport space. Default is 0.
<path>
- Defines paths to be drawn.
android:name
- Defines the name of the path.
android:pathData
- Defines path data using exactly same format as "d" attribute
in the SVG's path data. This is defined in the viewport space.
android:fillColor
- Specifies the color used to fill the path.
If this property is animated, any value set by the animation will override the original value.
No path fill is drawn if this property is not specified.
android:strokeColor
- Specifies the color used to draw the path outline.
If this property is animated, any value set by the animation will override the original value.
No path outline is drawn if this property is not specified.
android:strokeWidth
- The width a path stroke. Default is 0.
android:strokeAlpha
- The opacity of a path stroke. Default is 1.
android:fillAlpha
- The opacity to fill the path with. Default is 1.
android:trimPathStart
- The fraction of the path to trim from the start, in the range from 0 to 1. Default is 0.
android:trimPathEnd
- The fraction of the path to trim from the end, in the range from 0 to 1. Default is 1.
android:trimPathOffset
- Shift trim region (allows showed region to include the start and end), in the range
from 0 to 1. Default is 0.
android:strokeLineCap
- Sets the linecap for a stroked path: butt, round, square. Default is butt.
android:strokeLineJoin
- Sets the lineJoin for a stroked path: miter,round,bevel. Default is miter.
android:strokeMiterLimit
- Sets the Miter limit for a stroked path. Default is 4.
android:fillType
- Sets the fillType for a path. The types can be either "evenOdd" or "nonZero". They behave the
same as SVG's "fill-rule" properties. Default is nonZero. For more details, see
FillRuleProperty
<clip-path>
- Defines path to be the current clip. Note that the clip path only apply to
the current group and its children.
android:name
- Defines the name of the clip path.
android:pathData
- Defines clip path using the same format as "d" attribute
in the SVG's path data.
Gradient support
We support 3 types of gradients:
LinearGradient
,
RadialGradient
, or
SweepGradient
.
And we support all of 3 types of tile modes
Shader.TileMode
:
CLAMP, REPEAT, MIRROR.
Note that different attributes are relevant for different types of gradient.
LinearGradient |
RadialGradient |
SweepGradient |
startColor |
startColor |
startColor |
centerColor |
centerColor |
centerColor |
endColor |
endColor |
endColor |
type |
type |
type |
tileMode |
tileMode |
tileMode |
startX |
centerX |
centerX |
startY |
centerY |
centerY |
endX |
gradientRadius |
|
endY |
|
|
Also note that if any color item is defined, then
startColor, centerColor and endColor will be ignored.
Note that theme attributes in XML file are supported through
inflate(Resources, XmlPullParser, AttributeSet, Theme)
.
Summary
Public methods |
void
|
applyTheme(Resources.Theme t)
|
boolean
|
canApplyTheme()
|
void
|
clearColorFilter()
|
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)
|
int
|
getAlpha()
|
int
|
getChangingConfigurations()
|
ColorFilter
|
getColorFilter()
|
Drawable.ConstantState
|
getConstantState()
|
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)
|
void
|
invalidateSelf()
|
boolean
|
isAutoMirrored()
|
boolean
|
isStateful()
|
void
|
jumpToCurrentState()
|
Drawable
|
mutate()
|
void
|
scheduleSelf(Runnable what, long when)
|
void
|
setAlpha(int alpha)
|
void
|
setAutoMirrored(boolean mirrored)
|
void
|
setChangingConfigurations(int configs)
|
void
|
setColorFilter(ColorFilter colorFilter)
|
void
|
setColorFilter(int color, PorterDuff.Mode mode)
|
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
|
unscheduleSelf(Runnable what)
|
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 ()
clearColorFilter
public void clearColorFilter ()
create
public static VectorDrawableCompat create (Resources res,
int resId,
Resources.Theme theme)
Create a VectorDrawableCompat object.
Parameters |
res |
Resources : the resources. |
resId |
int : the resource ID for VectorDrawableCompat object. |
theme |
Resources.Theme : the theme of this vector drawable, it can be null. |
createFromXmlInner
public 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
. 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 |
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 ()
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 |
|
invalidateSelf
public void invalidateSelf ()
isAutoMirrored
public boolean isAutoMirrored ()
isStateful
public boolean isStateful ()
jumpToCurrentState
public void jumpToCurrentState ()
scheduleSelf
public void scheduleSelf (Runnable what,
long when)
Parameters |
what |
Runnable |
when |
long |
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 (ColorFilter colorFilter)
Parameters |
colorFilter |
ColorFilter |
setColorFilter
public void setColorFilter (int color,
PorterDuff.Mode mode)
Parameters |
color |
int |
mode |
PorterDuff.Mode |
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 |
unscheduleSelf
public void unscheduleSelf (Runnable what)
Protected methods
onBoundsChange
protected void onBoundsChange (Rect bounds)
onLevelChange
protected boolean onLevelChange (int level)
onStateChange
protected boolean onStateChange (int[] stateSet)