有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java画布。drawBitmap()并释放内存

我有一个ListView,列表中的项目是自定义的View。类View有一个字段

private Bitmap bitmap;

ViewonDraw方法使用

canvas.drawBitmap(bitmap, matrix, paint);

问题是我正在获取OutOfMemoryError。我想做的是在项目从屏幕上滚动时释放与Bitmap相关的内存。我想我可以写bitmap = null来允许垃圾收集器接收Bitmap,但我担心canvas.drawBitmap可能意味着这不起作用。canvas.drawBitmap是否保留对Bitmap的引用?或者调用canvas.drawBitmap是否意味着Canvas对象本身拥有与Bitmap相同的内存?问题是我真的不明白什么是Canvas,以及各种draw调用实际上做了什么。谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible.

    但是,如果调用recycle()并稍后尝试绘制位图,则会出现错误:“画布:尝试使用回收的位图”

    但是您需要对the android's site training on Managing Bitmap Memory进行一些广泛的阅读,因为根据您使用的API,您将有不同的策略

    看看Displaying Bitmaps Efficiently 的整个指南。它包含降低内存使用率所需的所有功能:

    如果您遵循这些指导原则,您应该能够消除内存泄漏并改善用户体验

    Does canvas.drawBitmap hold onto a reference to the Bitmap? Or does the call canvas.drawBitmap mean that the Canvas object itself has as much memory as the Bitmap?

    画布在您对位图进行措辞时保存对位图的引用

    画布可以作为绘制图形的实际表面的伪装或接口,它保存所有“绘制”调用。通过画布,绘图实际上是在放置在窗口中的基础位图上执行的。画布将绘制到定义的位图上