VectorDrawable 不显示或绘制错误

VectorDrawable not showing or drawed wrong

我有 5 个 vectorDrawables,其中 3 个是从 google 给我的 PSD 生成的。

其中两个显示正确(均来自 PSD 生成的矢量),但其他两个根本不起作用,当 运行 在 API 21 上时,但在 API 26, 27.

没有显示的两个向量看起来像这样

两个 ImageButton 看起来像这样:(full XML file)

  <ImageButton
    android:id="@+id/multiplayerButton"
    android:layout_width="0dp"
    android:layout_height="80dp"
    android:layout_marginTop="4dp"
    android:background="@drawable/rounded_corners"
    android:paddingBottom="2dp"
    android:paddingTop="2dp"
    android:scaleType="fitCenter"
    android:src="@drawable/button_multiplayer_icon"
    app:layout_constraintEnd_toEndOf="@id/guideline3"
    app:layout_constraintStart_toStartOf="@id/guideline2"
    app:layout_constraintTop_toBottomOf="@id/startButton" />

    <ImageButton
    android:id="@+id/achievementButton"
    android:layout_width="0dp"
    android:layout_height="80dp"
    android:layout_marginStart="2dp"
    android:layout_marginTop="4dp"
    android:background="@drawable/rounded_corners"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    android:scaleType="fitCenter"
    android:src="@drawable/button_achievements_icon"
    app:layout_constraintEnd_toEndOf="@id/multiplayerButton"
    app:layout_constraintStart_toEndOf="@id/highscoreButton"
    app:layout_constraintTop_toBottomOf="@id/multiplayerButton" />

vector是这个样子

<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/menu_buttons" android:name="ic multiplayer"
    android:pathData="m468.9 41.5 -73.1 15.5 -127.2 139.4 43.1 47.3 141.6 -129.2 15.5 -73.1zm-267.5 228.6 -55.8 61.2 -28.7 -28.7 -28.8 28.7 20 20 -69.5 69.5 51.6 51.6 69.4 -69.5 19.4 19.3 28.7 -28.7 -28.7 -28.7 63 -57.5 -40.7 -37.2zm-158.4 -228.6 15.6 73.1 274.3 250.3 -28.7 28.7 28.8 28.8 19.3 -19.3 69.5 69.5 51.6 -51.6 -69.4 -69.5 20 -20 -28.8 -28.7 -28.7 28.7 -250.3 -274.3 -73.1 -15.5z"/>

功能区 vector xml 文件如下所示

<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/menu_buttons" android:name="ic achievements"
    android:pathData="m256.3 343.9 47.2 21.9 23 -34.2 55.3 104 -69.8 68.3 -55.7 -160zm-0.7 0 -47.2 21.9 -22.9 -34.2 -55.3 104 69.8 68.3 55.7 -160zm0.3 -296.5 43.1 -20 27.4 38.9 47.3 4.2 4.2 47.4 38.9 27.3 -20 43.1 20 43.1 -38.9 27.4 -4.2 47.3 -47.3 4.2 -27.4 38.9 -43.1 -20 -43.1 20 -27.4 -38.9 -47.3 -4.2 -4.2 -47.3 -38.9 -27.4 20 -43.1 -20 -43.1 38.9 -27.3 4.2 -47.4 47.3 -4.2 27.4 -38.9 43.1 20zm0.2 48.5c-50.8 0 -92.1 41.2 -92.1 92c0 50.9 41.2 92.1 92.1 92.1c50.8 0 92 -41.2 92 -92.1c0 -50.8 -41.2 -92 -92 -92z"/>

其中变量menu_buttons定义为#FFC107,在colors.xml

VectorDrawable 添加到 API 级别 21。

Support Library 23.2 or higher provides full support to Vector Drawables and Animated Vector Drawables on devices running Android 5.0 (API level 21) or lower.

你应该使用

 app:srcCompat="@drawable/your_vector_image"

您需要将 vectorDrawables.useSupportLibrary = true 添加到您的 build.gradle 文件中:

// Gradle Plugin 2.0+  
 android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 } 

仅供参考

app:srcCompat attribute to reference vector drawables as well as any other drawable available to android:src.