为什么我的ImageView缩放不会像宽度允许那样填充布局?

时间:2017-12-07 21:33:36

标签: java android xml android-layout

我想要的是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 Guide CENTER_INSIDE应该

  

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

黑色图像是我现在的样子,而且我已经画出了我认为应该看到的东西(正如你所知,我是现代的米开朗基罗)。 image

4 个答案:

答案 0 :(得分:2)

将这两行添加到您的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 :(得分:1)

wrap_content如果之所以......将其更改为fill_parent或match_parent。

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

  

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

如果您的图像比例关闭,则不会拉伸。

爪哇

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

XML

android:scaleType="fitCenter"

旁注:fillXY可能会更好。

答案 2 :(得分: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>

答案 3 :(得分: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 on activity。

@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 
}