显示包含SVG的全宽ImageView会扭曲它

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

我的目的是显示一个SVG背景图像填充整个屏幕的宽度,在ConstraintLayoutButton叠加在这个背景图像上,这就是为什么你可以在下面的模型中看到:

enter image description here

背景图像是包含星星的图像。它的起点和终点将被限制在根GroupView,因此它将完全填满屏幕的宽度。

问题如下:无论是否将底边绑定到屏幕的底边,背景图像都显示为distorded,如下所示:

enter image description here

这是我写的代码:

<ImageView
    android:id="@+id/imageView16"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/ic_background_stars"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/linearLayout4"  // "linearLayout4" is just a widget shown above, it's not an important element for this StackOverflow question
    />

<Button
    android:id="@+id/button_home_share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="24dp"
    android:background="@color/colorRoyalRedLight"
    android:text="TextView"
    android:textAllCaps="false"
    android:textColor="@color/white"
    app:layout_constraintStart_toStartOf="@+id/imageView16"
    app:layout_constraintEnd_toEndOf="@+id/imageView16"
    app:layout_constraintTop_toTopOf="@+id/imageView16"
    app:layout_constraintBottom_toBottomOf="@+id/imageView16"
    />

My question

我如何使用我的SVG图像作为背景图像,看起来没有任何扭曲,填满整个屏幕的宽度?高度当然可以自我调整(以保持良好的比例),但不应该比按钮更短(在Y和X中)。

android android-imageview android-constraintlayout androidsvg
1个回答
2
投票

对于ImageView尝试设置android:scaleType="centerCrop"。见ImageView.ScaleType

CENTER_CROP 均匀缩放图像(保持图像的纵横比),使图像的尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充)。然后图像在视图中居中。从XML,使用以下语法:android:scaleType =“centerCrop”。

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