创建位图时引发OutOfMemoryError



我有一个带LCD屏幕的自定义Android设备。它可以绘制位图。我从Android视图中准备它们。

private Bitmap loadBitmapFromView() {
if ((mView.getVisibility() != VISIBLE) ||
(mView.getMeasuredWidth() <= 0) ||
(mView.getMeasuredHeight() <= 0)) return null;
Bitmap b;
if ((mView instanceof ImageView) && !(mView instanceof ImageButton)) {
BitmapDrawable drawable = ((BitmapDrawable) ((ImageView) mView).getDrawable());
if (drawable == null) return null;
b = Bitmap.createScaledBitmap(drawable.getBitmap().copy(drawable.getBitmap().getConfig(), true),
mView.getMeasuredWidth(), mView.getMeasuredHeight(), false);
} else {
b = Bitmap.createBitmap(mView.getMeasuredWidth(), mView.getMeasuredHeight(), ARGB_8888);
}
replaceColor(b, ColorChannel.ALPHA, 0, mBackgroundColor);
Canvas c = new Canvas(b);
mView.layout(mLocation.x - mParentLocation.x, mLocation.y - mParentLocation.y,
mLocation.x - mParentLocation.x + mView.getMeasuredWidth(),
mLocation.y - mParentLocation.y + mView.getMeasuredHeight());
mView.draw(c);
return b;
}

此处使用ARGB_8888是因为某些视图具有透明度。

使用一段时间后,它会停止绘制。

我将android:largeHeap="true"android:hardwareAccelerated="false"添加到Manifest中,得到OOM

12-17 11:19:32.591 15169-15169/com.appcard.androidterminal E/art: Throwing OutOfMemoryError "Failed to allocate a 614412 byte allocation with 4194304 free bytes and 244MB until OOM"
12-17 11:19:32.619 15169-15169/com.appcard.androidterminal E/Surface: dequeueBuffer failed (Function not implemented)
12-17 11:19:32.620 15169-15169/com.appcard.androidterminal E/ViewRootImpl: Could not lock surface
java.lang.IllegalArgumentException
at android.view.Surface.nativeLockCanvas(Native Method)
at android.view.Surface.lockCanvas(Surface.java:264)
at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2998)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2966)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2753)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2367)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5643)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我试图在绘制后回收位图,但无济于事。

这是因为内存限制。您可以加载位图的缩小版本。

请参阅此处的文档,了解有关如何有效加载位图的更多信息

https://developer.android.com/topic/performance/graphics/load-bitmap

我通过用Glide替换imageView.setImageBitmap(bitmap)imageView.setImageResource(imageRes)解决了OutOfMemory问题:https://bumptech.github.io/glide/doc/targets.html

尝试使用

Bitmap bitmap = Bitmap.createScaledBitmap(view.getDrawingCache(), width, height, false);

这里的视图是您想要绘制的视图,宽度和高度是输出的宽度和高度。

如果问题仍然存在,你可以将位图的大小缩小2倍左右。其次,如果你正在处理多个位图,你可以逐个处理,这样内存中就只有一到两个真正需要的位图。

相关内容

  • 没有找到相关文章

最新更新