0

I want to create a flashing effect by drawing a path with color grey, white (matching to the background), and then grey again. I want to flash 3 times, showing gray for 1 sec, white for 1 sec gray again for 1 sec, etc.

When I created a Handler for postDelayed(), the program skipped over the run() and did not execute it in the timing set, and no flashing occurred:

final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                invalidate(); //calls onDraw()
                Log.d(TAG, "Flashing now now");
            }
        }, 1000);

How would I implement such a flashing functionality with a timer and flash it 3 times?

Thanks!

Eddev
  • 890
  • 1
  • 10
  • 21

2 Answers2

2

You can try something like this,

int delay = 5000; // delay for 5 sec.

int period = 1000; // repeat every sec.

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {

public void run() {

System.out.println("done}

}, delay, period);

Otherwise you have plenty other examples to follow like this Example 1, Example 2, Example 3 etc. Let me know if you still need help.

Community
  • 1
  • 1
JNI_OnLoad
  • 5,472
  • 4
  • 35
  • 60
0

This is a working code example of how we flash a globe from blue to red and back again. You can modify the code on the inside to limit how many times and what time delay you want.

protected MyGlobeFlasherHandler handlerFlashGlobe = new MyGlobeFlasherHandler(this);

@Override
protected onCreate(Bundle bundle) { 
       handlerFlashGlobe.sendEmptyMessageDelayed(0, 700);
}

/**
 * private static handler so there are no leaked activities.
 */
protected static class MyGlobeFlasherHandler extends Handler {


        private final WeakReference<HomeBase> activity;

        public MyGlobeFlasherHandler(HomeBase activity) {
            this.activity = new WeakReference<HomeBase>(activity);
        }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

        if (activity.get() != null) {
            if (activity.get().shadedGlobe) {
                activity.get().imgData.setImageDrawable(activity.get().getResources().getDrawable(R.drawable.globe_blue));
            } else {
                activity.get().imgData.setImageDrawable(activity.get().getResources().getDrawable(R.drawable.globe_red));
            }

            activity.get().shadedGlobe = !activity.get().shadedGlobe;

            sendEmptyMessageDelayed(0, 700);
        }
    }

}
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • Hi Kristy, here you are animating by using two distinct images. That isn't very good programming practice; ideally, you would want to set a .png file and programmatically change the color by perhaps using `setIndent()`. – Eddev Apr 21 '16 at 21:51
  • @Eddev Can you tell me what you mean by "bad programming"? Also, the image is a photo and I can't imagine changing the color prgrammatically. – Kristy Welsh Apr 22 '16 at 20:50