2016-11-09 190 views
1

我有以下矢量繪製:矢量繪製

<?xml version="1.0" encoding="utf-8"?> 
<vector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="25dp" 
    android:height="25dp" 
    android:viewportWidth="25" 
    android:viewportHeight="25"> 

    <path 
     android:fillColor="#FFFFFF" 
     android:fillType="evenOdd" 
     android:strokeWidth="1" 
     android:pathData="M12.5,25 C19.4035594,25 25,19.4035594 25,12.5 C25,5.59644063 19.4035594,0 12.5,0 
C5.59644063,0 0,5.59644063 0,12.5 C0,19.4035594 5.59644063,25 12.5,25 Z M12.5,24 
C18.8512746,24 24,18.8512746 24,12.5 C24,6.14872538 18.8512746,1 12.5,1 
C6.14872538,1 1,6.14872538 1,12.5 C1,18.8512746 6.14872538,24 12.5,24 Z M16,6 
C17.6568542,6 19,7.34226429 19,8.99878564 L19,16.0012144 C19,17.6573979 
17.6534829,19 16,19 C14.3431458,19 13,17.6577357 13,16.0012144 L13,8.99878564 
C13,7.34260206 14.3465171,6 16,6 Z M14,16.0012144 C14,17.1052262 14.8952058,18 
16,18 C17.1026267,18 18,17.1036857 18,16.0012144 L18,8.99878564 C18,7.89477378 
17.1047942,7 16,7 C14.8973733,7 14,7.89631432 14,8.99878564 L14,16.0012144 Z 
M8.33473905,12.5 C8.14415114,12.3451212 7.57820716,11.8273213 
7.57820716,11.8273213 C6.80306599,11.0695455 6,10.0347728 6,9 C6,7 7,6 9,6 C11,6 
12,7 12,9 C12,10.0347728 11.196934,11.0695455 10.4217928,11.8273213 
C10.4217928,11.8273213 9.85584886,12.3451212 9.66526095,12.5 
C9.85584886,12.6548788 10.4217928,13.1726787 10.4217928,13.1726787 
C11.196934,13.9304545 12,14.9652272 12,16 C12,18 11,19 9,19 C7,19 6,18 6,16 
C6,14.9652272 6.80306599,13.9304545 7.57820716,13.1726787 C7.57820716,13.1726787 
8.14415114,12.6548788 8.33473905,12.5 Z M9.03460423,12 C10.1996214,11.0532638 
11,9.74316 11,9 C11,7.55228475 10.4477153,7 9,7 C7.55228475,7 7,7.55228475 7,9 
C7,9.74316 7.86958707,11.0532638 9.03460423,12 L8.96539577,13 
C7.80037862,13.9467362 7,15.25684 7,16 C7,17.4477153 7.55228475,18 9,18 
C10.4477153,18 11,17.4477153 11,16 C11,15.25684 10.1304129,13.9467362 
8.96539577,13 L9.03460423,12 Z" /> 
</vector> 

在Android Studio中預覽選項卡它看起來像這樣(這是需要什麼):

enter image description here

但是當我以編程方式將其設置爲ImageView作爲其源時,代碼爲:

imageMenuItem.setImageResource(R.drawable.keno); 

它看起來像這樣:

enter image description here

XML代碼爲ImageView很簡單:

<ImageView 
    android:id="@+id/image_menu_item" 
    style="@style/UpperItemsImageLeftMenu" 
    app:srcCompat="@drawable/leftmenu_keno" 
    android:layout_width="@dimen/upper_item_image_dim_left_menu" 
    android:layout_height="@dimen/upper_item_image_dim_left_menu" 
    /> 

深灰色的背景是,在其他地方設置。有誰知道我該如何解決這個問題?

+1

根據我的經驗,Vector Drawables不支持'evenOdd'填充規則,所以路徑需要重新定向以正確顯示https://developer.mozilla.org/en/docs/Web/SVG/Attribute/fill-rule – Duopixel

+1

