Code example for Paint

Methods: ascent, descent, measureText

0
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be 
            result = specSize;
        } else { 
            // Measure the text 
            result = (int) textPaint.measureText("hello") + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec 
                result = Math.min(result, specSize);
            } 
        } 
 
        return result;
    } 
 
    /** 
     * Determines the height of this view 
     * 
     * @param measureSpec A measureSpec packed into an int 
     * @return The height of the view, honoring constraints from measureSpec 
     */ 
    private int measureHeight(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
 
        ascent = (int) textPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be 
            result = specSize;
        } else { 
            // Measure the text (beware: ascent is a negative number) 
            result = (int) (-ascent + textPaint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec 
                result = Math.min(result, specSize);
            }