【问题标题】:Why my Bitmap after scaling seems empty为什么缩放后我的位图看起来是空的
【发布时间】:2013-12-11 21:14:49
【问题描述】:

我正在尝试缩放位图,使其大小翻倍。

但是缩放后位图显示为空,全是灰色...

代码如下:

Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(2, 2);

    // recreate the new Bitmap and set it back
    Bitmap bm2=Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);   
    //bm.recycle();

编辑编辑编辑编辑编辑编辑

我发现这是内存问题,如果我用小图像做它就可以了。

大图问题依旧!!!

谢谢你的建议!!!

【问题讨论】:

  • 发布您的源代码或两个版本的图像,既缩放又正常。

标签: android


【解决方案1】:

Bitmap Doc 开始,很明显Bitmap.createBitmap() 返回相同的位图或源位图的一部分。所以可能在这里它返回相同的位图对象。但是在这里你正在回收它

bm.recycle();

这就是为什么你得到 null

使用这个方法

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

并传递宽度为 1920 和高度为 2560

【讨论】:

  • 感谢 kalyan,如果我不回收,同样的结果.. :-((
  • 是否有任何位图正在返回或其为空??
  • 它不为空,在调试时它正确显示双倍宽度和双倍高度,但它都是灰色的...... :-((
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-15
  • 2022-10-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 2011-05-28
  • 2014-07-28
相关资源
最近更新 更多