XML中的<gradient>标记会导致Android崩溃

时间:2017-12-12 23:08:45

标签: android

我有一个包含渐变组件的图标。

<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
        android:width="100dp"
        android:height="100dp"
        android:viewportWidth="100.0"
        android:viewportHeight="100.0">
    <path
        android:pathData="M68.13,36.74L68.13,36C68.13,30.48 63.65,26 58.13,26L58.13,26L25,26L25,26C19.48,26 15,30.48 15,36L15,36L15,64C15,69.52 19.48,74 25,74L58.13,74C63.65,74 68.13,69.52 68.13,64L68.13,64L68.13,60.74L77.17,69.79L77.17,69.79C77.92,70.54 78.94,70.96 80,70.96C82.21,70.96 84,69.17 84,66.96L84,30.52C84,29.46 83.58,28.44 82.83,27.69C81.27,26.13 78.74,26.13 77.17,27.69L68.13,36.74Z"
        android:strokeColor="#00000000"
        android:fillType="evenOdd"
        android:strokeWidth="1">
        <aapt:attr name="android:fillColor">
<gradient

        android:startY="60.028559008"
        android:startX="41.416262106"
        android:endY="100.0"
        android:endX="81.32660406299999"
        android:type="linear">
<item android:offset="0.0" android:color="#FFFA8561" />
<item android:offset="1.0" android:color="#FFFC5D5D" />
            </gradient></aapt:attr></path>
    <path
        android:pathData="M53.93,51.56C53.57,47.09 51.34,44.29 49.38,41.81C47.57,39.52 46,37.54 46,34.63C46,34.39 45.86,34.18 45.64,34.07C45.42,33.96 45.15,33.98 44.95,34.12C42,36.09 39.54,39.42 38.69,42.6C38.09,44.81 38.01,47.29 38,48.94C35.28,48.39 34.67,44.58 34.66,44.54C34.63,44.34 34.5,44.17 34.31,44.07C34.12,43.98 33.9,43.97 33.71,44.06C33.57,44.13 30.21,45.73 30.01,52.11C30,52.32 30,52.54 30,52.75C30,58.95 35.38,64 42,64C48.64,63.98 54,58.94 54,52.75C54,52.44 53.93,51.56 53.93,51.56Z"
        android:strokeColor="#00000000"
        android:fillType="nonZero"
        android:fillColor="#FFFFFF"
        android:strokeWidth="1"/>
</vector>

我正在尝试将此XML用作我应用中的图标。

然而,出于某种原因,当我做

<ImageView src="@drawable/icon"/>

在API 23上运行,我的应用程序会立即崩溃。

但是,在API级别19上,同一个应用程序运行时没有任何问题。

如果删除<gradient>标记,API 23也可以正常运行。

我应该怎么做才能让API 23中的应用程序没有任何问题?

1 个答案:

答案 0 :(得分:2)

这里的主要问题是您尝试在API 19中使用为API 24定义的渐变和属性,例如android:fillTypeandroid:startY

解决方案是:

  

创建2个drawables文件夹

  • drawable
  • drawable-v24
  

