如何使ImageView按比例填充布局,尽可能占据宽度

3

我希望ImageView能够按照相同的比例缩放以填充布局,而不被裁剪。由于宽度将首先匹配,因此有效地意味着缩放直到宽度最大化。

我的代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    ImageView img = findViewById(R.id.displayImg);
    img.setImageBitmap(bmp); // bmp is a public static Bitmap set by another class
    img.setRotation(90);
    img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}

我的布局是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/displayImg" />

</android.support.constraint.ConstraintLayout>

根据Android ImageView ScaleType: A Visual GuideCENTER_INSIDE应该:
将图像等比例缩放(保持图像的宽高比),使得图像的宽度和高度都小于或等于视图对应的维度(减去填充)。当前显示的是黑色图像,我已经画出了我认为应该看到的图像(显然,我是现代版的米开朗基罗)。image

如果您要将图像放入的视图比图像本身小,则只会使用wrap_content来显示实际大小。而如果图像比屏幕尺寸小,并且您将其设置为match_parent,则会出现模糊或旋转时缩放异常的情况。 - tyczj
1
请解释下为什么要踩我。 - Kartik Chugh
4个回答

3
将这两行代码添加到你的ImageView中:
android:scaleType="fitXY"
android:adjustViewBounds="true"
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:id="@+id/displayImg" />

1

如果wrap_content是导致问题的原因... 将其更改为fill_parent或match_parent。

看一下这个: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

CENTER_INSIDE将图像均匀缩放(保持图像的纵横比),以便图像的宽度和高度都等于或小于视图的相应尺寸(减去填充)。

如果您的图像比例不正确,它不会被拉伸。

Java

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

XML

android:scaleType="fitCenter"

侧注:fillXY 可能更好。

这只是将图像居中。 - Kartik Chugh
使用fitXY。如果它被拉伸了,您需要提供一个更大的图像。您可以利用hdpi、mdpi、xhdpi、xxhdpi、xxxhdpi为所有设备制作图像。 - letsCode

0

您可以通过将ImageView的尺寸与父布局匹配,并将fitCenter作为scaleType应用,以便在不失去纵横比的情况下填充视图,从而实现所需的结果:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:id="@+id/displayImg" />
</android.support.constraint.ConstraintLayout>

不起作用。图像相同,只是移动到中心。 - Kartik Chugh
@K_7。谢谢你让我知道,我已经更新了我的答案,包括scaleType,这样图片就可以填充视图但不会失去其纵横比。 - petey
我不是Android专家,但从我所读到的关于约束布局的所有内容来看,你不应该使用match_parent,而应该使用0dp - FromTheStix

0

我认为您使用了错误的值。请在xml中尝试使用android:scaleType="fitXY"

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/displayImg"
        android:scaleType="fitXY" />

</android.support.constraint.ConstraintLayout>

在活动中,您不需要再次设置scaleType。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    ImageView img = findViewById(R.id.displayImg);
    img.setImageBitmap(bmp); // bmp is a public static Bitmap set by another class
    img.setRotation(90);
    //img.setScaleType(ImageView.ScaleType.ImageView.ScaleType.FIT_XY); You don't need this 
}

这只是拉伸宽度,不是我要找的。 - Kartik Chugh
1
你必须进行分辨率处理。图片有一个分辨率,如果你想要适应更大的屏幕分辨率,它会被拉伸。也许你可以为多种分辨率准备你的图片。 - Turgay Mutlu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接