放大图像以填充Android中的整个ImageView

放大图像以填充Android中的整个ImageView

本文介绍了放大图像以填充Android中的整个ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按比例放大图像以占据ImageView的整个大小.这与使用scaleType=fit_center稍有不同,因为 fit_center 如果图像的宽高比与ImageView的宽高比不完全匹配,则会在图像周围留下条带.取而代之的是,我希望图像能够居中并按比例放大以完全填满周围的视图,并且将多余的部分都切掉.

I'd like to scale an image up to take up the entire size of an ImageView. This is subtly different than using scaleType=fit_center because fit_center will leave bands around the image if the image aspect ratio does not exactly match the ImageView's aspect ratio. Instead, I would like the image to get centered and scaled up to completely fill the enclosing view, with any excess chopped off.

我可以通过在onCreate()期间计算自己的自定义图像矩阵来实现此目的:

I'm able to accomplish this by computing my own custom image matrix during onCreate():

final Display display = getWindowManager().getDefaultDisplay();
final float screenWidth = display.getWidth();
final float screenHeight = display.getHeight();
final float imageWidth = splashView.getDrawable().getIntrinsicWidth();
final float imageHeight = splashView.getDrawable().getIntrinsicHeight();
final Matrix splashMatrix = new Matrix();
final float scale = Math.max(screenHeight/imageHeight,screenWidth/imageWidth);
splashMatrix.postScale( scale, scale );
splashView.setImageMatrix(splashMatrix);

这很好用,但似乎必须更轻松一些.有人知道在保留图像的纵横比并完全填充ImageView的同时在ImageView中放大图像的方法吗?

This works fine, but it seems like there must be an easier away. Does anyone know of a way to scale up an image in an ImageView while both preserving the aspect ratio of the image and fully filling in the ImageView?

(注意:在我的情况下,我的ImageView占据了全屏,因此我使用getWindowManager().getDisplay()查找所需的图像大小.不能使用splashView.getWidth()/getHeight(),因为视图尚未尚未布置,并且没有尺寸)

(Note: in my case my ImageView is taking up the full screen, so I use getWindowManager().getDisplay() to find the desired image size. You can't use splashView.getWidth()/getHeight() because the view hasn't been laid out yet and doesn't have a size)

推荐答案

您可以使用android:scaleType="centerCrop".保持宽高比并按需要缩放图像.

You can use android:scaleType="centerCrop".Keeps the aspect ratio and scales the image just like you want it.

有关更多信息,请通过以下链接

For more information please go through the below link

http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType

这篇关于放大图像以填充Android中的整个ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:19