将此作为your_logo.xml放在drawable文件夹中(我已使用#FFFA8561从渐变中获取)

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp"
    android:viewportWidth="100.0"
    android:viewportHeight="100.0">
    <path
        android:pathData="M68.13,36.74L68.13,36C68.13,30.48 63.65,26 58.13,26L58.13,26L25,26L25,26C19.48,26 15,30.48 15,36L15,36L15,64C15,69.52 19.48,74 25,74L58.13,74C63.65,74 68.13,69.52 68.13,64L68.13,64L68.13,60.74L77.17,69.79L77.17,69.79C77.92,70.54 78.94,70.96 80,70.96C82.21,70.96 84,69.17 84,66.96L84,30.52C84,29.46 83.58,28.44 82.83,27.69C81.27,26.13 78.74,26.13 77.17,27.69L68.13,36.74Z"
        android:strokeColor="#00000000"
        android:fillColor="#FFFA8561"
        android:strokeWidth="1">
    </path>
    <path
        android:pathData="M53.93,51.56C53.57,47.09 51.34,44.29 49.38,41.81C47.57,39.52 46,37.54 46,34.63C46,34.39 45.86,34.18 45.64,34.07C45.42,33.96 45.15,33.98 44.95,34.12C42,36.09 39.54,39.42 38.69,42.6C38.09,44.81 38.01,47.29 38,48.94C35.28,48.39 34.67,44.58 34.66,44.54C34.63,44.34 34.5,44.17 34.31,44.07C34.12,43.98 33.9,43.97 33.71,44.06C33.57,44.13 30.21,45.73 30.01,52.11C30,52.32 30,52.54 30,52.75C30,58.95 35.38,64 42,64C48.64,63.98 54,58.94 54,52.75C54,52.44 53.93,51.56 53.93,51.56Z"
        android:strokeColor="#00000000"
        android:fillColor="#FFFFFF"
        android:strokeWidth="1"/>
</vector>
  

创建此gradient_logo.xml文件并将其放在drawable-v24文件夹中(不要担心Android Studio会抱怨此文件,即使标记为红色也会正常编译)

<?xml version="1.0" encoding="utf-8"?>
<gradient xmlns:android="http://schemas.android.com/apk/res/android"
    android:startY="60.028559008"
    android:startX="41.416262106"
    android:endY="100.0"
    android:endX="81.32660406299999"
    android:startColor="#FFFA8561"
    android:endColor="#FFFC5D5D"
    android:type="linear" />
  

your_logo.xml这个不同版本放在drawable-v24文件夹

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp"
    android:viewportWidth="100.0"
    android:viewportHeight="100.0">
    <path
        android:pathData="M68.13,36.74L68.13,36C68.13,30.48 63.65,26 58.13,26L58.13,26L25,26L25,26C19.48,26 15,30.48 15,36L15,36L15,64C15,69.52 19.48,74 25,74L58.13,74C63.65,74 68.13,69.52 68.13,64L68.13,64L68.13,60.74L77.17,69.79L77.17,69.79C77.92,70.54 78.94,70.96 80,70.96C82.21,70.96 84,69.17 84,66.96L84,30.52C84,29.46 83.58,28.44 82.83,27.69C81.27,26.13 78.74,26.13 77.17,27.69L68.13,36.74Z"
        android:strokeColor="#00000000"
        android:fillType="evenOdd"
        android:fillColor="@drawable/gradient_logo"
        android:strokeWidth="1">
    </path>
    <path
        android:pathData="M53.93,51.56C53.57,47.09 51.34,44.29 49.38,41.81C47.57,39.52 46,37.54 46,34.63C46,34.39 45.86,34.18 45.64,34.07C45.42,33.96 45.15,33.98 44.95,34.12C42,36.09 39.54,39.42 38.69,42.6C38.09,44.81 38.01,47.29 38,48.94C35.28,48.39 34.67,44.58 34.66,44.54C34.63,44.34 34.5,44.17 34.31,44.07C34.12,43.98 33.9,43.97 33.71,44.06C33.57,44.13 30.21,45.73 30.01,52.11C30,52.32 30,52.54 30,52.75C30,58.95 35.38,64 42,64C48.64,63.98 54,58.94 54,52.75C54,52.44 53.93,51.56 53.93,51.56Z"
        android:strokeColor="#00000000"
        android:fillType="nonZero"
        android:fillColor="#FFFFFF"
        android:strokeWidth="1"/>
</vector>
  

使用AppCompatImageView添加图片

<android.support.v7.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/your_logo"/>

结论:通过这种方式,您的项目可以正常编译,但您会发现最终会有2个略有不同的徽标,一个用于API&gt; = 24,另一个用于API&lt; = 23

enter image description here

我不知道您是否接受这一点,但如果您关心的徽标在每个平台上都不完全相同,那么可用的解决方案就是:

  1. 不要使用矢量作为徽标。使用mipmap文件夹中分发的经典PNG集。
  2. 请勿在徽标中使用渐变,并选择与API兼容的平面版本&gt; = 19