3

Prelude

I was setting up a custom stylized text that changes its text size dynamically using app:autoSizeTextType="uniform" and adding a two different strokes to it in its onDraw method. Of the two different strokes one acts as the main outline and the other as a padding between the text and the main outline.

Problem im facing

Since im using app:autoSizeTextType="uniform" the text most likely reaches the end of TextView, but since I'm adding a stroke to this, the strokes are clipped. Adding android:clipChildren="false" to the parent also doesn't do the trick.

Code

@Override public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int restoreColor = this.getCurrentTextColor();

    if (strokeColor != null) {
        // Outer outline
        TextPaint outlinePaint = this.getPaint();
        outlinePaint.setStyle(Paint.Style.STROKE);
        outlinePaint.setStrokeJoin(strokeJoin);
        outlinePaint.setStrokeMiter(strokeMiter);
        this.setTextColor(ContextCompat.getColor(getContext(), R.color.colorLighterGrey));
        outlinePaint.setStrokeWidth((getTextSize() / 10) + 2);
        super.onDraw(canvas);
        // Middle padding 
        TextPaint paddingPaint = this.getPaint();
        paddingPaint.setStyle(Paint.Style.STROKE);
        paddingPaint.setStrokeJoin(strokeJoin);
        paddingPaint.setStrokeMiter(strokeMiter);
        this.setTextColor(strokeColor == restoreColor ? ContextCompat.getColor(getContext(), R.color.colorLighterGrey) : strokeColor);
        paddingPaint.setStrokeWidth(getTextSize() / 10);
        super.onDraw(canvas);

        this.setTextColor(restoreColor);
        TextPaint paint = this.getPaint();
        paint.setStyle(Paint.Style.FILL);
        super.onDraw(canvas);
    }
}

Output

Without layout Bounds enabled With layout Bounds enabled

P.S. Totally new to this kind of manipulation, any help would be appreciated. Also had a look at Draw outside bounds

Ritesh Shakya
  • 575
  • 5
  • 17

0 Answers0