Android中的矢量动态调整大小

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

我在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

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

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

android svg svg-android android-vectordrawable
2个回答
6
投票

问题在于:

android:scaleType="center"

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

将图像置于视图中心,但不执行缩放。

尝试使用另一个scaleType,如:

android:scaleType="fitCenter"

0
投票

我有同样的问题。

如果你有原始的SVG。在Inkscape(或任何其他矢量编辑器)中打开相同的内容。

现在转到文件 - >文档属性并展开“将页面调整为内容...”,然后单击“将页面调整为绘图或选择”。

现在关闭窗口并保存SVG。

使用矢量工具将其导入android并使用任何宽度和高度。它将采用图像视图的宽度和高度。另外,不要忘记将比例类型更改为FitXY。

© www.soinside.com 2019 - 2024. All rights reserved.