Android 如何在 ImageView 中缩放图像以保持纵横比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2521959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:10:07  来源:igfitidea点击:

How to scale an Image in ImageView to keep the aspect ratio

androidandroid-imageviewimage-scaling

提问by michael

In Android, I defined an ImageView's layout_widthto be fill_parent(which takes up the full width of the phone).

在 Android 中,我定义了一个ImageView's layout_widthto be fill_parent(它占据了手机的整个宽度)。

If the image I put to ImageViewis bigger than the layout_width, Android will scale it, right? But what about the height? When Android scales the image, will it keep the aspect ratio?

如果我放置的图像ImageView大于layout_width,Android 会缩放它,对吗?但是身高呢?当Android缩放图像时,它会保持纵横比吗?

What I find out is that there is some white space at the top and bottom of the ImageViewwhen Android scales an image which is bigger than the ImageView. Is that true? If yes, how can I eliminate that white space?

我发现ImageView当 Android 缩放大于ImageView. 真的吗?如果是,我怎样才能消除那个空白?

回答by Steve Haley

  1. Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you're setting the image to the ImageView using android:src="..."rather than android:background="...". src=makes it scale the image maintaining aspect ratio, but background=makes it scale anddistort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.)

  2. You should also see android:adjustViewBoundsto make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.

    Then as Samuh wrote, you can change the way it default scales images using the android:scaleTypeparameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself! Just remember to look at the layouts in the emulator itself (or an actual phone) as the preview in Eclipse is usually wrong.

  1. 是的,默认情况下,Android 会缩小您的图像以适合 ImageView,同时保持纵横比。但是,请确保您使用android:src="..."而不是将图像设置为 ImageView android:background="..."src=使其缩放图像保持纵横比,但background=使其缩放扭曲图像以使其完全适合 ImageView 的大小。(不过,您可以同时使用背景和源,这对于在主图像周围显示框架等操作非常有用,仅使用一个 ImageView。)

  2. 您还应该看到android:adjustViewBounds使 ImageView 调整自身大小以适应重新缩放的图像。例如,如果您在通常是方形 ImageView 中有一个矩形图像,则 adjustViewBounds=true 会将 ImageView 的大小也调整为矩形。这会影响其他 Views 在 ImageView 周围的布局。

    然后正如 Samuh 所写,您可以使用android:scaleType参数更改默认缩放图像的方式。顺便说一下,发现它是如何工作的最简单的方法就是自己尝试一下!请记住查看模拟器本身(或实际手机)中的布局,因为 Eclipse 中的预览通常是错误的。

回答by Gratzi

See android:adjustViewBounds.

android:adjustViewBounds

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

如果您希望 ImageView 调整其边界以保留其可绘制对象的纵横比,则将此设置为 true。

回答by Kevin Parker

To anyone else having this particular issue. You have an ImageViewthat you want to have a width of fill_parentand a height scaled proportionately:

对于有这个特殊问题的其他人。您有一个要按比例缩放ImageView的宽度fill_parent和高度:

Add these two attributes to your ImageView:

将这两个属性添加到您的ImageView:

android:adjustViewBounds="true"
android:scaleType="centerCrop"

And set the ImageViewwidth to fill_parentand height to wrap_content.

并将ImageView宽度设置为fill_parent,高度为wrap_content

Also, if you don't want your image to be cropped, try this:

另外,如果您不想裁剪图像,请尝试以下操作:

 android:adjustViewBounds="true"
 android:layout_centerInParent="true"

回答by Kevin

If you want an ImageViewthat both scales up and down while keeping the proper aspect ratio, add this to your XML:

如果您希望ImageView在保持适当纵横比的同时进行缩放,请将其添加到您的 XML 中:

android:adjustViewBounds="true"
android:scaleType="fitCenter"

Add this to your code:

将此添加到您的代码中:

// We need to adjust the height if the width of the bitmap is
// smaller than the view width, otherwise the image will be boxed.
final double viewWidthToBitmapWidthRatio = (double)image.getWidth() / (double)bitmap.getWidth();
image.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);

It took me a while to get this working, but this appears to work in the cases both where the image is smaller than the screen width and larger than the screen width, and it does not box the image.

我花了一段时间才开始工作,但这似乎适用于图像小于屏幕宽度和大于屏幕宽度的情况,并且它不会将图像装箱。

回答by Mike6679

This worked for me:

这对我有用:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="39dip"
android:scaleType="centerCrop"
android:adjustViewBounds ="true"

回答by shafiq-ur-rehman

this solved my problem

这解决了我的问题

android:adjustViewBounds="true"
android:scaleType="fitXY"

回答by Mukesh Parmar

Below code Working for scale image as aspect ratio:

下面的代码用于缩放图像作为纵横比:

Bitmap bitmapImage = BitmapFactory.decodeFile("Your path");
int nh = (int) ( bitmapImage.getHeight() * (512.0 / bitmapImage.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(bitmapImage, 512, nh, true);
your_imageview.setImageBitmap(scaled);

回答by Samuh

Take a look at ImageView.ScaleTypeto control and understand the way resizing happens in an ImageView. When the image is resized (while maintaining its aspect ratio), chances are that either the image's height or width becomes smaller than ImageView's dimensions.

查看ImageView.ScaleType以控制和了解在ImageView. 调整图像大小时(同时保持其纵横比),图像的高度或宽度可能会小于ImageView的尺寸。

回答by Atif Mahmood

Use these properties in ImageView to keep aspect ratio:

在 ImageView 中使用这些属性来保持纵横比:

android:adjustViewBounds="true"
android:scaleType="fitXY"

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    />

回答by Jeet

This is how it worked for me inside a ConstraintLayout:

这就是它在 ConstraintLayout 中对我的工作方式:

<ImageView
    android:id="@+id/myImg"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"/>

Then in code, I set the drawable as:

然后在代码中,我将 drawable 设置为:

ImageView imgView = findViewById(R.id.myImg);
imgView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.image_to_show, null));

This fits the image nicely according to its aspect ratio and keeps it in centre.

这根据其纵横比很好地适合图像并将其保持在中心。