Android Paint brush basic usage

Common methods:

Construction method

Paint mPaint = new Paint();

Reset brush

mPaint.reset();

Set color

mPaint.setColor(Color.RED); /setARGB(int a, int r, int g, int b)

Set transparency

mPaint.setAlpha(255);

Set style

mPaint.setStyle(Paint.Style.FILL)
 Paint.Style.FILL   填充内容
 Paint.Style.STROKE 描边
 Paint.Style.FILL_AND_STROKE

Set brush width

mPaint.setStrokeWidth(50)

Set cap

mPaint.setStrokeCap(Paint.Cap.BUTT)
 Paint.Cap.BUTT   没有
 Paint.Cap.ROUND  圆形
 Paint.Cap.SQUARE

Set the line connection style

mPaint.setStrokeJoin(Paint.Join.MITER);
 Join.MITER(结合处为锐角)
 Join.Round(结合处为圆弧)
 Join.BEVEL(结合处为直线)

Anti-aliasing

mPaint.setAntiAlias(true);

Will lose some performance

Set whether to use image dithering

mPaint.setDither(true);

It will make the color of the drawn picture more clear and full. (Also lose performance)

Text drawing:

Get character line spacing

mPaint.getFontSpacing()

Get the spacing between characters

mPaint.getLetterSpacing();

Set the spacing between characters

mPaint.setLetterSpacing(letterSpacing)

Set Strikethrough

mPaint.setStrikeThruText(true);

Whether to set underline

mPaint.setUnderlineText(true);

Set text size

mPaint.setTextSize(textSize)

Get text size

mPaint.getTextSize();

Set font type

mPaint.setTypeface(Typeface.BOLD)
 Typeface.BOLD 粗体
 Typeface.ITALIC 斜体

Load custom font

Typeface.create(familyName, style)

Set text slant

mPaint.setTextSkewX(-0.25f);

The officially recommended -0.25f is italic

Text alignment

mPaint.setTextAlign(Align.LEFT)
 Align.LEFT  左对齐
 Align.CENTER 中间对其
 Align.RIGHT 右对齐

Calculate a string of specified length

mPaint.breakText(text, measureForwards, maxWidth, measuredWidth)
 text:字符串
 measureForwards:boolean
 maxWidth:int
 measuredWidth:float[]  结果接收

Get rectangular area of ​​text (width and height)

mPaint.getTextBounds(text, index, count, bounds)`
 `mPaint.getTextBounds(text, start, end, bounds)

Get the rough width of the text

mPaint.measureText(str);

Get the width of each character

`

mPaint.getTextWidths(str, measuredWidth);
mPaint.getTextWidths(text, start, end, widths)`textWidths字符数
Published 60 original articles · 25 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_41466437/article/details/104701327