我的UI线程不睡觉正常

我的UI线程不睡觉正常

本文介绍了我的UI线程不睡觉正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个拼图15(http://en.wikipedia.org/wiki/Fifteen_puzzle)的比赛,我有用户的活动来选择背景图像,然后我的形象传递到一个新的活动以规模和裁剪。

i'm making a 15 puzzle (http://en.wikipedia.org/wiki/Fifteen_puzzle) game, and i have an activity for user to select the background image, and then i pass that image to a new activity in order to scale and crop it.

现在我想先为3秒,然后它会洗牌显示解决方案给用户,我使用code是这样的:

Now i want to show the solution to the user first for 3 secs then it would shuffle, i'm using code like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //...skip the code that gets the image and scales it

    start();
}

然后在我的start()funcion:

then in my start() funcion:

public void start() {

    //the createPuzzle function would create all the Views(tiles)
    //and add them to the root LinearLayout using addView() function
    createPuzzle(game.getBoardConfig(), dimension);

    //i was trying to sleep here

    shuffle();
}

我用:

try {
    Thread.sleep(3000);
} catch (InterruptedException e) {}

SystemClock.sleep(3000);

但他们都不正确工作,他们都停顿了一下之后,我选择了图像的线程,当它被暂停,我不能看到新的活动,我创建了瓷砖。当线程恢复,它已经显示出了洗牌的难题。

but neither of them worked properly, they all paused the the thread right after i selected the image and when it was paused i can't see the new activity and the tiles i created. When the thread resumes, it was already showing the shuffled puzzle.

我一直在寻找在相当长一段时间的文件,但仍无法找出什么是错我的code,感谢您的帮助!

I've been looking at the documentation for quite a while but still can't figure out what's wrong with my code, thanks for helping!

推荐答案

不要让UI线程睡眠,因为这将锁定整个UI线程这是一个禁忌。相反,使用处理后可运行的延迟......像

Don't make the UI thread sleep because that will lock up the entire UI thread which is a no-no. Instead, use a Handler to post a runnable with a delay...like

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
         public void run() {
              shuffle();
         }
    }, 3000);

这篇关于我的UI线程不睡觉正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:02