我的应用程序中的Android OutOfMemoryError。

时间:2013-08-22 04:46:10

标签: android bitmap out-of-memory

OOM例外这是我使用的代码

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap b = ((BitmapDrawable) drawable).getBitmap();
    if (b == null) {
        Log.d("null", "yes");
    } else {
        Log.d("not null", "yes");
    }
    Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();
    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);

    canvas.drawBitmap(roundBitmap, 0, 0, null);

}

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
    Bitmap sbmp;
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
            Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
            sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);

    return output;
}

这是追踪追踪

 E/AndroidRuntime(10555): FATAL EXCEPTION: main
 E/AndroidRuntime(10555): java.lang.OutOfMemoryError
 E/AndroidRuntime(10555):   at android.graphics.Bitmap.nativeCreate(Native Method)
 E/AndroidRuntime(10555):   at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
 E/AndroidRuntime(10555):   at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
 E/AndroidRuntime(10555):   at com.blsk.bigtoss.RoundedImageView.getCroppedBitmap(RoundedImageView.java:68)
 E/AndroidRuntime(10555):   at com.blsk.bigtoss.RoundedImageView.onDraw(RoundedImageView.java:55)
 E/AndroidRuntime(10555):   at android.view.View.draw(View.java:13759)
 E/AndroidRuntime(10555):   at android.view.View.getDisplayList(View.java:12710)
 E/AndroidRuntime(10555):   at android.view.View.getDisplayList(View.java:12754)
 E/AndroidRuntime(10555):   at android.view.View.draw(View.java:13483)
 E/AndroidRuntime(10555):   at android.view.ViewGroup.drawChild(ViewGroup.java:3169) 
 E/AndroidRuntime(10555):   at android.view.ViewGroup.dispatchDraw(ViewGroup.ja

注意:面对错误这两行

Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),Config.ARGB_8888);  and this

    Bitmap roundBitmap = getCroppedBitmap(bitmap, w);

1 个答案:

答案 0 :(得分:2)

我怀疑您正在创建和分配太多位图对象到您的堆

您可以仅使用一个将重复使用的位图对象来优化它

@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();

    if (drawable == null) {
        return;
    }

    if (getWidth() == 0 || getHeight() == 0) {
        return;
    }
    Bitmap bitmapCommon = ((BitmapDrawable) drawable).getBitmap();
    if (bitmapCommon == null) {
        Log.d("null", "yes");
    } else {
        Log.d("not null", "yes");
    }
     bitmapCommon = bitmapCommon.copy(Bitmap.Config.ARGB_8888, true);

    int w = getWidth(), h = getHeight();
    bitmapCommon = getCroppedBitmap(bitmapCommon, w);

    canvas.drawBitmap(bitmapCommon, 0, 0, null);

}

同样可以优化getCroppedBitmap()