裁剪以适应Android中的图像

裁剪以适应Android中的图像,android,image-manipulation,Android,Image Manipulation,我已经尝试了一段时间,我想从位图创建一张壁纸。假设所需的墙纸大小为320x480,源图像大小为2048x2048 我不确定裁剪是否合适,但我想实现的是获得与所需壁纸尺寸(320x480)具有相同比例的大部分图片 因此,在本例中,我希望从源位图中获取2048x1365或(确切地说是1365.333),并将其缩小到320x480 我尝试过的技巧是: 1) 首先将位图裁剪为2048x1365 bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 13

我已经尝试了一段时间,我想从
位图
创建一张壁纸。假设所需的墙纸大小为320x480,源图像大小为2048x2048

我不确定裁剪是否合适,但我想实现的是获得与所需壁纸尺寸(320x480)具有相同比例的大部分图片

因此,在本例中,我希望从源
位图中获取2048x1365或(确切地说是1365.333),并将其缩小到320x480

我尝试过的技巧是:

1) 首先将位图裁剪为2048x1365

bm = Bitmap.createBitmap(bm, xOffset, yOffset, 2048, 1365);
2) 将其缩小到320x480

bm = Bitmap.createScaledBitmap(bm, 320, 480, false);
这产生了OutOfMemory错误

有没有办法做到这一点

问候,


dezull

这里有一个答案,它能让你在大多数情况下达到目的:
多亏了开源,我在第230行的Android Gallery源代码中找到了答案:-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);

我知道这是一个令人难以置信的迟到回复,但类似这样的回答可能:

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;
}

我认为你的标题应该更好地描述为“缩放到合适,保持相同的高宽比”谢谢,这可能与标题一样合适,但实际上,我想要实现的是“缩放”,并“裁剪”图像的某些区域以适合。如果你解决了问题,请分享你的解决方案。谢谢,但不是我正在寻找的解决方案。请看我自己的答案,只需使用Rect来缩放它。它不会产生OOM:-)我有你同样的问题。你能解释一下mOutputX是什么吗,mCrop。。。或者更好,您是否可以编写示例函数,而不是接收位图并返回裁剪和缩放的位图?非常感谢。这些是输出宽度和高度。这个主题已经被添加到Android开发者的网站上:1。将srrect设置为位图的大小(0,0,bitmapWidth,bitmapHeight)。2.将dstRect设置为显示区域的大小。3.从“int dx”行执行。您可以使用我的JNI解决方案进行裁剪,这样可以避免同时具有两个位图:github.com/AndroidDeveloperLB/androidjnibitmaperations。另外,我想问一下,是否有一种方法可以避免这一切,那就是使用一个定制的绘图工具,它只绘制你想要绘制的位图的一部分。link死了!尝试发布内容而不是链接