【问题标题】:Android: Adding a VectorDrawable to an ImageViewAndroid:将 VectorDrawable 添加到 ImageView
【发布时间】:2017-04-13 13:41:30
【问题描述】:

所以,我想制作一个带有可点击国家的可滚动世界地图。所以当我发现有类似VectorDrawables 的东西时,我决定试一试。

接下来我从SVG 中创建了一个VectorDrawable xml 文件,并尝试将其放入我的ImageView 中。在搜索一些信息时,我发现有 101 种方法可以做到这一点,但没有一种对我有用...... 有趣的是,当我在ImageView 中的activity.xml 中添加"android:src="@raw/worldmap" 时,地图会显示在预览中,但每当我尝试运行该应用程序时,它就会崩溃。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/activity_show_hint"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.bert.myapplication.ShowHintActivity"
    android:weightSum="1">

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:scaleType="center"
        android:src="@raw/worldmap">
    </ImageView>


</LinearLayout>

【问题讨论】:

  • 您在哪个操作系统上运行?
  • 我正在运行 Ubuntu 16.04.2

标签: java android imageview android-vectordrawable


【解决方案1】:

首先,将您的 VectorDrawable 移动到 Drawable 文件夹(不是原始文件夹)。然后,在你的 Gradle 文件中添加这一行:

defaultConfig{
vectorDrawables.useSupportLibrary = true
}

最后,在你的 imageView 中,改成这个;

 <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"
        android:scaleType="center"
       app:srcCompat="@drawable/worldmap">
    </ImageView>

【讨论】:

    猜你喜欢
    • 2019-11-30
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多