Most visited

Recently visited

Added in API level 21

VectorDrawable

public class VectorDrawable
extends Drawable

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.VectorDrawable


这使您可以基于XML矢量图形创建绘图。

Note: To optimize for the re-drawing performance, one bitmap cache is created for each VectorDrawable. Therefore, referring to the same VectorDrawable 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.

VectorDrawable can be defined in an XML file with the <vector> element.

The vector drawable 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. The default value is src_in.
android:autoMirrored
Indicates if the drawable needs to be mirrored when its layout direction is RTL (right-to-left).
android:alpha
The opacity of this drawable.
<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.
android:pivotX
The X coordinate of the pivot for the scale and rotation of the group. This is defined in the viewport space.
android:pivotY
The Y coordinate of the pivot for the scale and rotation of the group. This is defined in the viewport space.
android:scaleX
The amount of scale on the X Coordinate.
android:scaleY
The amount of scale on the Y coordinate.
android:translateX
The amount of translation on the X coordinate. This is defined in the viewport space.
android:translateY
The amount of translation on the Y coordinate. This is defined in the viewport space.
<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. May be a color or, for SDK 24+, a color state list or a gradient color. 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. May be a color or, for SDK 24+, a color state list or a gradient color. 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.
android:strokeAlpha
The opacity of a path stroke.
android:fillAlpha
The opacity to fill the path with.
android:trimPathStart
The fraction of the path to trim from the start, in the range from 0 to 1.
android:trimPathEnd
The fraction of the path to trim from the end, in the range from 0 to 1.
android:trimPathOffset
Shift trim region (allows showed region to include the start and end), in the range from 0 to 1.
android:strokeLineCap
Sets the linecap for a stroked path: butt, round, square.
android:strokeLineJoin
Sets the lineJoin for a stroked path: miter,round,bevel.
android:strokeMiterLimit
Sets the Miter limit for a stroked path.
android:fillType
Sets the fillType for a path. It is the same as SVG's "fill-rule" properties. For more details, see https://www.w3.org/TR/SVG/painting.html#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.
  • Here is a simple VectorDrawable in this vectordrawable.xml file.
     <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:height="64dp"
         android:width="64dp"
         android:viewportHeight="600"
         android:viewportWidth="600" >
         <group
             android:name="rotationGroup"
             android:pivotX="300.0"
             android:pivotY="300.0"
             android:rotation="45.0" >
             <path
                 android:name="v"
                 android:fillColor="#000000"
                 android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
         </group>
     </vector>
     
  • Summary

    Public constructors

    VectorDrawable()

    Public methods

    void applyTheme(Resources.Theme t)

    将指定的主题应用于此Drawable及其子项。

    boolean canApplyTheme()
    void draw(Canvas canvas)

    在其边界(通过setBounds设置)中绘制,考虑可选效果,如alpha(通过setAlpha设置)和color filter(通过setColorFilter设置)。

    int getAlpha()

    获取drawable的当前alpha值。

    int getChangingConfigurations()

    返回此drawable可能更改的配置参数的掩码,要求重新创建它。

    ColorFilter getColorFilter()

    返回当前的颜色过滤器,如果没有设置,则返回 null

    Drawable.ConstantState getConstantState()

    返回保存此Drawable的共享状态的 Drawable.ConstantState实例。

    int getIntrinsicHeight()

    返回drawable的内在高度。

    int getIntrinsicWidth()

    返回drawable的内部宽度。

    int getOpacity()

    返回此Drawable的不透明度/透明度。

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

    从可选的由主题设计的XML资源中扩展此Drawable。

    boolean isAutoMirrored()

    当它的布局方向是从右到左的RTL时,告诉它是否自动镜像。

    boolean isStateful()

    指示此drawable是否会根据状态更改其外观。

    Drawable mutate()

    使这个drawable可变。

    void setAlpha(int alpha)

    为drawable指定一个alpha值。

    void setAutoMirrored(boolean mirrored)

    设置当其布局方向为RTL(从右到左)时,此Drawable是否自动镜像。

    void setColorFilter(ColorFilter colorFilter)

    为绘图指定一个可选的颜色过滤器。

    void setTintList(ColorStateList tint)

    指定该drawable的色彩颜色作为颜色状态列表。

    void setTintMode(PorterDuff.Mode tintMode)

    指定该drawable的色调混合模式。

    Protected methods

    boolean onStateChange(int[] stateSet)

    如果您认识到指定的状态,请在您的子类中覆盖此内容以更改外观。

    Inherited methods

    From class android.graphics.drawable.Drawable
    From class java.lang.Object

    Public constructors

    VectorDrawable

    Added in API level 21
    VectorDrawable ()

    Public methods

    applyTheme

    Added in API level 21
    void applyTheme (Resources.Theme t)

    将指定的主题应用于此Drawable及其子项。

    Parameters
    t Resources.Theme: the theme to apply

    canApplyTheme

    Added in API level 21
    boolean canApplyTheme ()

    Returns
    boolean

    draw

    Added in API level 21
    void draw (Canvas canvas)

    在其边界(通过setBounds设置)中绘制,考虑可选效果,如alpha(通过setAlpha设置)和color filter(通过setColorFilter设置)。

    Parameters
    canvas Canvas: The canvas to draw into

    getAlpha

    Added in API level 21
    int getAlpha ()

    获取drawable的当前alpha值。 0表示完全透明,255表示完全不透明。 该方法由Drawable子类实现,返回的值特定于该类如何处理alpha。 如果该类未覆盖此方法以返回特定于其使用alpha的值,则默认返回值为255。

    Returns
    int

    getChangingConfigurations

    Added in API level 21
    int getChangingConfigurations ()

    返回此drawable可能更改的配置参数的掩码,要求重新创建它。 缺省实现返回默认情况下通过setChangingConfigurations(int)或0提供的值。 子类可以将其扩展到它们所拥有的任何其他drawable的变化配置中或其中。

    Returns
    int Returns a mask of the changing configuration parameters, as defined by ActivityInfo.

    getColorFilter

    Added in API level 21
    ColorFilter getColorFilter ()

    返回当前的颜色过滤器,如果没有设置,则返回 null

    Returns
    ColorFilter the current color filter, or null if none set

    getConstantState

    Added in API level 21
    Drawable.ConstantState getConstantState ()

    返回保存此Drawable的共享状态的 Drawable.ConstantState实例。

    Returns
    Drawable.ConstantState The ConstantState associated to that Drawable.

    getIntrinsicHeight

    Added in API level 21
    int getIntrinsicHeight ()

    返回drawable的内在高度。

    固有高度是可拉伸的布局高度,包括任何固有的填充。 如果drawable没有固有的高度,例如纯色,则此方法返回-1。

    Returns
    int the intrinsic height, or -1 if no intrinsic height

    getIntrinsicWidth

    Added in API level 21
    int getIntrinsicWidth ()

    返回drawable的内部宽度。

    固有宽度是可展开的布局的宽度,包括任何固有的填充。 如果drawable没有固有的宽度,例如纯色,则此方法返回-1。

    Returns
    int the intrinsic width, or -1 if no intrinsic width

    getOpacity

    Added in API level 21
    int getOpacity ()

    返回此Drawable的不透明度/透明度。 返回的值是在抽象格式常数之一PixelFormatUNKNOWNTRANSLUCENTTRANSPARENT ,或OPAQUE

    OPAQUE drawable是一个绘制所有内容的范围,完全覆盖drawable后面的所有内容。 一个TRANSPARENT drawable是一个在其范围内没有绘制任何东西的图形,允许它后面的所有东西都显示出来。 TRANSLUCENT drawable是任何其他状态中的drawable,drawable将绘制其边界内的一些内容,但不是全部内容,并且至少drawable后面的某些内容将可见。 如果无法确定可绘制内容的可见性,则最安全/最佳返回值为TRANSLUCENT。

    一般而言,Drawable应该尽可能保守并且返回值。 例如,如果它包含多个子drawable,并且一次只显示其中一个,如果只有一个子项是TRANSLUCENT,而其他子项是OPAQUE,则应返回TRANSLUCENT。 您可以使用方法resolveOpacity(int, int)将两个不透明度标准减少到适当的单个输出。

    请注意,返回的值不一定会考虑客户端通过setAlpha(int)setColorFilter(ColorFilter)方法应用的自定义alpha或颜色过滤器。 一些子类,比如BitmapDrawableColorDrawable ,并GradientDrawable ,对价值做账setAlpha(int) ,但总的行为是依赖于子类实现的。

    Returns
    int int The opacity class of the Drawable.

    inflate

    Added in API level 21
    void inflate (Resources r, 
                    XmlPullParser parser, 
                    AttributeSet attrs, 
                    Resources.Theme theme)

    从可选的由主题设计的XML资源中扩展此Drawable。 对于每个Drawable,这不能被调用一次以上。 请注意,框架可能已经调用过这一次从XML资源创建Drawable实例。

    Parameters
    r Resources: Resources used to resolve attribute values
    parser XmlPullParser: XML parser from which to inflate this Drawable
    attrs AttributeSet: Base set of attribute values
    theme Resources.Theme: Theme to apply, may be null
    Throws
    XmlPullParserException
    IOException

    isAutoMirrored

    Added in API level 21
    boolean isAutoMirrored ()

    当它的布局方向是从右到左的RTL时,告诉它是否自动镜像。 LayoutDirection

    Returns
    boolean boolean Returns true if this Drawable will be automatically mirrored.

    isStateful

    Added in API level 21
    boolean isStateful ()

    指示此drawable是否会根据状态更改其外观。 客户可以使用它来确定是否有必要计算它们的状态并调用setState。

    Returns
    boolean True if this drawable changes its appearance based on state, false otherwise.

    mutate

    Added in API level 21
    Drawable mutate ()

    使这个drawable可变。 这个操作不能逆转。 一个可变的drawable保证不与其他drawable共享它的状态。 当你需要修改从资源加载的drawable的属性时,这是特别有用的。 默认情况下,从同一资源加载的所有可绘制实例共享一个公共状态; 如果修改一个实例的状态,则所有其他实例都将收到相同的修改。 在可变Drawable上调用此方法将不起作用。

    Returns
    Drawable This drawable.

    setAlpha

    Added in API level 21
    void setAlpha (int alpha)

    为drawable指定一个alpha值。 0表示完全透明,255表示完全不透明。

    Parameters
    alpha int

    setAutoMirrored

    Added in API level 21
    void setAutoMirrored (boolean mirrored)

    设置当其布局方向为RTL(从右到左)时,此Drawable是否自动镜像。 LayoutDirection

    Parameters
    mirrored boolean: Set to true if the Drawable should be mirrored, false if not.

    setColorFilter

    Added in API level 21
    void setColorFilter (ColorFilter colorFilter)

    为绘图指定一个可选的颜色过滤器。

    如果Drawable具有ColorFilter,则Drawable的绘图内容的每个输出像素在混合到Canvas的渲染目标之前将被颜色过滤器修改。

    通过 null删除任何现有的颜色过滤器。

    注意:设置非 null颜色过滤器会禁用 tint

    Parameters
    colorFilter ColorFilter: The color filter to apply, or null to remove the existing color filter

    setTintList

    Added in API level 21
    void setTintList (ColorStateList tint)

    指定该drawable的色彩颜色作为颜色状态列表。

    Drawable的绘图内容在绘制到屏幕前将与它的色调混合在一起。 这功能类似于setColorFilter(int, PorterDuff.Mode)

    注意:通过 setColorFilter(ColorFilter)setColorFilter(int, PorterDuff.Mode)设置颜色过滤器会覆盖色彩。

    Parameters
    tint ColorStateList: Color state list to use for tinting this drawable, or null to clear the tint

    setTintMode

    Added in API level 21
    void setTintMode (PorterDuff.Mode tintMode)

    指定该drawable的色调混合模式。

    定义在绘制屏幕之前,该绘制的色调应该如何混合到绘图中。 默认色调模式是SRC_IN

    注意:通过 setColorFilter(ColorFilter)setColorFilter(int, PorterDuff.Mode)设置颜色过滤器会覆盖色调。

    Parameters
    tintMode PorterDuff.Mode: A Porter-Duff blending mode

    Protected methods

    onStateChange

    Added in API level 21
    boolean onStateChange (int[] stateSet)

    如果您认识到指定的状态,请在您的子类中覆盖此内容以更改外观。

    Parameters
    stateSet int
    Returns
    boolean Returns true if the state change has caused the appearance of the Drawable to change (that is, it needs to be drawn), else false if it looks the same and there is no need to redraw it since its last state.

    Hooray!