简体   繁体   中英

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. Or you can consider using different Paint to avoid the problem.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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