Code example for TextPaint

Methods: ellipsize, getTextSize, setTextAlign

0
 
    private void drawSingle(Canvas canvas, int i, float rate, int left,
            int top, int right, int bottom) {
        final int height = bottom - top;
 
        this.textPaint.setTextAlign(Align.LEFT);
        final String title = getEllipsizedText((i + 1) + ". " + this.titles[i]);
 
        canvas.drawText(title, left,
                (top + bottom + this.textPaint.getTextSize()) / 2, textPaint);
 
        final int graphLeft = left + this.textWidth;
        canvas.drawRect(graphLeft, bottom - height * 0.85f, graphLeft
                + ((right - this.rateWidth) - graphLeft) * rate, top + height
                * 0.85f, paint);
 
        this.textPaint.setTextAlign(Align.RIGHT);
        canvas.drawText(values[i] + "%", right,
                (top + bottom + this.textPaint.getTextSize()) / 2, textPaint);
    } 
 
    private String getEllipsizedText(String text) {
        return TextUtils.ellipsize(text, this.textPaint, this.textWidth,
                TruncateAt.END).toString();
    } 
}