2

I have written this code on render() method. Now, I want to make the text blink, but it is not working

canvas.drawText(blinkText, 10, 10, paint);
if (blink == true) {
    canvas.drawText(blinkText, 10, 10, paint);
} else {
    canvas.drawText("Low Fuel", 10, 10, null);
}

and in update() method

if (blink == false) {
    blink = true;
} else {
    blink = false;
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • http://stackoverflow.com/questions/9294112/how-to-make-the-textview-blinking – Naufal Oct 31 '14 at 12:27
  • http://stackoverflow.com/questions/3450839/blinking-text-in-android-view – Naufal Oct 31 '14 at 12:28
  • http://www.coderzheaven.com/2012/09/13/create-blinking-text-android/ – Naufal Oct 31 '14 at 12:29
  • We do need some code ( when is update is called?). Simple said you need to instantiate a timer class and, for example, every 2 seconds the timer shots (=call update) and you have to invalidate your drawing area to draw again. – fklappan Oct 31 '14 at 12:29
  • thanks for reply but i want to draw the text using paint i am not using textview...! @ Naufal –  Oct 31 '14 at 12:31
  • can u send me code or comment i am not going to use textview? @ eftokay? –  Oct 31 '14 at 12:32
  • When do you call your update method? – fklappan Oct 31 '14 at 12:47
  • in render method.... after completion of if-else condition –  Oct 31 '14 at 12:48
  • Please edit your question and provide whole methods and some glue code ( when is what called etc ) so we can help you. Oh and by the way, you can refactor your update method to blink = !blink; – fklappan Oct 31 '14 at 12:54
  • private void doDrawRunning(Canvas canvas, Paint paint) { mBGFarMoveX = mBGFarMoveX - 1; mBGNearMoveX = mBGNearMoveX - 4; int newFarX = backGround.getWidth() - (-mBGFarMoveX); if (newFarX <= 0) { mBGFarMoveX = 0; canvas.drawBitmap(backGround, mBGFarMoveX, 0, null); canvas.drawText(blinkText, 10, 10, paint); } else { canvas.drawBitmap(backGround, mBGFarMoveX, 0, null); canvas.drawBitmap(backGround, newFarX, 0, null); canvas.drawText(blinkText, 10, 10, paint); } } –  Oct 31 '14 at 13:54

2 Answers2

2

If your update() method is called from a separate Thread repeatedly, it makes sense that you wouldn't see any changes because you're not only not invalidating the View, but could also be swapping the visibility too quickly to tell.

So first, let's declare a blink duration and a holder for our last update time: private static final int BLINK_DURATION = 350; // 350 ms private long lastUpdateTime = 0; private long blinkStart=0;

Now, assuming you're setting setting the Bitmap to be the src or background of the View (which you should be doing if you're animating on top of it), you should be able to draw the text over the View on its Canvas after the View's dispatchDraw(Canvas) call:

@Override
public void dispatchDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTextSize(40);
    paint.setColor(Color.RED);
    canvas.drawBitmap(back, width / 2 - back.getWidth() / 2, height / 2
            - back.getHeight() / 2, null);
    if (blink)
        canvas.drawText(blinkText, back.getWidth() / 2,
                back.getHeight() / 2, paint);
}

Note: generally you would use onDraw(Canvas), but in this situation the View could be a layout with a background and children, in which case we use dispatchDraw(Canvas) to make sure we actually draw above all the View's children.

Now that we have our drawing method setup, let's adjust your update method to only flip once the duration has passed, and then actually invalidate the View:

public void update() {
    if (System.currentTimeMillis() - lastUpdateTime >= BLINK_DURATION
            && !blink) {
        blink = true;
        blinkStart = System.currentTimeMillis();
    }
    if (System.currentTimeMillis() - blinkStart >= 150 && blink) {
        blink = false;
        lastUpdateTime = System.currentTimeMillis();
    }

}

Now it is working fine

jgritty
  • 11,660
  • 3
  • 38
  • 60
Cruceo
  • 6,763
  • 2
  • 31
  • 52
1
if(System.currentTimeMillis() - blinkStart >= 150 && blink) {
    blink = false;
    lastUpdateTime = System.currentTimeMillis();
}

you can use thread for blink