@Duopixel可以調出填充規則,儘管我會添加更多的細節。 VectorDrawable默認填充規則是「非零」;改變填充規則只是在API 24中引入的。所以,你應該堅持「非零」嘗試配置你的矢量圖形軟件給你。一些鏈接: https://medium.com/@dineshbob10/the-mystery-of-the-disappearing-holes-a-gripping-tale-of-using-svg-in-android-442f6035a452#.hnq7fat30 https: //blog.stylingandroid.com/vectordrawable-fill-windings/ –

+1

@Duopixel @Lewis,你是對的。我從xml中移除了'android:fillType =「evenOdd」',預覽顯示了實心圓。非常感謝你!太糟糕了,你沒有回答,但評論它,因爲我不能設置正確的答案。 –

回答

4

好的,這裏是解決方案。

問題

問題的來源是與XML的填充規則:android:fillType="evenOdd"。 Android不支持此參數,直到Nougat版本,所以我們必須以某種方式避免它。正如@LewisMcGeary指出的那樣,在這個頂點上有一個很好的解釋:Marc Allison's blog。按照@Duopixel的建議,可以在this link上找到有關此填充規則的更多信息。

我的解決方案

我通過改變軟件解決了這個問題。我用這個online tool如之前提到的博客文章,設置如下建議:

enter image description here

,然後我救了它與下面的內容,以XML:

<?xml version="1.0" encoding="utf-8"?> 
<vector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:auto="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" 
    tools:ignore="NewApi" 
    android:viewportWidth="25" 
    auto:viewportWidth="25" 
    android:viewportHeight="25" 
    auto:viewportHeight="25" 
    android:width="25dp" 
    auto:width="25dp" 
    android:height="25dp" 
    auto:height="25dp"> 
    <path 
     android:pathData="M12.5 25C19.40356 25 25 19.40356 25 12.5C25 5.596441 19.40356 0 12.5 0C5.596441 0 0 5.596441 0 12.5C0 19.40356 5.596441 25 12.5 25M16 6C17.65685 6 19 7.342264 19 8.998786L19 16. 00121C19 17.6574 17.65348 19 16 19C14.34315 19 13 17.65774 13 16.00121L13 8.998786C13 7.342602 14.34652 6 16 6M14 16.00121C14 17.10523 14.89521 18 16 18C17.10263 18 18 17.10369 18 16.00121L18 8. 998786C18 7.894774 17.10479 7 16 7C14.89737 7 14 7.896314 14 8.998786L14 16.00121M8.334739 12.5C8.144151 12.34512 7.578207 11.82732 7.578207 11.82732C6.803066 11.06955 6 10.03477 6 9C6 7 7 6 9 6C11  6 12 7 12 9C12 10.03477 11.19693 11.06955 10.42179 11.82732C10.42179 11.82732 9.855849 12.34512 9.665261 12.5C9.855849 12.65488 10.42179 13.17268 10.42179 13.17268C11.19693 13.93046 12 14.96523 12  16C12 18 11 19 9 19C7 19 6 18 6 16C6 14.96523 6.803066 13.93046 7.578207 13.17268C7.578207 13.17268 8.144151 12.65488 8.334739 12.5M9.034604 12C10.19962 11.05326 11 9.74316 11 9C11 7.552285 10.44771  7 9 7C7.552285 7 7 7.552285 7 9C7 9.74316 7.869587 11.05326 9.034604 12M8.965396 13C7.800378 13.94674 7 15.25684 7 16C7 17.44772 7.552285 18 9 18C10.44771 18 11 17.44772 11 16C11 15.25684 10.13041 13 .94674 8.965396 13M12.5 24C6.148726 24 1 18.85127 1 12.5C1 6.148726 6.148726 1 12.5 1C18.85127 1 24 6.148726 24 12.5C24 18.85127 18.85127 24 12.5 24" 
     auto:pathData="M12.5 25C19.40356 25 25 19.40356 25 12.5C25 5.596441 19.40356 0 12.5 0C5.596441 0 0 5.596441 0 12.5C0 19.40356 5.596441 25 12.5 25M16 6C17.65685 6 19 7.342264 19 8.998786L19 16. 00121C19 17.6574 17.65348 19 16 19C14.34315 19 13 17.65774 13 16.00121L13 8.998786C13 7.342602 14.34652 6 16 6M14 16.00121C14 17.10523 14.89521 18 16 18C17.10263 18 18 17.10369 18 16.00121L18 8. 998786C18 7.894774 17.10479 7 16 7C14.89737 7 14 7.896314 14 8.998786L14 16.00121M8.334739 12.5C8.144151 12.34512 7.578207 11.82732 7.578207 11.82732C6.803066 11.06955 6 10.03477 6 9C6 7 7 6 9 6C11  6 12 7 12 9C12 10.03477 11.19693 11.06955 10.42179 11.82732C10.42179 11.82732 9.855849 12.34512 9.665261 12.5C9.855849 12.65488 10.42179 13.17268 10.42179 13.17268C11.19693 13.93046 12 14.96523 12  16C12 18 11 19 9 19C7 19 6 18 6 16C6 14.96523 6.803066 13.93046 7.578207 13.17268C7.578207 13.17268 8.144151 12.65488 8.334739 12.5M9.034604 12C10.19962 11.05326 11 9.74316 11 9C11 7.552285 10.44771  7 9 7C7.552285 7 7 7.552285 7 9C7 9.74316 7.869587 11.05326 9.034604 12M8.965396 13C7.800378 13.94674 7 15.25684 7 16C7 17.44772 7.552285 18 9 18C10.44771 18 11 17.44772 11 16C11 15.25684 10.13041 13 .94674 8.965396 13M12.5 24C6.148726 24 1 18.85127 1 12.5C1 6.148726 6.148726 1 12.5 1C18.85127 1 24 6.148726 24 12.5C24 18.85127 18.85127 24 12.5 24" 
     android:fillColor="#ffffff" 
     auto:fillColor="#ffffff" /> 
