CodeAndMagic / GaugeView

An Android library for drawing gauges on Canvas.
Apache License 2.0
224 stars 156 forks source link

Text is not shown properly at Android 4.4 devices #4

Open thomarik opened 10 years ago

thomarik commented 10 years ago

due to a change in the android Paint.measureText() the text in the GaugeView are not shown properly, since the value of measureText will be ceiled up to the next integer, the text is shown at the left side of the gauge and just one digit (without unit). https://github.com/android/platform_frameworks_base/commit/8e04840f38a16f806754dfca3de50c2548e67913

LorenzGardner commented 7 years ago

Actually, it doesn't seem to be a messureText issue at all. as it is only used if you set a unit value for the text. The single digit only issue persists even without a unit.

This is related to issue with fonts of size less than 1, and the canvas.drawText just doing bad things at that point.

https://code.google.com/p/android/issues/detail?id=40965

setting Linear and subPixel to true on the painter didn't resolve the issue as far as I could tell, but using the workaround listed in:

https://code.google.com/p/android/issues/detail?id=39755

I simply replaces the cavas.drawText line with a function that did the following:

private void drawTextWorkaround(final Canvas canvas, final String value, float x, float y, final Paint paint) {
    //Save original font size
    float originalTextSize = paint.getTextSize();

    // set a magnification factor
    final float magnifier = 100f;

    // Scale the canvas
    canvas.save();
    canvas.scale(1f / magnifier, 1f / magnifier);

    // increase the font size
    paint.setTextSize(originalTextSize * magnifier);

    canvas.drawText(value, x*magnifier, y*magnifier, paint);

    // bring everything back to normal
    canvas.restore();
    paint.setTextSize(originalTextSize);
}

Long term the solution would probably be to not scale the canvas down so small. I think, this is done for easy of specifying coordinates (i.e. center is simply .5). GagueView could easily be modified to work on a scale of 10, instead of 1 (i.e. center is now 5). That way the default font wouldn't be .3, but 3 and wouldn't run into this canvas.drawText issue with small fonts.