我想将图像放大以占据 ImageView 的整个大小。这与使用略有不同,scaleType=fit_center
因为如果图像纵横比与 ImageView 的纵横比不完全匹配,fit_center将在图像周围留下条带。相反,我希望图像居中并放大以完全填充封闭的视图,并切掉任何多余的部分。
我可以通过在 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?
(注意:在我的情况下,我的 ImageView 占据了全屏,所以我getWindowManager().getDisplay()
用来查找所需的图像大小。你不能使用splashView.getWidth()
/getHeight()
因为视图尚未布局并且没有大小)