Canvas和surfaceView例如崩溃/冻结 - 内存泄漏? [英] Canvas and surfaceView example crash/freeze - Memory Leak?

查看:1221
本文介绍了Canvas和surfaceView例如崩溃/冻结 - 内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP://www.helloandroid。 COM /教程/如何用帆布 - 您 - Android的应用程序部分-1

在本教程链接源$ C ​​$ C下载结束时可用。 我下载了code和尝试这个例子中,它吸引了袋鼠在屏幕和1-2分钟之内,我收到了崩溃/应用僵住了。

At the end of this tutorial link for source code download is available. I downloaded the code and tried this example , It draws a kangaroo in the screen and within 1-2 minutes i get a crash/the application froze.

我试了爱可视70互联网平板电脑。

I tried on Archos 70 Internet Tablet.

我想知道原因,或者某些事情是错误的这种

下面是 LogCat中

04-13 17:03:24.089:DEBUG /的OnDraw(2070):lefutott
04-13 17:03:24.097:DEBUG /的OnDraw(2070):lefutott
04-13 17:03:24.113:DEBUG /的OnDraw(2070):lefutott
04-13 17:03:24.128:DEBUG /的OnDraw(2070):lefutott
04-13 17:03:24.136:DEBUG /的OnDraw(2070):lefutott
04-13 17:03:24.152:DEBUG /的OnDraw(2070):lefutott
04-13 17:03:24.167:DEBUG /的OnDraw(2070):lefutott
04-13 17:03:24.175:DEBUG /的OnDraw(2070):lefutott
04-13 17:04:10.019:WARN / ActivityManager(1302):广播超时BroadcastRecord {457f99c8 android.intent.action.TIME_TICK} - receiver=android.app.ActivityThread$PackageInfo$ReceiverDispatcher$InnerReceiver@4561a5c8
04-13 17:04:10.019:WARN / ActivityManager(1302):暂停时接收器:BroadcastFilter {454ed4c8 ReceiverList {454fbd70 1302系统/ 1000本地:4561a5c8}}
04-13 17:04:38.972:INFO /过程(1302):发送信号。 PID:1302 SIG:3
04-13 17:04:38.972:信息/ dalvikvm(1302):主题ID = 3:反应信号3
04-13 17:04:39.097:信息/ dalvikvm(1302):写堆栈跟踪到/data/anr/traces.txt
04-13 17:05:09.097:INFO /过程(1302):发送信号。 PID:1302 SIG:3
04-13 17:05:09.097:信息/ dalvikvm(1302):主题ID = 3:反应以信号3
04-13 17:05:09.128:信息/ dalvikvm(1302):写堆栈跟踪到/data/anr/traces.txt
04-13 17:05:09.128:INFO /过程(1302):发送信号。 PID:1433 SIG:3
04-13 17:05:09.128:信息/ dalvikvm(1433):主题ID = 3:反应信号3
04-13 17:05:09.144:信息/ dalvikvm(1433):写堆栈跟踪到/data/anr/traces.txt
04-13 17:05:11.144:信息/ Watchdog_N(1302):dumpKernelStacks
04-13 17:05:11.144:ERROR / Watchdog_N(1302):无法打开堆TID 1302:13(权限被拒绝)
04-13 17:05:11.144:ERROR / Watchdog_N(1302):无法打开堆TID 1303:13(权限被拒绝)

