我的应用程序有一个包含一些 ImageButtons 的布局。此外,我的应用程序会呈现一些位图供以后使用。通过使用 Bitmap 参数创建一个 Canvas,然后在画布中绘制来呈现位图。此位图渲染任务发生在非 ui 线程中,因此 gui 可以在位图渲染时保持响应。
发生 ANR。原因似乎是 ImageButton.draw() 方法没有完成。此外,该方法似乎无法完成,因为位图渲染任务在 ImageButton 绘制时开始渲染。
以下是日志输出的一些代码片段,以及来自 ANR 的 traces.txt 文件。
我的结论是否可能/可能,在非 ui 线程中绘制到 Canvas 可能会导致 ImageButton.draw() 挂起?如果是这样,有什么建议可以解决这个问题吗?
private class BitmapRenderTask implements Runnable
{
...
public void run()
{
Log.e("start render task", rect.toString());
...
Canvas canvas = new Canvas(bitmap);
Log.e("created canvas", "created canvas");
//do stuff to draw into canvas
Log.e("drew to canvas", "drew to canvas");
...
Log.e("done, exit", rect.toString());
public class MyImageButton extends ImageButton
{
public void draw(Canvas canvas)
{
Log.e("start drawing", this.toString());
super.draw(canvas);
Log.e("end drawing", this.toString());
}
}
My log:
04-24 14:10:12.115: E/start render task(9446): 1 Rect(512, 0 - 800, 512)
04-24 14:10:12.230: E/start drawing(9446): MyButton1
04-24 14:10:12.230: E/end drawing(9446): MyButton1
04-24 14:10:12.230: E/start drawing(9446): MyButton2
04-24 14:10:12.230: E/end drawing(9446): MyButton2
04-24 14:10:12.230: E/start drawing(9446): MyButton3
04-24 14:10:12.230: E/end drawing(9446): MyButton3
04-24 14:10:12.230: E/start drawing(9446): MyButton4
04-24 14:10:12.235: E/end drawing(9446): MyButton4
04-24 14:10:12.235: E/start drawing(9446): MyButton5
04-24 14:10:12.235: E/end drawing(9446): MyButton5
04-24 14:10:12.235: E/start drawing(9446): MyButton6
04-24 14:10:12.390: E/drew to canvas(9446): drew to canvas
04-24 14:10:12.395: E/done, exit(9446): Rect(512, 0 - 800, 512)
04-24 14:10:12.395: E/start render task(9446): Rect(0, 1024 - 512, 1084)
04-24 14:10:12.410: E/drew to canvas(9446): drew to canvas
04-24 14:10:12.395: E/done, exit(9446): Rect(0, 1024 - 512, 1084)
痕迹.txt: