简体   繁体   English

Android:渐变作为填充颜色影响描边颜色

[英]Android: Gradient as fill color influences stroke color

I have assigned a gradient as a fill color to my vector drawable.我已将渐变作为填充颜色分配给我的矢量可绘制对象。 Strangely, the gradient has now also been adopted as the stroke color, although I actually assigned a separate color for the stroke color.奇怪的是,渐变现在也被用作描边颜色,虽然我实际上为描边颜色分配了一个单独的颜色。 Does anyone know how it comes to this?有谁知道这是怎么回事?

<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>

If you are using the same Paint for the vector drawable fill color and the stroke color, you need to assign the correct color every time before you actually do the drawing.如果您对矢量可绘制填充颜色和描边颜色使用相同的 Paint,则每次实际绘制之前都需要指定正确的颜色。 Or you can consider using different Paint to avoid the problem.或者您可以考虑使用不同的 Paint 来避免该问题。

[update] Ok, forget the above paragraph. [更新] 好吧,忘记上面这段了。 With the code, I find it works if changed to this:使用代码,如果更改为以下代码,我发现它可以工作:

<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>

Hope this solves your problem!希望这能解决您的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM