裁剪到合适的机器人形象机器人、合适、形象

2023-09-06 00:55:55 作者:自导自演。

我一直在尝试了一段时间,我想创建一个从位图壁纸。比方说,所需的墙纸的大小是小320x480,并且源图像尺寸为2048×2048。

I've been trying this for some time, I would like to create a wallpaper from a Bitmap. Let's say the desired wallpaper size is 320x480, and the source image size is 2048x2048.

我不知道作物,以配合是否是正确的词,但我想实现的是让具有同等比例为所需壁纸大小(小320x480)画面的大部分。

I'm not sure whether crop-to-fit is the right term, but what I would like to achieve is to get most part of the picture that has the equal ratio as the desired wallpaper size (320x480).

因此​​,在这种情况下,我想从源头位图获取2048x1365或(1365.333 ......准确的说),这下扩展到小320x480。

So in this case, I would like to get 2048x1365 or (1365.333... to be exact) from the source Bitmap, and scale it down to 320x480.

这是我曾尝试该技术是:

The technique that I have tried is:

1)裁剪位图到2048x1365第一个

1) Crop the Bitmap into 2048x1365 first

bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365);

2)比例缩小到小320x480

2) Scale it down to 320x480

bm = Bitmap.createScaledBitmap(bm, 320, 480, false);

而产生内存溢出错误。

which produced OutOfMemory error.

有什么办法来实现这一目标?

Is there any way to achieve this?

问候,

dezull

推荐答案

由于开源,我发现从Android的相册源$ C ​​$ C答案here在行230 - D

Thanks to open source, I found the answer from Android Gallery source code here at line 230 :-D

croppedImage = Bitmap.createBitmap(mOutputX, mOutputY, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(croppedImage);

Rect srcRect = mCrop.getCropRect();
Rect dstRect = new Rect(0, 0, mOutputX, mOutputY);

int dx = (srcRect.width() - dstRect.width()) / 2;
int dy = (srcRect.height() - dstRect.height()) / 2;

// If the srcRect is too big, use the center part of it.
srcRect.inset(Math.max(0, dx), Math.max(0, dy));

// If the dstRect is too big, use the center part of it.
dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));

// Draw the cropped bitmap in the center
canvas.drawBitmap(mBitmap, srcRect, dstRect, null);