</vector> 

我手動刪除了所有auto:xxx標籤和結束與

<?xml version="1.0" encoding="utf-8"?> 
<vector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:ignore="NewApi" 
    android:viewportWidth="25" 
    android:viewportHeight="25" 
    android:width="25dp" 
    android:height="25dp" 
    > 
    <path 
     android:pathData="M12.5 25C19.40356 25 25 19.40356 25 12.5C25 5.596441 19.40356 0 12.5 0C5.596441 0 0 5.596441 0 12.5C0 19.40356 5.596441 25 12.5 25M16 6C17.65685 6 19 7.342264 19 8.998786L19 16.00121C19 17.6574 17.65348 19 16 19C14.34315 19 13 17.65774 13 16.00121L13 8.998786C13 7.342602 14.34652 6 16 6M14 16.00121C14 17.10523 14.89521 18 16 18C17.10263 18 18 17.10369 18 16.00121L18 8.998786C18 7.894774 17.10479 7 16 7C14.89737 7 14 7.896314 14 8.998786L14 16.00121M8.334739 12.5C8.144151 12.34512 7.578207 11.82732 7.578207 11.82732C6.803066 11.06955 6 10.03477 6 9C6 7 7 6 9 6C11 6 12 7 12 9C12 10.03477 11.19693 11.06955 10.42179 11.82732C10.42179 11.82732 9.855849 12.34512 9.665261 12.5C9.855849 12.65488 10.42179 13.17268 10.42179 13.17268C11.19693 13.93046 12 14.96523 12 16C12 18 11 19 9 19C7 19 6 18 6 16C6 14.96523 6.803066 13.93046 7.578207 13.17268C7.578207 13.17268 8.144151 12.65488 8.334739 12.5M9.034604 12C10.19962 11.05326 11 9.74316 11 9C11 7.552285 10.44771 7 9 7C7.552285 7 7 7.552285 7 9C7 9.74316 7.869587 11.05326 9.034604 12M8.965396 13C7.800378 13.94674 7 15.25684 7 16C7 17.44772 7.552285 18 9 18C10.44771 18 11 17.44772 11 16C11 15.25684 10.13041 13.94674 8.965396 13M12.5 24C6.148726 24 1 18.85127 1 12.5C1 6.148726 6.148726 1 12.5 1C18.85127 1 24 6.148726 24 12.5C24 18.85127 18.85127 24 12.5 24" 
     android:fillColor="#ffffff" 
     /> 
</vector> 

這是我一直在尋找。

+1

實際上'android:fillType =「evenOdd」'現在支持幾個月:https://issuetracker.google.com/issues/70782765 –