Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A smooth very slow text animation in an Android canvas, SUBPIXEL_TEXT_FLAG not working

I can't get to achieve a smooth very slow text animation in a Canvas because Canvas.drawText doesn't want to draw "between pixels". For example, given 4 consecutive frames where I draw a text with an Y offset of 0, 0.5, 1, 1.5, the canvas will actually draw it at offsets 0, 0, 1, 1, respectively, which causes the animation to be "jerky". There's a flag called Paint.SUBPIXEL_TEXT_FLAG that's supposed to keep the float precision.

I found a related thread in which Romain Guy said that this flag is currently not supported in Android: Meaning of some Paint constants in Android.

My question is: is there any existing workaround ?

Note: Drawing the text in another bitmap once, then draw this bitmap with float offsets instead of drawing the text doesn't seem to work either.

like image 828
Joan Avatar asked Sep 14 '12 08:09

Joan


1 Answers

You could simulate this effect by drawing two texts side-by-side with alpha balancing (between 127 and 255) between the two elements.

Let's assume your text is moving from up to bottom, and the current vertical position is 10.28. You just have to draw one text at position 10 with an alpha near of 127 and the other text at position 11 with an alpha near of 255.

Here is a little (ugly :D) example :

private void doDraw(Canvas canvas) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);
    paint.setTextSize(20);
    canvas.drawRect(0, 0, getWidth(), getHeight(), paint);

    mY += 0.05f;
    paint.setColor(Color.RED);

    if (Math.floor(mY) == mY) {
        canvas.drawText("test", mX, mY, paint);
    } else {
        float mY1 = (float) Math.floor(mY);
        float mY2 = mY1 + 1;
        float delta = mY - mY1;

        paint.setAlpha((int) ((1 - delta) * 127) + 127);
        canvas.drawText("test", mX, mY1, paint);

        paint.setAlpha((int) ((delta) * 127) + 127);
        canvas.drawText("test", mX, mY2, paint);
    }
}
like image 189
DayS Avatar answered Nov 11 '22 02:11

DayS