如何使用LayerDrawable解决OutOfMemoryError

时间:2012-09-30 10:50:19

标签: android bitmap drawable out-of-memory

错误:

java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:775)
at android.content.res.Resources.loadDrawable(Resources.java:1968)
at android.content.res.Resources.getDrawable(Resources.java:677)
at android.widget.ImageView.resolveUri(ImageView.java:542)
at android.widget.ImageView.setImageResource(ImageView.java:315)
at app.Main.GridActivity.initaliseLevel(GridActivity.java:245)
at app.Main.GridActivity.load_player_settings(GridActivity.java:180)
at app.Main.GridActivity.onResume(GridActivity.java:79)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1158)
at android.app.Activity.performResume(Activity.java:4607)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2448)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2486)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2000)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)

图像由以下设置:

ImageButton b0 = (ImageButton) findViewById(R.id.ImageButton00);
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = r.getDrawable(R.drawable.image);
layers[1] = r.getDrawable(R.drawable.imagetwo);
LayerDrawable layerDrawable = new LayerDrawable(layers);
b0.setImageDrawable(layerDrawable);

通过阅读其他答案,我发现图像需要在使用后回收。这是最好的选择吗?如果是这样,如何将LayerDrawable转换为位图,以便可以回收?

1 个答案:

答案 0 :(得分:2)

在你的清单>应用程序属性部分有一个“大堆”设置,在应用程序节点中选择您的活动,然后在堆设置中选择“真”,这对我有帮助,直到我解决了泄漏。

并且我在我的活动中也有这个:

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

    @Override
protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.nameslayout));
    System.gc();
}

跳跃帮助