android - Android 中的矢量动态调整大小

标签 android svg svg-android android-vectordrawable

我在 android 中使用 SVG 导入图像,但我无法使用 ImageView 调整它的大小,图像是这样的。

enter image description here

导入的drawable是:

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="208dp"
android:height="208dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
    android:fillColor="#8A1D3043"
    android:pathData="...lines and curves..."/>
</vector>

当我更改 ImageView(它附加到的位置)layout_height 和 layout_width 时,我得到:

        <ImageView
        android:layout_height="100dp"
        android:layout_width="100dp"
        app:layout_width="@{smallScreen ? @dimen/img_small_screen_size : @dimen/img_big_screen_size}"
        app:layout_height="@{smallScreen ? @dimen/img_small_screen_size : @dimen/img_big_screen_size}"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@{message}"
        android:src="@{image}"
        android:scaleType="center"
        tools:src="@drawable/img_message_private"/>

enter image description here

注意vector的高和宽都是208dp,但是我的ImageView必须是100dp。我认为必须使用 dataBind 动态调整 View 的大小,因此不能选择多个向量。

显然,图像不会随其 ImageView 缩放。有人知道为什么以及如何轻松绕过它吗?

最佳答案

问题出在线路上:

android:scaleType="center"

Here's a link to the documentation在哪里你可以看到这个的定义:

Center the image in the view, but perform no scaling.

尝试使用另一种 scaleType,例如:

android:scaleType="fitCenter"

关于android - Android 中的矢量动态调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34866956/

相关文章:

android - 如何在 res/values/dimens.xml 中使用主题定义的变量?

javascript - javascript 中的 SVG 翻译转换在 IE 11 中不起作用

internet-explorer - SVG未在Firefox和IE中使用Kendo模板渲染

java - 由 : java. lang.NullPointerException 引起:尝试在空对象引用上调用虚拟方法 'float java.lang.Float.floatValue()'

android - Parcelable 如何在 Android 中工作?空指针异常

android - PJSIP + FFMPEG 构建错误

android - 我们如何从 xmpp 服务器访问存档消息到 android 应用程序

html - IE 文档模式 8 或以下不兼容 .svg 图片类型

android - 具有可点击区域的可缩放 SVG - Android

android:load svg file from web and show it on image view