Code example for Paint

Methods: getTextBounds

0
    @Override   
    protected synchronized void onDraw(Canvas canvas) {  
        // First draw the regular progress bar, then custom draw our text   
        super.onDraw(canvas);  
        Rect bounds = new Rect();  
        textPaint.getTextBounds(text, 0, text.length(), bounds);  
        int x = getWidth() / 2 - bounds.centerX();  
        int y = getHeight() / 2 - bounds.centerY();  
        canvas.drawText(text, x, y, textPaint);  
    }   
   
    public synchronized void setText(String text) {  
        this.text = text;  
        drawableStateChanged();   
    }   
   
    public void setTextColor(int color) {  
        textPaint.setColor(color);  
        drawableStateChanged();   
    }   
}