Question

I have my class implements Drawable. I need to redraw it periodicaly to get some "blink" effect. I am using timer.schedule to schedule invalidateSelf() but nothing happend.

private class DrawableImpl extends Drawable {
    private boolean blinkFlag = false;
    private Timer timer = new Timer(false);

    private int maxFlashCount = 21;
    private int intervalBetweenFlashesInMs = 100;
    private int currentFlashNumber = 0;

    @Override
    public void draw(Canvas canvas) {
        Log.i(TAG, "draw");

        /*draw stable part*/

        if (blinkFlag ) {
            Log.i(TAG, "blink");
            /*draw bliking part*/
        }

        blinkFlag = !blinkFlag;

        final DrawableImpl drawableImpl = this; 
        if (currentFlashNumber < maxFlashCount) {
            Log.i(TAG, "schedule");
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.i(TAG, "run from back");
                            drawableImpl.invalidateSelf();
                        }
                    });
                }
            }, intervalBetweenFlashesInMs);
            currentFlashNumber++;
        }
    }
}

In Log something like this
02-08 23:07:44.791: INFO/(258): draw
02-08 23:07:44.791: INFO/(258): blink
02-08 23:07:44.791: INFO/(258): schedule
02-08 23:07:45.011: INFO/(258): run from back
02-08 23:07:45.021: INFO/(258): draw
02-08 23:07:45.021: INFO/(258): schedule
02-08 23:07:45.171: INFO/(258): run from back
02-08 23:07:45.171: INFO/(258): draw
02-08 23:07:45.171: INFO/(258): blink
02-08 23:07:45.171: INFO/(258): schedule
02-08 23:07:45.331: INFO/(258): run from back

Why it didn`t work? Should I use another methods for that?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top