内存不足的Andr​​oid中使用位图工作期间 [英] Out of memory during working with Bitmap in android

查看:91
本文介绍了内存不足的Andr​​oid中使用位图工作期间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面引用的方法圆任何位图的角上。有些时候它完美的作品,有的时候不是因为内存不足的异常。例外情况经常发生在该行

 位图输出= Bitmap.createBitmap(bitmap.getWidth()
        bitmap.getHeight(),Config.ARGB_8888);

难道我错过任何特殊的codeS吗?请帮我。

 公共静态位图roundBitmap(位图位图,诠释roundedRadius){
    如果(位图== NULL)
        返回null;
    如果(roundedRadius == 0){
        返回位图;
    }
    //位图输出= bitmap.copy(Config.ARGB_8888,真);
    位图输出= Bitmap.createBitmap(bitmap.getWidth()
            bitmap.getHeight(),Config.ARGB_8888);
    帆布帆布=新的Canvas(输出);
    最终诠释颜色= 0xffffff00;
    最终涂料粉刷=新的油漆();
    最终矩形矩形=新的Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
    最后RectF rectF =新RectF(RECT);
    最终浮动roundPx = roundedRadius;    paint.setAntiAlias​​(真);
    canvas.drawARGB(0,0,0,0);
    paint.setColor(颜色);
    canvas.drawRoundRect(rectF,roundPx,roundPx,油漆);    paint.setXfermode(新PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(位图,矩形,矩形,油漆);
    返回输出;
}


解决方案

如果你不调用后需要原始位 roundBitmap()你应该叫 bitmap.recycle()来释放该位图使用的内存。

I use the method quoted below to round the corners of any bitmap. Some time it works perfectly, some time not because of OutOfMemory exception. The exception often occur at the line

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
        bitmap.getHeight(), Config.ARGB_8888);

Do I miss any special codes here? Please help me.

 public static Bitmap roundBitmap(Bitmap bitmap, int roundedRadius) {
    if (bitmap == null)
        return null;
    if (roundedRadius == 0) {
        return bitmap;
    }
    // Bitmap output = bitmap.copy(Config.ARGB_8888, true);
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xffffff00;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = roundedRadius;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

解决方案

If you don't need the original bitmap after calling roundBitmap() you should call bitmap.recycle() to free the memory used by bitmap.

这篇关于内存不足的Andr​​oid中使用位图工作期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