Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android画布上,如何使用drawText调整字距和字母之间的间距?_Android_Canvas_Text_Drawtext_Kerning - Fatal编程技术网

在Android画布上,如何使用drawText调整字距和字母之间的间距?

在Android画布上,如何使用drawText调整字距和字母之间的间距?,android,canvas,text,drawtext,kerning,Android,Canvas,Text,Drawtext,Kerning,当我使用canvas.drawText直接在画布上绘制文本时,如何调整紧排?自从Lollipop以来,在Paint上可以使用setLetterSpacing方法。这种方法对我有效: if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { paint.setLetterSpacing(-0.04f); // setLetterSpacing is only availabl

当我使用canvas.drawText直接在画布上绘制文本时,如何调整紧排?

自从Lollipop以来,在Paint上可以使用setLetterSpacing方法。这种方法对我有效:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    paint.setLetterSpacing(-0.04f);  // setLetterSpacing is only available from LOLLIPOP and on
    canvas.drawText(text, xOffset, yOffset, paint);
} else {
    float spacePercentage = 0.05f;
    drawKernedText(canvas, text, xOffset, yOffset, paint, spacePercentage);
}


/**
 * Draw kerned text by drawing the text string character by character with a space in between.
 * Return the width of the text.
 * If canvas is null, the text won't be drawn, but the width will still be returned/
 * kernPercentage determines the space between each letter. If it's 0, there will be no space between letters.
 * Otherwise, there will be space between each letter. The  value is a fraction of the width of a blank space.
 */
private int drawKernedText(Canvas canvas, String text, float xOffset, float yOffset, Paint paint, float kernPercentage) {
    Rect textRect = new Rect();
    int width = 0;
    int space = Math.round(paint.measureText(" ") * kernPercentage);
    for (int i = 0; i < text.length(); i++) {
        if (canvas != null) {
            canvas.drawText(String.valueOf(text.charAt(i)), xOffset, yOffset, paint);
        }
        int charWidth;
        if (text.charAt(i) == ' ') {
            charWidth = Math.round(paint.measureText(String.valueOf(text.charAt(i)))) + space;
        } else {
            paint.getTextBounds(text, i, i + 1, textRect);
            charWidth = textRect.width() + space;
        }
        xOffset += charWidth;
        width += charWidth;
    }
    return width;
}

由于棒棒糖,在油漆上可以使用setLetterSpacing方法。这种方法对我有效:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
    paint.setLetterSpacing(-0.04f);  // setLetterSpacing is only available from LOLLIPOP and on
    canvas.drawText(text, xOffset, yOffset, paint);
} else {
    float spacePercentage = 0.05f;
    drawKernedText(canvas, text, xOffset, yOffset, paint, spacePercentage);
}


/**
 * Draw kerned text by drawing the text string character by character with a space in between.
 * Return the width of the text.
 * If canvas is null, the text won't be drawn, but the width will still be returned/
 * kernPercentage determines the space between each letter. If it's 0, there will be no space between letters.
 * Otherwise, there will be space between each letter. The  value is a fraction of the width of a blank space.
 */
private int drawKernedText(Canvas canvas, String text, float xOffset, float yOffset, Paint paint, float kernPercentage) {
    Rect textRect = new Rect();
    int width = 0;
    int space = Math.round(paint.measureText(" ") * kernPercentage);
    for (int i = 0; i < text.length(); i++) {
        if (canvas != null) {
            canvas.drawText(String.valueOf(text.charAt(i)), xOffset, yOffset, paint);
        }
        int charWidth;
        if (text.charAt(i) == ' ') {
            charWidth = Math.round(paint.measureText(String.valueOf(text.charAt(i)))) + space;
        } else {
            paint.getTextBounds(text, i, i + 1, textRect);
            charWidth = textRect.width() + space;
        }
        xOffset += charWidth;
        width += charWidth;
    }
    return width;
}

此解决方案用于跟踪,但不用于紧排。由于此解决方案适用于您,我建议您将原始问题改为要求跟踪。此解决方案用于跟踪,但不用于紧排。由于此解决方案适合您,因此我建议您将原始问题的措辞改为要求跟踪。