创建具有c​​reateScaledBitmap在Android的缩放位图位图、缩放、reateScaledBitmap、Android

2023-09-13 01:10:11 作者:机械发条的萌公主

我想创建一个缩放位图,但我似乎得到了DIS-图像成比例。它看起来像一个正方形,而我想成为矩形。

I want to create a scaled bitmap, but I seemingly get a dis-proportional image. It looks like a square while I want to be rectangular.

我的code:

Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 960, 960, false);

我想要的图像,以具有960一MAX我将如何做呢?设置宽度不编译。这也可能是简单的,但我不能环绕它在我的头上。谢谢

I want the image to have a MAX of 960. How would I do that? Setting width to null doesn't compile. It's probably simple, but I can't wrap my head around it. Thanks

推荐答案

如果你已经在内存中的原始位图,你不需要做 inJustDe codeBounds , inSampleSize ,等你只需要弄清楚用什么比例,并相应扩大。

If you already have the original bitmap in memory, you don't need to do the whole process of inJustDecodeBounds, inSampleSize, etc. You just need to figure out what ratio to use and scale accordingly.

final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = myBitmap.getWidth();
int inHeight = myBitmap.getHeight();
if(inWidth > inHeight){
    outWidth = maxSize;
    outHeight = (inHeight * maxSize) / inWidth; 
} else {
    outHeight = maxSize;
    outWidth = (inWidth * maxSize) / inHeight; 
}

Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);

如果该图像的唯一用途是一定比例的版本,你最好不要使用Tobiel的回答,以减少内存使用情况。

If the only use for this image is a scaled version, you're better off using Tobiel's answer, to minimize memory usage.