在 Android 中使用 createScaledBitmap 创建缩放位图 [英] Creating a scaled bitmap with createScaledBitmap in Android
问题描述
我想创建一个缩放的位图,但我似乎得到了一个不成比例的图像.它看起来像一个正方形,而我想变成矩形.
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.
我的代码:
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 960, 960, false);
我希望图像的 MAX 为 960.我该怎么做?将宽度设置为 null
不会编译.这可能很简单,但我无法理解它.谢谢
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
推荐答案
如果内存中已经有了原始位图,则无需进行inJustDecodeBounds
、的整个过程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.
这篇关于在 Android 中使用 createScaledBitmap 创建缩放位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!