将矢量绘制为XML中的imageview

时间:2018-02-27 17:17:45

标签: android android-layout

我创建了一个矢量drawable。这是animation.xml文件

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="24dp"
    android:width="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">

    <group
        android:name="customGroup">

        <path
            android:fillColor="#64B5F6"
            android:fillAlpha="50"
            android:strokeColor="#64B5F6"
            android:strokeWidth="0.1"
            android:pathData="M0,0 C0.5,-8 5,0 10,-0.5 C13,-0.5 17,-8 18,0 L18.5,5 L0.1,5 Z"/>

        <path
            android:fillColor="#BBDEFB"
            android:fillAlpha="40"
            android:strokeWidth="0.1"
            android:strokeColor="#BBDEFB"
            android:pathData="M7.5,0 C8,-8 12,0 16,-0.5 C20,-1 22,-6 24,-1 L24,5 L7.5,4.97 Z"/>

        <path
            android:fillColor="#42A5F5"
            android:fillAlpha="50"
            android:strokeColor="#42A5F5"
            android:strokeWidth="0.1"
            android:pathData="M5,3 C6,-5.2 9.5,3 15,0 C18,-1 20,-9 22,3 L22.3,5 L5,4.95 Z"/>

    </group>

</vector>

Here是我使用上述animation.xml文件

获得的图像

我正在尝试将此drawable设置为activity中的图片视图。这是我的activity布局文件

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/animation" />

</RelativeLayout>

Here是我得到的输出。当我看到预览窗口时,它正在工作。只有在我运行项目时才会出现问题。

为什么图像会被切割到顶部?请帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

在您的gradle文件中

,首先添加此行

 android {
   defaultConfig {
     vectorDrawables.useSupportLibrary = true
    }
 }

然后,替换你的ImageView而不是

  <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/animation" />

如果它仍然不起作用,您可以保留这些更改,并在Aplication类中添加以下行(从Aplication扩展的那个,然后在清单中声明它)

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
相关问题