-1

The code is for making a text color change evenly every 200 ms. Why is the first version change uneven/flickering, and the second one evenly changes?

               //first version
                long lt = System.currentTimeMillis(); 
                TextView tv = ...   
                 for (int i = 1; i < 120; i++) {
                final int cl = i % 2 == 0 ? 0xFFFF0000 : 0x00000000;
                Message w = Message.obtain(handler, new Runnable() {
                    public void run() {
                        tv.setTextColor(cl);
                        tv.requestLayout();

                    }
                });

                handler.sendMessageDelayed(w,i * 200L - (System.currentTimeMillis()-lt));

            }

                 //second version
            Animation anim = new AlphaAnimation(0.0f, 1.0f);
            anim.setDuration(200); //You can manage the time of the blink with this parameter
            anim.setStartOffset(20);
            anim.setRepeatMode(Animation.RESTART);
            anim.setRepeatCount(Animation.INFINITE);
            tv.startAnimation(anim);
Sam Adamsh
  • 3,163
  • 6
  • 28
  • 50
  • 2
    Most likely because an `Animation` is stored, and the system knows it's going to redraw it several times, whereas a `Message` is much heavier duty and not designed for something like this. – Codeman May 24 '13 at 21:32

1 Answers1

1

I have had this problem before when creating/using animations. After you are done with your animation, simply call clearAnimation().

This will insure that it has fully stopped and should be nice and smooth, giving the user the experience you desire.

How to stop an animation (cancel() does not work)

Read more:

http://developer.android.com/reference/android/view/View.html

Regards,

Community
  • 1
  • 1
Jared Burrows
  • 50,718
  • 22
  • 143
  • 180