矢量在android工作室中声明

时间:2017-03-19 12:36:26

标签: android vector layout

如何在android studio中使用矢量图形?

如何以编程方式更改矢量图形颜色?有可能吗?

你能指导一下矢量图在android中是如何工作的吗?

2 个答案:

答案 0 :(得分:0)

您无法在运行时更改单个路径的颜色。

但您可以使用setTint更改整个矢量的颜色。

 <ImageView
     android:id="@+id/vectorImageView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:tint="@color/my_nice_color"
     android:src="@drawable/ic_vector_drawable"
     android:scaleType="fitCenter" />

DrawableCompat.setTint(myImageView.getDrawable(),ContextCompat.getColor(context, R.color.another_nice_color));

Read this setColorTint doc

Here is full document regarding vector assets. Read this.

答案 1 :(得分:0)

VectorDrawable就像Android中的任何drawable一样。我在下面给出一个简单的VectorDrawable xml文件。您可以调整Android Studio中的数据,看看形状(绘图)如何变化。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="600dp"
        android:height="600dp"
        android:viewportWidth="800.0"
        android:viewportHeight="800.0">

    <path
        android:pathData="M  162   8
        q    -07    00   -41    26
        q    -34    27   -50    64
        q    -25    59   -19   117
        q     07    70    53   121
        q     57    63   151    62
        q     87   -01   140   -66
        q     46   -55    48  -142
        q     01   -56   -34  -105
        q    -38   -52   -77   -70
        l    -29   -11
        q     16   -01    31   -02
        q     59   -01   119   -02     "

        android:strokeLineCap="round"
        android:strokeColor="#f00f"
        android:fillColor="#00000000"
        android:strokeWidth="32"/>



</vector>

另请阅读VectorDrawable和AnimatedVectorDrawable的文档。

虽然你可以做路径变形,其他类型的动画,如trimPathEnd等你不能做其他事情,比如以编程方式更改strokeColor。

希望这有帮助。