将矢量绘制为XML中的imageview

问题描述 投票:0回答:1

我创建了一个矢量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是我得到的输出。当我看到预览窗口时,它正在工作。仅在我运行项目时才出问题。

为什么这个形象会被切断?请帮我解决这个问题。

提前致谢。

android android-layout
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.