4

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);
}
Luci
  • 1,278
  • 9
  • 17
  • what was your scale param? i use every number and non of them working :( i use this library and have this problem – Amir Hossein Ghasemi Feb 18 '15 at 07:06
  • @AmirHosseinGhasemi Did you ever figure this out? I am also using this library and cannot seem to find a good answer on how to fix this. OP's solution does not seem to work for me either. – Evan Zimmerman Jun 10 '15 at 20:06
  • Yes, it worked for me but only with textSize bigger than 1f. AS Kuffs said, the drawText function doesn't work well if size is under 1f. All my drawings are done in a 1f x 1f rectangle so that the image could be easily scales to any dimension. That was the reason for which my text size was originally 0.05f. The function I wrote, draw a text with a bigger size but then rescale the whole canvas to match the entire image. – Luci Jun 11 '15 at 21:00
  • @Luci I have the same "configuration", a 1f x 1f rectangle and a wanted textSize of 0.05f. But I can't figure the good parameters to use with your function to draw my text where I want. I want my text to be drawn at x = 0.5f, y = 0.8f with 0.05f size. What values should I use for Paint.textSize and the scale parameter ? – Magnas Jun 04 '16 at 08:58
  • @Magnas here are my settings for default text paint getDefaultTextValuePaint() { final Paint paint = new Paint(Paint.LINEAR_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); paint.setColor(mTextValueColor); paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setStrokeWidth(0.005f); paint.setTextSize(mTextValueSize); paint.setTextAlign(Align.CENTER); paint.setTypeface(Typeface.SANS_SERIF); paint.setShadowLayer(0.01f, 0.002f, 0.002f, mTextShadowColor); return paint; } where TextValueSize is 0.3f – Luci Jun 08 '16 at 19:45

1 Answers1

2

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)

Community
  • 1
  • 1
Kuffs
  • 35,581
  • 10
  • 79
  • 92