开发者

What is the right way of making of blinking text

I have a textview and I have many buttons. One of the button should be able to start the 'anima开发者_开发问答tion' 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);


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