制作闪烁文本的正确方法是什么

发布于 2024-12-05 20:28:06 字数 1416 浏览 0 评论 0原文

我有一个文本视图,有很多按钮。其中一个按钮应该能够很好地启动“动画”,它不是动画中的动画,它只需要使文本变红 2 秒,然后文本应该变绿 2 秒,然后再变回红色。 .. -其中一个按钮应停止“动画”并将文本设置为白色 -一个应该使文本恢复 1 秒,而不是蓝色 2 秒,然后再次恢复为黑色......

重点是按钮应该能够由用户随时按下。

我想我应该使用 Handler 但我不确定模式,我不知道线程的停止应该是什么样子,我的意思是当我启动线程时,稍后我应该告诉他停止......什么是最好的方法来做到这一点?

我总是用愚蠢的技巧来编写这种想法,我不知道模式是什么,正确的方法是什么?

谢谢

这里是我如何做到这一点的一些代码,但我觉得这不是正确的方式

 private boolean flagForStop=true;
    private Handler handler1=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case 0:
                flagForStop=false;
            case 1:
                flagForStop=true;
              break;
            case 2:
                new Thread(){
                    public void run(){
                        while(true){
                            if(flagForStop)break;
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            //do something
                        }
                    }
                }.start();
                break;
            default:
                break;
            }
        }

    };

,而不是我的听众类似 handler1.sendEmptyMessage(0);

I have a textview and I have many buttons. One of the button should be able to start the 'animation' well it is not animation in anim, it just need to make the text go red for a 2 seconds than the text should go green for 2 seconds than again back to red....
-one of the buttons should stop the 'animation' and set the text to white
-one should make the text back for 1 sec, than blue for 2sec and again back to black...

the point is the buttons should be able to be pressed by the user in any time.

I think I should use Handler but I am not sure for the patter , I do not know how the stoping of the thread should look like, I mean when I start thread , later on I should tell him to stop... What is the best way to do this ?

I always code this kind of thinks with stupid tricks , and I do not know what is the pattern, what is the right way to do this ?

Thanks

here is some code of how I do it, but I feel that this is not the right way

 private boolean flagForStop=true;
    private Handler handler1=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case 0:
                flagForStop=false;
            case 1:
                flagForStop=true;
              break;
            case 2:
                new Thread(){
                    public void run(){
                        while(true){
                            if(flagForStop)break;
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            //do something
                        }
                    }
                }.start();
                break;
            default:
                break;
            }
        }

    };

and than i the listener something like
handler1.sendEmptyMessage(0);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

束缚m 2024-12-12 20:28:06

这是我的做法,它并不完美,但它应该适用于您上面提到的所有类型的动画。

首先创建一个名为AnimStep的类,其中包含两个字段:时间颜色。构造函数是 AnimStep(int time, String color)

然后创建一个名为 AnimSequence 的类,其中包含 AnimStep 对象的数组。您可以使用 this.getStep(index) 获取特定步骤。

例如,“粉红 2 秒,洋红色 3 秒,然后永远黑色”的 AnimSequence 将包含以下 AnimStep 数组:{new AnimStep(0, "pink"), new AnimStep(2, "magenta" ), new AnimStep(5, "black")}

然后创建一个名为 Animation 的类,该类在单独的线程中永久运行并定期唤醒(例如每 100 毫秒)。该类具有三个字段:

  • sequence:指向 AnimSequence 对象的指针
  • startTime:时间戳
  • step:表示元素索引的整数在 AnimStep 数组中,

当您单击按钮时,您将 AnimSequence 传递给 Animation。这会将 this.sequence 设置为给定的 AnimSequence、this.startTime = 、 this.step = 1。 它还将文本的颜色设置为 AnimSequence 中第一个 AnimStep 的颜色。

现在,每次动画唤醒时,它都会执行以下操作:

if (this.step >= this.sequence.size()) return // do nothing
currentStep = this.sequence.getStep(this.step)
elapsedTime = <current time stamp for now> - this.time
if (elapsedTime >= currentStep.time) {
  this.step++
  yourtext.color = currentStep.color
}

Here is how I would do it, it's not perfect but it should work for all the types of animations you've mentioned above.

First create a class called AnimStep which contains two fields: time and color. Constructor is AnimStep(int time, String color)

Then create a class called AnimSequence which contains an array of AnimStep objects. You get a specific step with this.getStep(index).

For instance AnimSequence for "pink 2 seconds then magenta 3 seconds then black forever" would contain the following array of AnimSteps: {new AnimStep(0, "pink"), new AnimStep(2, "magenta"), new AnimStep(5, "black")}

Then create a class called Animation which runs permanently in a separate thread and wakes up regularly (e.g. every 100ms). This class has three fields:

  • sequence: a pointer to an AnimSequence object
  • startTime: a timestamp
  • step: an integer which represents the index of an element in an array of AnimStep

When you click a button, you pass an AnimSequence to Animation. This sets this.sequence to the given AnimSequence, this.startTime = , this.step = 1. It also sets the color of the text to the color of you first AnimStep in AnimSequence.

Now each time the Animation wakes up, it does the following:

if (this.step >= this.sequence.size()) return // do nothing
currentStep = this.sequence.getStep(this.step)
elapsedTime = <current time stamp for now> - this.time
if (elapsedTime >= currentStep.time) {
  this.step++
  yourtext.color = currentStep.color
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文