3

I have been using the method below (in various forms) for years, to render text with outlines on Android canvases, and it has never given me any issue until I tested an app on a Samsung Galaxy Tab recently. When using larger text, it becomes apparent that there is a large gap between the outline and the filled text.

public static void renderMessage(Canvas offScreenCanvas, String value, float inputXPos, float inputYPos, float textSize,
                                 boolean bold, boolean centreTextAroundLocation, int inputTextColour, int actualTextOutlineColour,
                                 float textOutlineStrokeWidth, final float standardStrokeWidth, Display display) {

    Paint paint = new Paint(){
        {
            setAntiAlias(true);
            setFilterBitmap(true);
            setStrokeWidth(standardStrokeWidth);
            setStyle(Paint.Style.FILL);
        }
    };

    if (bold) {
        paint.setTypeface(Typeface.DEFAULT_BOLD);
    }

    if (centreTextAroundLocation) {
        paint.setTextAlign(Paint.Align.CENTER);
    }

    paint.setTextSize(textSize);
    Rect bounds = new Rect();
    paint.getTextBounds(value, 0, value.length(), bounds);
    float actualHeight = (float)bounds.height();
    float actualWidth = (float)bounds.width();
    float xPos = inputXPos;
    float yPos = inputYPos;
    if (centreTextAroundLocation) {
        yPos = inputYPos - (paint.descent() + paint.ascent()) / 2.0f;
    }
    else {
        if (inputXPos == -1.0f) {
            xPos = (float)display.getWidth() / 2.0f - actualWidth / 2.0f;
        }

        if (inputYPos == -1.0f) {
            yPos = (float)display.getHeight() / 2.0f - actualHeight / 2.0f;
        }
    }

    if (actualTextOutlineColour != 0) {
        paint.setColor(actualTextOutlineColour);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(textOutlineStrokeWidth);
        offScreenCanvas.drawText(value, xPos, yPos, paint);
        paint.setStrokeWidth(standardStrokeWidth);
        paint.setStyle(Paint.Style.FILL);
    }

    paint.setColor(inputTextColour);
    offScreenCanvas.drawText(value, xPos, yPos, paint);
}

Literally all of my other test devices are fine, even at hugely exaggerated font sizes. It is only the Samsung Galaxy Tab that looks like the image below. I've tried every variation of that method I can think of, and I'm stumped. I have no idea where that gap is coming from.

enter image description here

HomerPlata
  • 1,687
  • 5
  • 22
  • 39

0 Answers0