gpt4 book ai didi

android - 在创建位图时抛出 OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-29 00:57:11 26 4
gpt4 key购买 nike

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

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 是因为一些 View 具有透明度。

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

我在 Manifest 中添加了 android:largeHeap="true"android:hardwareAccelerated="false" 并得到了 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

关于android - 在创建位图时抛出 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53812472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com