7

我无法在 Canvas 中实现平滑非常缓慢的文本动画,因为Canvas.drawText不想在“像素之间”绘制。例如,给定 4 个连续帧,其中我绘制 Y 偏移量为 0、0.5、1、1.5 的文本,画布实际上将分别在偏移量 0、0、1、1 处绘制它,这导致动画为“生涩”。有一个名为Paint.SUBPIXEL_TEXT_FLAG应该保持浮点精度的标志。

我找到了一个相关线程,其中 Romain Guy 说 Android 目前不支持此标志:Android中某些 Paint 常量的含义

我的问题是:是否有任何现有的解决方法?

注意:在另一个位图中绘制一次文本,然后用浮点偏移量绘制这个位图而不是绘制文本似乎也不起作用。

4

1 回答 1

3

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);
    }
}
于 2012-09-14T09:28:26.517 回答