我想创建一个画布上的游戏动画的Android中的绘画和更新循环,但我有麻烦,因为我似乎无法让线程正常工作。它似乎几乎立即崩溃。这是我曾尝试过的:如何实现paint-update循环?
// Create the thread supplying it with the runnable object
Thread thread = new Thread(runnable);
// Start the thread in oncreate()
thread.start();
class runner implements Runnable {
// This method is called when the thread runs
long wait = 1000;
public void run() {
update();
}
public void update()
{
try{
Thread.currentThread();
//do what you want to do before sleeping
Thread.sleep(wait);//sleep for 1000 ms
//do what you want to do after sleeping
}
catch(InterruptedException ie){
//If this thread was interrupted by another thread
}
run();
}
}
此外,当我等待下降更低崩溃更快。
是否有更合适的方法来解决这个问题?
更改为此:
class runner implements Runnable {
// This method is called when the thread runs
long wait = 10;
boolean blocked = false;
public void run() {
if(!blocked){
blocked = true;
paint();
}
}
public void paint()
{
update();
}
public void update()
{
try{
Thread.currentThread();
//do what you want to do before sleeping
Thread.sleep(wait);//sleep for 1000 ms
//do what you want to do after sleeping
}
catch(InterruptedException ie){
//If this thread was interrupted by another thread
}
paint();
}
}
这导致了同样的错误...:/
你可以发布日志 – Vinay 2010-09-08 15:45:36
我如何获得上述日志? – tylercomp 2010-09-08 15:49:14
如果您使用的是eclipse,请打开ddms或logcat窗口。如果您不使用eclipse,请从命令行运行adb logcat(或Windows上的adb.exe logcat)。 adb位于tools文件夹中。 – 2010-09-08 16:03:18