dez*_*ull 5 android image-manipulation
我已经尝试了一段时间了,我想创建一个壁纸Bitmap
.假设所需的壁纸尺寸为320x480,源图像尺寸为2048x2048.
我不确定作物适合是否是正确的术语,但我想要实现的是获得与所需壁纸尺寸(320x480)具有相同比例的大部分图片.
所以在这种情况下,我想从源头获得2048x1365或(1365.333 ......准确)Bitmap
,并将其缩小到320x480.
我尝试过的技术是:
1)首先将位图裁剪为2048x1365
bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365);
Run Code Online (Sandbox Code Playgroud)
2)将其缩小到320x480
bm = Bitmap.createScaledBitmap(bm, 320, 480, false);
Run Code Online (Sandbox Code Playgroud)
这产生了OutOfMemory错误.
有没有办法实现这个目标?
问候,
dezull
dez*_*ull 15
由于开源的,我发现从Android图库源代码的答案在这里,在管线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);
Run Code Online (Sandbox Code Playgroud)
Dan*_*ker 10
我知道这是一个非常迟到的回复,但这样的事情可能是:
public static Bitmap scaleCropToFit(Bitmap original, int targetWidth, int targetHeight){
//Need to scale the image, keeping the aspect ration first
int width = original.getWidth();
int height = original.getHeight();
float widthScale = (float) targetWidth / (float) width;
float heightScale = (float) targetHeight / (float) height;
float scaledWidth;
float scaledHeight;
int startY = 0;
int startX = 0;
if (widthScale > heightScale) {
scaledWidth = targetWidth;
scaledHeight = height * widthScale;
//crop height by...
startY = (int) ((scaledHeight - targetHeight) / 2);
} else {
scaledHeight = targetHeight;
scaledWidth = width * heightScale;
//crop width by..
startX = (int) ((scaledWidth - targetWidth) / 2);
}
Bitmap scaledBitmap = Bitmap.createScaledBitmap(original, (int) scaledWidth, (int) scaledHeight, true);
Bitmap resizedBitmap = Bitmap.createBitmap(scaledBitmap, startX, startY, targetWidth, targetHeight);
return resizedBitmap;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16025 次 |
最近记录: |