简体   繁体   English

显示包含SVG的全角ImageView会使它变形

[英]Showing a full-width ImageView containing a SVG distorts it

My aim is to show a SVG background image filling the whole screen's width, in a ConstraintLayout . 我的目标是在ConstraintLayout显示填充整个屏幕宽度的SVG背景图像。 A Button is superposed to this background image, that's why you can see in the mock-up below: 一个Button叠加到此背景图像上,这就是为什么您可以在下面的模型中看到的原因:

在此处输入图片说明

The background image is the one that contains the stars. 背景图像是包含星星的图像。 Its start and end sides would be constrained to the root GroupView , so that it would fill entirely the width of the screen. 它的开始和结束都将被约束到根GroupView ,以便它将完全填充屏幕的整个宽度。

The problem is the following: whether I bind the bottom side to the bottom side of the screen or not, the background image appears distorded, as illustrated in the following: 问题如下:无论我是否将底面绑定到屏幕底面,背景图像看起来都失真,如下图所示:

在此处输入图片说明

Here is the code I've written: 这是我编写的代码:

<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 我的问题

How could I use my SVG image as a background image that would appear without any distorsion, filling the whole screen's width? 如何使用SVG图像作为背景图像而不会出现任何扭曲,从而占据整个屏幕的宽度? The height can of course adapt itself (to keep good proportions), but shouldn't be shorter (in Y and in X) than the button. 高度当然可以自适应(以保持良好的比例),但是高度(在Y和X中)不应短于按钮。

For the ImageView try setting android:scaleType="centerCrop" . 对于ImageView尝试设置android:scaleType="centerCrop" See ImageView.ScaleType . 请参见ImageView.ScaleType

CENTER_CROP CENTER_CROP

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). 统一缩放图像(保持图像的纵横比),以使图像的两个尺寸(宽度和高度)都等于或大于视图的相应尺寸(减去填充)。 The image is then centered in the view. 然后,图像在视图中居中。 From XML, use this syntax: android:scaleType="centerCrop". 在XML中,使用以下语法:android:scaleType =“ centerCrop”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM