Android drawText在Lollipop上缺少字符

时间:2014-11-28 21:22:49

标签: android android-5.0-lollipop drawtext

我有一个显示比例尺的Android应用程序,它在所有版本上运行良好,直到5.0(Lollipop),其中使用drawText方法绘制的所有文本都以某种方式被截断。 仅显示两个字母文本的第二个字母 即第一个标记仅为'0'字母,但不显示 对于第二个2标记(40和80),仅显示0,其余为140 ... 250,不显示任何文本。
文本大小似乎没问题,但并不是所有的字符都显示出来。 图像在[0..1,0..1]方格中缩放 我在drawText上找到并尝试了很多帖子,但没有一个帮助 (View.setLayerType()所有组合,setLinearText(true),Paint.Style.FILL ...)

private void drawScale(final Canvas canvas) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    // On canvas, North is 0 degrees, East is 90 degrees, South is 180 etc.
    // We start the scale somewhere South-West so we need to first rotate the canvas.
    canvas.rotate(mScaleRotation, 0.5f, 0.5f);

    final int totalTicks = mDivisions * mSubdivisions + 1;
    for (int i = 0; i < totalTicks; i++) {
        final float y1 = mScaleRect.top;
        final float y2 = y1 + 0.015f; // height of division
        final float y3 = y1 + 0.045f; // height of subdivision

        final float value = getValueForTick(i);
        final Paint paint = getRangePaint(value);
        if (0 == value % mDivisions) {
            // Draw a division tick
            canvas.drawLine(0.5f, y1, 0.5f, y3, paint);
            // Draw the text 0.15 away from the division tick
            canvas.drawText(valueString(value), 0.5f, y3 + 0.045f, paint);
        }
        else {
            // Draw a subdivision tick
            canvas.drawLine(0.5f, y1, 0.5f, y2, paint);
        }
        canvas.rotate(mSubdivisionAngle, 0.5f, 0.5f);
    }
    canvas.restore();
}

其中范围绘制就像这样创建

mRangePaints[i] = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
mRangePaints[i].setColor(mRangeColors[i]);
mRangePaints[i].setStyle(Paint.Style.STROKE);
mRangePaints[i].setStrokeWidth(0.005f);
mRangePaints[i].setTextSize(0.05f);
mRangePaints[i].setTypeface(Typeface.SANS_SERIF);
mRangePaints[i].setTextAlign(Align.CENTER);
mRangePaints[i].setShadowLayer(0.005f, 0.002f, 0.002f, mTextShadowColor);

和canvas就像这样创建

mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(mBackground);

这是来自and android 5.0的捕获

Android 5.0

这是从android 4.4

中捕获的

Android <5.0

任何帮助,线索等......都将深表感谢。

修改 感谢您的建议,

我编写了一个函数,可以重新缩放textSize 10f的所有大小。 每次对canvas.drawText的原始调用都被替换为对此

的调用

drawScaledText(canvas,...原始值......)

一切似乎都很好。

以下是代码段:

public static void drawScaledText(Canvas canvas, String text, float x, float y, Paint paint, float scale) {
    float originalStrokeWidth = paint.getStrokeWidth();
    float originalTextSize = paint.getTextSize();
    float textScaling = 10f/originalTextSize;
    paint.setStrokeWidth(originalStrokeWidth * textScaling);
    paint.setTextSize(originalTextSize * textScaling);
    canvas.save();
    canvas.scale(scale/textScaling, scale/textScaling);
    canvas.drawText(text, x * textScaling, y * textScaling, paint);
    canvas.restore();
    paint.setStrokeWidth(originalStrokeWidth);
    paint.setTextSize(originalTextSize);
}

1 个答案:

答案 0 :(得分:2)

canvas.DrawText()canvas.DrawTextOnPath()在1

以下的尺寸下效果不佳

增加字体大小。您可以缩放画布以进行补偿。

请参阅此问题的答案。 Android 4.2.1 wrong character kerning (spacing)