【问题标题】:Android: Gradient as fill color influences stroke colorAndroid:渐变作为填充颜色影响描边颜色
【发布时间】:2020-12-12 04:17:11
【问题描述】:

我已将渐变作为填充颜色分配给我的矢量可绘制对象。奇怪的是,渐变现在也被用作笔触颜色,尽管我实际上为笔触颜色分配了一个单独的颜色。有谁知道这是怎么回事?

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

<path
    android:strokeWidth="0.5"
    android:strokeColor="#000000"
    android:pathData="M0,0
                      v16
                      M28,0
                      v16
                      M6,12
                      q-2,0 -2,-2
                      v-4
                      q0,-2 2,-2
                      h9.567
                      a6,6 0 1,1 0,8
                      z">
    <aapt:attr name="android:fillColor">
        <gradient
            android:startY="8"
            android:startX="4"
            android:startColor = "#FF333333"
            android:endY="8"
            android:endX="26"
            android:endColor = "#FFFF0000"
            android:type="linear">
        </gradient>
    </aapt:attr>
</path>

</vector>

【问题讨论】:

  • 你能发一些代码吗?
  • @oziomajnr 为了更好地理解我还添加了代码
  • 为什么不从使用矢量drawable的cml中分配渐变... android:tint="@drawable/my_grdient"

标签: android android-studio ui-design


【解决方案1】:

如果您对矢量可绘制填充颜色和描边颜色使用相同的 Paint,则每次实际绘制之前都需要指定正确的颜色。或者你可以考虑使用不同的 Paint 来避免这个问题。

[update] 好吧,忘记上面这段了。有了代码,我发现如果改成这样就可以了:

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


    <path
        android:pathData="M0,0
                      v16
                      M28,0
                      v16
                      M6,12
                      q-2,0 -2,-2
                      v-4
                      q0,-2 2,-2
                      h9.567
                      a6,6 0 1,1 0,8
                      z">
        <aapt:attr name="android:fillColor">
            <gradient
                android:startY="8"
                android:startX="4"
                android:startColor = "#FF333333"
                android:endY="8"
                android:endX="26"
                android:endColor = "#FFFF0000"
                android:type="linear">
            </gradient>
        </aapt:attr>
    </path>

    <path
        android:strokeWidth="0.5"
        android:strokeColor="#000000"
        android:pathData="M0,0
                      v16
                      M28,0
                      v16
                      M6,12
                      q-2,0 -2,-2
                      v-4
                      q0,-2 2,-2
                      h9.567
                      a6,6 0 1,1 0,8
                      z">
    </path>
</vector>

希望这能解决您的问题!

【讨论】:

  • 但我不是这样。为了更好地理解,我还添加了代码
猜你喜欢
  • 2015-12-21
  • 2014-01-31
  • 2018-11-26
  • 2013-02-21
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
相关资源
最近更新 更多