【问题标题】:Android - how to set tint on vector drawables with two or more colorsAndroid - 如何在具有两种或多种颜色的矢量绘图上设置色调
【发布时间】:2019-08-25 10:55:38
【问题描述】:

我喜欢矢量可绘制对象,而且我可以使用简单的 tint 属性在每个布局中更改它的色调,如下所示:

<ImageView
            android:id="@+id/image_payment_cash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint="@color/color1"
            android:src="@drawable/ic_cash" />

但我想知道是否有一种方法可以为同一个矢量(对于多色矢量)指定两种或多种色调? 所以可以这样设置:

 <ImageView
            android:id="@+id/image_payment_cash"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint1="@color/color1"
            android:tint2 = "@color/color2"
            android:src="@drawable/ic_cash" />

【问题讨论】:

    标签: android android-layout android-drawable android-resources android-vectordrawable


    【解决方案1】:

    不,默认情况下,无法在 XML 中为 Vector Drawable 着色多种不同颜色。

    您可以在 Java/Kotlin 中以编程方式将可绘制文件膨胀为 VectorDrawable 类,然后遍历每一层,然后单独更改颜色。
    通过使用相同的代码,您可以创建具有“tint1”和“tint2”属性的自定义 ImageView。

    如果您使用 API 21 以下的 Vector Drawables,请注意 Android 框架用于向后移植某些 VD 元素的不同类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      相关资源
      最近更新 更多