mPaint = new Paint();
mPaint.setColor(Color.BLUE); mPaint.setAlpha(255);
public enum Style {
/**
* Geometry and text drawn with this style will be filled, ignoring all
* stroke-related settings in the paint.
*/
FILL (0),
/**
* Geometry and text drawn with this style will be stroked, respecting
* the stroke-related fields on the paint.
*/
STROKE (1),
/**
* Geometry and text drawn with this style will be both filled and
* stroked at the same time, respecting the stroke-related fields on
* the paint. This mode can give unexpected results if the geometry
* is oriented counter-clockwise. This restriction does not apply to
* either FILL or STROKE.
*/
FILL_AND_STROKE (2);
Style(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
mPaint.setStrokeWidth(50);
/**
* The Cap specifies the treatment for the beginning and ending of
* stroked lines and paths. The default is BUTT.
*/
public enum Cap {
/**
* The stroke ends with the path, and does not project beyond it.
*/
BUTT (0),
/**
* The stroke projects out as a semicircle, with the center at the
* end of the path.
*/
ROUND (1),
/**
* The stroke projects out as a square, with the center at the end
* of the path.
*/
SQUARE (2);
private Cap(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.BLUE);
mPaint.setAlpha(255);
//设置画笔的样式
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//画笔的宽度
mPaint.setStrokeWidth(50);
mPaint.setStrokeCap(Paint.Cap.SQUARE);//方形
mPaint.setStrokeJoin(Paint.Join.BEVEL);//直线
Path path = new Path();
path.moveTo(100, 100);
path.lineTo(300, 100);
canvas.drawPath(path, mPaint);
mPaint.reset();//重置
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(50);
mPaint.setStrokeCap(Paint.Cap.ROUND);//圆形
mPaint.setStrokeJoin(Paint.Join.BEVEL);//直线
Path path1 = new Path();
path1.moveTo(100, 200);
path1.lineTo(300, 200);
canvas.drawPath(path1, mPaint);
mPaint.reset();//重置
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(50);
mPaint.setStrokeCap(Paint.Cap.BUTT);//没有
mPaint.setStrokeJoin(Paint.Join.BEVEL);//直线
Path path2 = new Path();
path2.moveTo(100, 300);
path2.lineTo(300, 300);
canvas.drawPath(path2, mPaint);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.BLUE);
mPaint.setAlpha(255);
mPaint.setStyle(Paint.Style.STROKE);//设置画笔的样式
mPaint.setStrokeWidth(50);//画笔的宽度
mPaint.setStrokeCap(Paint.Cap.BUTT);//线帽
mPaint.setStrokeJoin(Paint.Join.BEVEL);
Path path = new Path();
path.moveTo(100, 100);
path.lineTo(300, 100);
path.lineTo(100, 300);
path.close();
canvas.drawPath(path, mPaint);
mPaint.reset();//重置
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(50);
mPaint.setStrokeCap(Paint.Cap.BUTT);//圆形
mPaint.setStrokeJoin(Paint.Join.ROUND);//圆弧
Path path1 = new Path();
path1.moveTo(100, 400);
path1.lineTo(300, 400);
path1.lineTo(100, 700);
path1.close();
canvas.drawPath(path1, mPaint);
mPaint.reset();//重置
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(50);
mPaint.setStrokeCap(Paint.Cap.BUTT);//没有
mPaint.setStrokeJoin(Paint.Join.MITER);//锐角
Path path2 = new Path();
path2.moveTo(100, 800);
path2.lineTo(300, 800);
path2.lineTo(100, 1100);
path2.close();
canvas.drawPath(path2, mPaint);
}
mPaint.setAntiAlias(true);
setLetterSpacing
mPaint.setStrikeThruText(true);
mPaint.setUnderlineText(true);
mPaint.setTextSize(textSize);
mPaint.setTypeface(Typeface.BOLD); // Style public static final int NORMAL = 0;//常规 public static final int BOLD = 1;//粗体 public static final int ITALIC = 2; //斜体 public static final int BOLD_ITALIC = 3;//粗斜体
Typeface.create(familyName, style)
mPaint.setTextSkewX(-0.25f);
mPaint.setTextAlign(Align.LEFT)
public enum Align {
/**
* The text is drawn to the right of the x,y origin
*/
LEFT (0),//左对齐
/**
* The text is drawn centered horizontally on the x,y origin
*/
CENTER (1),//居中
/**
* The text is drawn to the left of the x,y origin
*/
RIGHT (2);//右对齐
private Align(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}
int breadText = mPaint.breakText(text, measureForwards, maxWidth, measuredWidth)
Rect bounds获取文本的矩形区域(宽高) mPaint.getTextBounds(text, index, count, bounds) mPaint.getTextBounds(text, start, end, bounds) //获取文本的宽度,和上面类似,但是是一个比较粗略的结果 float measureText = mPaint.measureText(str); //获取文本的宽度,和上面类似,但是是比较精准的。 float[] measuredWidth = new float[10]; //measuredWidth得到每一个字符的宽度;textWidths字符数 int textWidths = mPaint.getTextWidths(str, measuredWidth); mPaint.getTextWidths(text, start, end, widths)
public class PaintView extends View {
private Paint mPaint;
private String text = "你是我世界之光,我心另一半";
public PaintView(Context context) {
this(context,null);
}
public PaintView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public PaintView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);//设置画笔的样式
mPaint.setStrokeCap(Paint.Cap.BUTT);//线帽
mPaint.setStrokeJoin(Paint.Join.BEVEL);
int top = 100;
int baselineX = 0;
mPaint.setTextSize(50);
mPaint.setTextAlign(Paint.Align.LEFT);
canvas.drawLine(0, top, 2000, top, mPaint);
//文本Metrics
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
float baselineY = top - fontMetrics.top;
canvas.drawText(text, baselineX, baselineY, mPaint);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getPaddingLeft();
//dy 代表的是:高度的一半到 baseLine的距离
Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
// top 是一个负值 bottom 是一个正值 top,bttom的值代表是 bottom是baseLine到文字底部的距离(正值)
// 必须要清楚的,可以自己打印就好
int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
int baseLine = getHeight()/2 + dy;
canvas.drawText(costom_text,x,baseLine,paint);
}
/**
* 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);
}
int dy = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom;
int baseLine = getHeight()/2 + dy;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有