我已经尝试这个有一段时间了,我想从Bitmap
。假设所需的壁纸尺寸为 320x480,源图像尺寸为 2048x2048。
我不确定“裁剪适合”是否是正确的术语,但我想要实现的是获得与所需壁纸尺寸(320x480)具有相同比例的图片的大部分。
所以在这种情况下,我想从源获取 2048x1365 或(1365.333...准确地说)Bitmap
,并将其缩小到 320x480。
我尝试过的技术是:
1)先将Bitmap裁剪成2048x1365
bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365);
2) 将其缩小到 320x480
bm = Bitmap.createScaledBitmap(bm, 320, 480, false);
这产生了 OutOfMemory 错误。
有什么办法可以实现这一点吗?
Regards,
dezull
感谢开源,我从Android Gallery源码中找到了答案here在第 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)