Android drawText missing characters on Lollipop

1.4k views Asked by At

I had an android app that displays a scale and it worked fine on all version until 5.0 (Lollipop) where the all the texts drawn with drawText method are somehow truncated. Only the second letter of a tow letters text is displayed.
i.e. The first mark is only '0' letter but is not displayed.
For the second 2 marks (40, and 80) only 0 is displayed and for the rest, 140... 250, no text is displayed.
The text size seems to be OK but not all the chars are displayed. The image is scaled in [0..1, 0..1] square.
I have found and try lot of posts on drawText but none of them helped.
(View.setLayerType() all combinations, 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();
}

where a range paint is create like this

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);

and canvas is created like this

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

This is a capture from and android 5.0

Android 5.0

This is capture from an android 4.4

Android <5.0

Any help, clue, etc... will be deeply appreciated.

EDIT: Thank you for suggestions,

I have wrote a function that re-scale all the sizes as for an textSize 10f. Every original call to canvas.drawText was replaced with a call to this

drawScaledText(canvas, ... original values ...)

Everything seem to work fine.

Here is the code snippet:

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

There are 1 answers

0
Kuffs On BEST ANSWER

canvas.DrawText() and canvas.DrawTextOnPath() do not work well at sizes under 1

Increase your font size. You can scale your canvas to compensate.

See answer to this question. Android 4.2.1 wrong character kerning (spacing)