04-13 17:03:24.089: DEBUG/ondraw(2070): lefutott
04-13 17:03:24.097: DEBUG/ondraw(2070): lefutott
04-13 17:03:24.113: DEBUG/ondraw(2070): lefutott
04-13 17:03:24.128: DEBUG/ondraw(2070): lefutott
04-13 17:03:24.136: DEBUG/ondraw(2070): lefutott
04-13 17:03:24.152: DEBUG/ondraw(2070): lefutott
04-13 17:03:24.167: DEBUG/ondraw(2070): lefutott
04-13 17:03:24.175: DEBUG/ondraw(2070): lefutott
04-13 17:04:10.019: WARN/ActivityManager(1302): Timeout of broadcast BroadcastRecord{457f99c8 android.intent.action.TIME_TICK} - receiver=android.app.ActivityThread$PackageInfo$ReceiverDispatcher$InnerReceiver@4561a5c8
04-13 17:04:10.019: WARN/ActivityManager(1302): Receiver during timeout: BroadcastFilter{454ed4c8 ReceiverList{454fbd70 1302 system/1000 local:4561a5c8}}
04-13 17:04:38.972: INFO/Process(1302): Sending signal. PID: 1302 SIG: 3
04-13 17:04:38.972: INFO/dalvikvm(1302): threadid=3: reacting to signal 3
04-13 17:04:39.097: INFO/dalvikvm(1302): Wrote stack traces to '/data/anr/traces.txt'
04-13 17:05:09.097: INFO/Process(1302): Sending signal. PID: 1302 SIG: 3
04-13 17:05:09.097: INFO/dalvikvm(1302): threadid=3: reacting to signal 3
04-13 17:05:09.128: INFO/dalvikvm(1302): Wrote stack traces to '/data/anr/traces.txt'
04-13 17:05:09.128: INFO/Process(1302): Sending signal. PID: 1433 SIG: 3
04-13 17:05:09.128: INFO/dalvikvm(1433): threadid=3: reacting to signal 3
04-13 17:05:09.144: INFO/dalvikvm(1433): Wrote stack traces to '/data/anr/traces.txt'
04-13 17:05:11.144: INFO/Watchdog_N(1302): dumpKernelStacks
04-13 17:05:11.144: ERROR/Watchdog_N(1302): Unable to open stack of tid 1302 : 13 (Permission denied)
04-13 17:05:11.144: ERROR/Watchdog_N(1302): Unable to open stack of tid 1303 : 13 (Permission denied)

推荐答案

在OnDraw中看起来是这样的:

The onDraw looks like this:

    @Override
    public void onDraw(Canvas canvas) {

            Paint paint = new Paint();


            Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                            R.drawable.kangoo);
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(kangoo, 10, 10, null);
    }

所以,每次运行时,它创建一个新的油漆和可能(我没有用的BitmapFactory),创建一个新的位图。这似乎有点小题大做(尤其是,因为油漆没有使用,但它似乎是在第2部分)。我会考虑将这些位的构造面板,看它是否有差别,是这样的:

So each time it runs, it's creating a new paint and possibly (I've not used the BitmapFactory), creating a new bitmap. This seems like overkill (particularly, since the paint isn't used, although it does seem to be in part 2). I'd consider moving these bits to the constructor for the panel to see if it makes a difference, something like:

public class Panel extends SurfaceView implements SurfaceHolder.Callback{
    // rest of class ignored for brevity

    private Paint paint;
    private Bitmap kangoo;

    public Panel(Context context, AttributeSet attrs) {
    super(context, attrs); 
    getHolder().addCallback(this);
    canvasthread = new CanvasThread(getHolder(), this);
    setFocusable(true);
         paint = new Paint();


         kangoo = BitmapFactory.decodeResource(getResources(),
                            R.drawable.kangoo);
}

    @Override
    public void onDraw(Canvas canvas) {                
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(kangoo, 10, 10, null);
    }
}

这也可能是值得在模拟器中运行它,看,如果你得到了同样的问题,或者是特定的设备。

It may also be worth running it in the emulator to see if you get the same problem, or if it is device specific.

编辑: 另外值得一提的是,在CanvasThread类的run()方法没有任何睡在里面,所以它的运行速度,因为它可以,刷新屏幕。这可能是正常的教程,但在真正的应用程序我希望某种目标帧速率的run方法适当的睡眠,否则就好像你会使用非常多的CPU周期(和$ P $的psumably电池),用户可能不会注意到。

It's also worth noting that the run() method in the CanvasThread class doesn't have any sleeps in it, so it's running as fast as it can, redrawing the screen. This is probably ok for a tutorial, but in a real app I would expect some kind of target frame rate with an appropriate sleep in the run method, otherwise it seems like you would be using an awful lot of cpu cycles (and presumably battery) that the user probably isn't going to notice.

这篇关于Canvas和surfaceView例如崩溃/冻结 - 内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