简体   繁体   English

如何在不阻止UI的情况下在屏幕上显示值,并使其在Android活动中以样式淡出?

[英]How can I show a value on the screen without blocking the UI and make it fade out with style in android activity?

I'm making a word game for android, and basically whenever a user enters something right I'm updating the score in the application, and I want to show the score on the screen to make it show in big then fade out slowly and get smaller, how can I do that? 我正在为Android制作文字游戏,基本上,每当用户输入正确的内容时,我都会在应用程序中更新分数,并且我想在屏幕上显示分数以使其大幅度显示,然后逐渐淡出并得到较小,我该怎么办? and is it possible to implement it in an AsyncTask class? 并且可以在AsyncTask类中实现它吗? This is the method I'm using to check if the word entered is right. 这是我用来检查输入的单词是否正确的方法。

    public class checkWord extends AsyncTask<String, String, Void> {

    private String c;

    @Override
    protected Void doInBackground(String... arg0) {
        int size = correctWords.size();
        String word = arg0[0];

        for (int i = 0; i < size; i++) {
            if (word.equalsIgnoreCase(correctWords.get(i))) {
                publishProgress("bad");
            }
        }

        try {
            c = bufferedReader.readLine();
            while (c != null && !c.equalsIgnoreCase(word)) {

                c = bufferedReader.readLine();
            }

            if (c != null) {

                correctWords.add(0, word);
                score += word.length();
                publishProgress("good");

            } else {
                incorrectWords.add(0, word);
                publishProgress("bad");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        closeWordFile();
        openWordFile();
        return null;
    }

So is there anyway I could pass a param to the publishProgress so that in onProgressUpdate I draw the score they got, for example +3 then make it fade out? 因此,无论如何,我可以将参数传递给publishProgress,以便在onProgressUpdate中绘制得出的分数,例如+3然后使其淡出? This is my onProgressUpdate where I add seconds to the timer and play a sound if it's a valid word or not 这是我的onProgressUpdate,在其中我将秒数添加到计时器并播放声音(如果不是有效字词的话)

         @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        if (values[0].matches("bad"))
            failureSound.start();
        if (values[0].matches("good")) {
            successSound.start();
            if (countDownTimer != null) {
                countDownTimer.cancel();
                startTimer(countDownTime + c.length() / 2);
            }
        }
    }

Because of Single threaded model in Android, only main thread can update UI, You can try the same in 由于Android中存在单线程模型,因此只有主线程才能更新UI,您可以在

 runOnUiThread(new Runnable()
{
    public void run() 
    {}
}

method. 方法。

Instead use a toast message. 而是使用祝酒消息。 It will not affect your UI. 它不会影响您的用户界面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM