1

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);
4

1 回答 1

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()优化

于 2013-08-22T04:54:55.297 回答