canvas教程

android16#--canvas.drawText

字号+ 作者:H5之家 来源:H5之家 2017-03-12 12:02 我要评论( )

дддлcanvas.drawTextе[float y]塣FontMetricsRect庬塣 drawText apiζdrawText /*** Draw the text, with origin at (x,y), using the specified paint. The* origin is interpreted based on the Align setting in the paint.** @param text The text

дддлcanvas.drawTextе[float y]塣FontMetricsRect庬塣

drawText

apiζdrawText

/** * Draw the text, with origin at (x,y), using the specified paint. The * origin is interpreted based on the Align setting in the paint. * * @param text The text to be drawn * @param x The x-coordinate of the origin of the text being drawn * @param y The y-coordinate of the baseline of the text being drawn * @param paint The paint used for the text (e.g. color, size, style) */ public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) { native_drawText(mNativeCanvasWrapper, text, 0, text.length(), x, y, paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface); }


@param text
@param xxи壬
@param ybaselineybaseline
@param paint

baseline

baselineλácode

@Override protected void onDraw(Canvas canvas) { //super.onDraw(canvas); mRect = new Rect(); mPaint = new Paint(); mPaint.setTextSize(64); mPaint.setColor(Color.RED); Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt(); mPaint.getTextBounds(textStr, 0, textStr.length(), mRect); int w = mRect.width(); int h = mRect.height(); mRect = new Rect(mRectLeft, mRectTop, mRectLeft+w, mRectTop+(-fontMetrics.top)+fontMetrics.bottom); canvas.drawRect(mRect, mPaint); mBaseLine = (mRect.bottom+mRect.top)/2 - (fontMetrics.bottom+fontMetrics.top)/2; mBaseLine = -fontMetrics.top; /*y == mBaseLine*/ mPaint.setColor(Color.WHITE); canvas.drawText(textStr, mRectLeft, mBaseLine, mPaint); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(3); /*mBaseLineλ*/ canvas.drawLine(mRectLeft, mBaseLine, mRectLeft+w, mBaseLine, mPaint); }

Ч

д

baselineλ

FontMetrics

FontMetricsPaintPaintAPI

Class that describes the various metrics for a font at a given text size. Remember, Y values increase going down, so those values will be positive, and values that measure distances going up will be negative. This class is returned by getFontMetrics().

趨fontsizegetFontMetrics()
FontMetrics5

public static class FontMetrics { /** * The maximum distance above the baseline for the tallest glyph in * the font at a given text size. */ public float top; /** * The recommended distance above the baseline for singled spaced text. */ public float ascent; /** * The recommended distance below the baseline for singled spaced text. */ public float descent; /** * The maximum distance below the baseline for the lowest glyph in * the font at a given text size. */ public float bottom; /** * The recommended additional space to add between lines of text. */ public float leading; }

еbaselinetop塣
topapiλbaseline-topbaseline
ascentλbaseline
descentλbaselineascent
bottomλbaseline
leading淭0к

Rect

RectPaintgetTextBounds()font sizebaseline
Rect4lefttoprightbottomrectbaselinetopbottombaseline

FontMetricsRect @Override protected void onDraw(Canvas canvas) { //super.onDraw(canvas); mRect = new Rect(); mPaint = new Paint(); mPaint.getTextBounds(textStr, 0, 1, mRect); mPaint.setTextSize(textSize); mPaint.setColor(0xffF4A460); Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt(); mPaint.getTextBounds(textStr, 0, textStr.length(), mRect); int w = mRect.width(); int h = mRect.height(); /*fontMetrics0xffF4A460*/ mRect = new Rect(mRectLeft, mRectTop, mRectLeft+w, mRectTop+(-fontMetrics.top)+fontMetrics.bottom); canvas.drawRect(mRect, mPaint); /*baselinemRectTopview*/ mBaseLine = -fontMetrics.top + mRectTop; /*y == mBaseLine*/ mPaint.setColor(Color.WHITE); canvas.drawText(textStr, mRectLeft, mBaseLine, mPaint); mPaint.setColor(Color.BLUE); mPaint.setStrokeWidth(4); /*mBaseLineλ*/ canvas.drawLine(mRectLeft, mBaseLine, mRectLeft+w, mBaseLine, mPaint); mPaint.setColor(Color.GREEN); /*bottomλ*/ canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.bottom, mRectLeft+2*w , mBaseLine+fontMetrics.bottom, mPaint); mPaint.setColor(Color.RED); /*topλ*/ canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.top, mRectLeft+2*w , mBaseLine+fontMetrics.top, mPaint); mPaint.setColor(Color.GRAY); /*ascentλ*/ canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.ascent, mRectLeft+w+50 , mBaseLine+fontMetrics.ascent, mPaint); mPaint.setColor(Color.WHITE); /*descentλ*/ canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.descent, mRectLeft+w+50 , mBaseLine+fontMetrics.descent, mPaint); mPaint.setColor(Color.YELLOW); /*leadingλ*/ canvas.drawLine(mRectLeft, mBaseLine+fontMetrics.leading, mRectLeft+w/2 , mBaseLine+fontMetrics.leading, mPaint); /*rectλ*/ mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.getTextBounds(textStr, 0, 1, mRect); mRect.top+=mBaseLine; mRect.bottom+=mBaseLine; canvas.drawRect(mRect, mPaint); /*:12hgrectλ*/ mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.getTextBounds(textStr, 0, 7, mRect); mRect.top+=mBaseLine; mRect.bottom+=mBaseLine; canvas.drawRect(mRect, mPaint); }

Ч

д

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评
s