gpt4 book ai didi

android - 如何在android中高质量且不崩溃地缩放位图

转载 作者:行者123 更新时间:2023-11-29 02:10:36 24 4
gpt4 key购买 nike

我正在使用 url 下载图片,但缩放后它崩溃了,而且缩放不正确。这是我用过的代码,

//Code to fetch bitmap  
Bitmap bmp=HttpFetch.fetchBitmap(imageUrl);

//Scale bitmap

Bitmap myBitmap = getResizedBitmap(bmp, viewWidth, viewHeight);

public static Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

int srcWidth = bm.getWidth();
int srcHeight = bm.getHeight();

int desiredWidth = newWidth;
int desiredHeight = newHeight;

int inSampleSize = 1;
while(srcWidth / 2 > desiredWidth){
srcWidth /= 2;
srcHeight /= 2;
inSampleSize *= 2;
}

float desiredWidthScale = (float) desiredWidth / srcWidth;
float desiredHeightScale = (float) desiredHeight / srcHeight;

// Decode with inSampleSize
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = inSampleSize;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Matrix matrix = new Matrix();
matrix.postScale(desiredWidthScale, desiredHeightScale);
original = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
return original;
}

找不到使用位图获取 BitmapFactory.Option 的代码以及它使用文件名的大部分地方,是否可以使用位图获取 BitmapFactory.Options?

当我们从网络下载时,请提出更好的缩放位图的解决方案。

最佳答案

original = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

此行返回一个不可变的 Bitmap,在调用 getResizedBitmap() 后,如果您试图修改此 Bitmap,可能会导致崩溃。

Android#createBitmap()

详情请查阅 LogCat。

关于android - 如何在android中高质量且不崩溃地缩放位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7669703/

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