gpt4 book ai didi

android - 如何实现绘画更新循环?

转载 作者:行者123 更新时间:2023-11-29 15:33:06 25 4
gpt4 key购买 nike

我想在 android 中为 Canvas 上的游戏动画创建一个绘制和更新循环,但我遇到了麻烦,因为我似乎无法让线程正常工作。它似乎几乎立即崩溃。这是我尝试过的:

    // 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();
}

这会导致相同的错误...:/

最佳答案

我注意到的第一件事是运行调用更新和更新调用运行。这将导致 NO PUN INTENDED Stack Overflow。他们互相调用,直到堆栈填满。然后它应该崩溃。

关于android - 如何实现绘画更新循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3669459/

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