gpt4 book ai didi

java - 缩放位图随机不显示在 ImageView 中

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:07 24 4
gpt4 key购买 nike

我正在缩小 MediaStore.ACTION_IMAGE_CAPTURE 拍摄的图像(尝试遵循所有更好的做法)

缩放图像分两步完成:

  1. 将捕获图像的完美样本解码为位图
  2. 旋转和缩放位图以适合我的设备显示尺寸。

我真的不明白为什么有时图像在 ImageView 中完美显示,有时却是空白!非常感谢任何帮助:-)

更新:我正在onActivityResult中完成所有处理工作。不确定这是这种奇怪行为背后的原因吗?

为了更好地理解问题,我分享了代码的一些关键部分:

位图工厂选项

BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[32 * 1024];
options.inSampleSize = calculateInSampleSize(imageHeight, imageWidth, screenHeight, screenWidth);

样本量计算

private int calculateInSampleSize(int imageHeight, int imageWidth, int reqHeight, int reqWidth)
{
int inSampleSize = 1;
if (imageHeight > reqHeight || imageWidth > reqWidth) {

final int halfHeight = imageHeight / 2;
final int halfWidth = imageWidth / 2;

while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}

解码位图并调整其大小

image = BitmapFactory.decodeFile(fileName, options);
Matrix matrix = new Matrix();

float scaleFactor = Math.min(1.0f, Math.max(width / imageWidth, height / imageHeight));
matrix.postScale(scaleFactor, scaleFactor);

Bitmap tempImage = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, false);
if( image.isMutable() || (image.getHeight()!=tempImage.getHeight() || image.getWidth()!=tempImage.getWidth()) ) {
image.recycle();
}

imageView.setImageBitmap(tempImage);

最佳答案

我终于弄清楚了,问题是整个布局在 onResume() 中被重新绘制,因此 ImageView 被重置。我调用了相同的方法,在 onResume() 而不是 onActivityResult() 方法中将此位图设置为 imageview,最终能够使其正常工作。

关于java - 缩放位图随机不显示在 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32748169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com