Code example for TextPaint

Methods: ascentdescent

0
			// We were told how big to be, so nothing to do. 
			result = specSize;
		} else { 
			// The lines should already be broken up. Calculate our max desired height for our current mode. 
			int numTitleLines = titleBreaker.getLines().size();
			float titleLineHeight = -textPaint.ascent() + titlePaint.descent();
			int titleBlockHeight = (int) (numTitleLines * titleLineHeight);
 
			int numTextLines = textBreaker.getLines().size();
			float textLineHeight = -titlePaint.ascent() + textPaint.descent();
			int textBlockHeight = (int) (numTextLines * textLineHeight);
 
			result = getPaddingTop() + titleBlockHeight + titleTextPadding + textBlockHeight + getPaddingBottom();
 
			if (imgSizeMode == 0) { // calculate height and width from title/text
				imgHeightInPixel = 0;
				if (numTitleLines < imgSizeInLines) {
					// title lines are not enough, also add title-text padding and remaining lines from text 
					imgHeightInPixel = (int) (numTitleLines * titleLineHeight + titleTextPadding);
					imgHeightInPixel += (imgSizeInLines - numTitleLines) * textLineHeight;
				} else { 
					// title lines are enough 
					imgHeightInPixel = (int) (imgSizeInLines * titleLineHeight);
				} 
				imgHeightInPixel -= imagePadding;
				imgWidthInPixel = imgHeightInPixel;
			} 
			// Image alone may be higher than title+text combined, => calculate its height 
			result = Math.max(result, getPaddingTop() + imgHeightInPixel + getPaddingBottom());
 
			// Respect AT_MOST value if that was what is called for by measureSpec. 
			if (specMode == MeasureSpec.AT_MOST) {
				result = Math.min(result, specSize);
			} 
		} 
		return result;
	} 
 
	private int breakWidth(int availableWidth) {
		float titleLineHeight = -titlePaint.ascent() + titlePaint.descent();
		float textLineHeight = -textPaint.ascent() + textPaint.descent();
 
		if (imgSizeMode == 0 && (imgHeightInPixel == -1 || imgWidthInPixel == -1)) {
			// We don't know the exact size of the image yet, so approximate it 
			imgHeightInPixel = (int) (titleLineHeight * imgSizeInLines);
			imgWidthInPixel = imgHeightInPixel;
		} 
 
		int fullWidth = availableWidth - getPaddingLeft() - getPaddingRight();
		int tabbedWidth = (int) (fullWidth - (imgWidthInPixel + imagePadding));
 
		int imageHeightInTitleLines = (int) Math.ceil(imgHeightInPixel / titleLineHeight);
 
		int titleLines = 100;
		int[] titleMaxWidths = new int[titleLines];
		tabbedTitleLines = Math.min(titleLines, imageHeightInTitleLines);
		Arrays.fill(titleMaxWidths, 0, tabbedTitleLines, tabbedWidth);
		Arrays.fill(titleMaxWidths, tabbedTitleLines, titleLines, fullWidth);
		titleBreaker.setMaxWidths(titleMaxWidths);
		titleBreaker.breakText(title, titlePaint);
 
		int textLinesToTab = 0;
		float realTitleLinesHeight = titleBreaker.getLines().size() * titleLineHeight;
		if ((realTitleLinesHeight + titleTextPadding) < imgHeightInPixel) {
			float d = imgHeightInPixel - (realTitleLinesHeight + titleTextPadding);