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);

Lukap
  • 31,523
  • 64
  • 157
  • 244
  • 1
    I thought blinking text had been forbidden a long time ago! – Jean-François Corbett Sep 23 '11 at 09:55
  • it is not the point that the text will blink of some other action will go on, the point is the patter, the way of coding things..., that is the part I do not understand, I do not know what is the right way to do this kind of scenarios – Lukap Sep 23 '11 at 10:24

1 Answers1

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
}
Bob Roberts
  • 398
  • 2
  • 6
  • but the problem is I have a sequence of 2 images, and those two should be swapping for unknown time, that's until someone press another button – Lukap Sep 23 '11 at 12:32
  • @Lukap: stopping and starting the thread is explained in this answer: http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android . – Bob Roberts Sep 23 '11 at 13:24