Android 4.2 on Nexus 7: canvas.drawText() not working correctly

前端 未结 3 461
故里飘歌
故里飘歌 2020-12-01 19:28

I\'m facing a serious issue with my Application, published on Google Play and apparently working fine on all versions of Android except for > 4.0.

This is a screensh

3条回答
  •  既然无缘
    2020-12-01 20:24

    I had a similar problem, trying to make a view with custom letter spacing so I just made this 2 methods, hope someone finds them helpful.

    /**
     * Draws a text in the canvas with spacing between each letter.
     * Basically what this method does is it split's the given text into individual letters
     * and draws each letter independently using Canvas.drawText with a separation of
     * {@code spacingX} between each letter.
     * @param canvas the canvas where the text will be drawn
     * @param text the text what will be drawn
     * @param left the left position of the text
     * @param top the top position of the text
     * @param paint holds styling information for the text
     * @param spacingPx the number of pixels between each letter that will be drawn
     */
    public static void drawSpacedText(Canvas canvas, String text, float left, float top, Paint paint, float spacingPx){
    
        float currentLeft = left;
    
        for (int i = 0; i < text.length(); i++) {
            String c = text.charAt(i)+"";
            canvas.drawText(c, currentLeft, top, paint);
            currentLeft += spacingPx;
            currentLeft += paint.measureText(c);
        }
    }
    
    /**
     * returns the width of a text drawn by drawSpacedText
     */
    public static float getSpacedTextWidth(Paint paint, String text, float spacingX){
        return paint.measureText(text) + spacingX * ( text.length() - 1 );
    }
    

提交回复
热议问题