1.绘制文字
public class MyTextView extends View {
Paint paint = new Paint();
private String str = "好好学习,努力奋斗,abcdefg";
public MyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
//设置画笔颜色,也就是文字的颜色
paint.setColor(Color.RED);
//设置字体大小
paint.setTextSize(25);
//设置背景色
setBackgroundColor(Color.BLUE);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureView(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* text:要绘制的文字
* x:右上角横坐标
* y:右上角纵坐标
*/
canvas.drawText(str, 0, getHeight()/2, paint);
}
/**
* 测量
*/
private void measureView(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(720, 300);
} else if (widthMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(720, height);
} else if (heightMeasureSpec == MeasureSpec.AT_MOST) {
setMeasuredDimension(width, 300);
}
}
}
1.1 水平居中
要X轴居中,需要左右边距相等,那么:
x = (viewWidth - textWidth) / 2
代码如下:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//拿到字符串的宽度 设置X轴居中
float strWidth = paint.measureText(str);
float x = (getWidth() - strWidth) / 2;
/**
* text:要绘制的文字
* x:右上角横坐标
* y:右上角纵坐标
*/
canvas.drawText(str, x, getHeight()/2, paint);
}
1.2 垂直居中
要做到文字垂直居中,首先要了解一下BaseLine这个名词:
在canvas
中drawText
绘制文字时候,也是有规则的,这个规则就是baseLine
(基线)。什么又是基线呢,说白了就是一条直线,我们这里理解的是确定它的位置。我们先来看一下基线:
我们在绘制文字的时候通常会认为y轴坐标就是文字的最顶部,这个理解是错误的。
正确的是y轴坐标是基线的位置。canves在绘制的时候以基线为基准,显然ascent远大于descent。
扫描二维码关注公众号,回复:
6225877 查看本文章
文字要居中显示,那么y就要往下平移: y = (|ascent| - descent) / 2;
代码如下:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//拿到字符串的宽度 设置X轴居中
float strWidth = paint.measureText(str);
float textLeftMargin = (getWidth() - strWidth) / 2;
//拿到字符串的高度
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float y = getHeight() / 2 + (Math.abs(fontMetrics.ascent) - fontMetrics.descent);
/**
* text:要绘制的文字
* x:右上角横坐标
* y:右上角纵坐标
*/
canvas.drawText(str, textLeftMargin, y, paint);
}
1.3 设置对齐方式
设置对齐方式,是以y轴线为基准。左对齐向右绘制,右对齐向左绘制,中心对齐是分别向两侧绘制。
1.3.1 左对齐(默认对齐方式)
paint.setTextAlign(Paint.Align.LEFT);
1.3.2 右对齐
paint.setTextAlign(Paint.Align.RIGHT);
1.3.3 中心对齐
paint.setTextAlign(Paint.Align.CENTER);
1.4 设置字体
系统提供了四种字体方式:
-
Typeface.BOLD 粗体
-
Typeface.NORMAL 默认
-
Typeface.ITALIC 斜体
-
Typeface.BOLD_ITALIC 粗斜体
-
Typeface.DEFAULT_BOLD 默认粗体
1.5 设置缩放
paint.setTextScaleX(1.5f);
1.6 设置下划线
paint.setUnderlineText(true);
1.7 设置文本删除线
paint.setStrikeThruText(true);
1.8 其他
-
paint.getFontSpacing() //获取行高
-
paint.setTextSize(50); //设置字体大小
2.绘制圆形
2.1 三种画笔样式
- 实心
paint.setStyle(Paint.Style.FILL);
- 空心
paint.setStyle(Paint.Style.STROKE);
- 实心加描边
paint.setStyle(Paint.Style.FILL_AND_STROKE);
2.2 画笔属性
- 抗锯齿
paint.setFlags(Paint.ANTI_ALIAS_FLAG );
- 防抖动
paint.setFlags(Paint.DITHER_FLAG);
3.绘制图片
代码:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
int width = (getWidth() - bitmap.getWidth())/2;
int height = (getHeight() - bitmap.getHeight())/2;
canvas.drawBitmap(bitmap,width,height,paint);
4.绘制扇形
代码:
paint.setColor(Color.GREEN);
RectF rect = new RectF(0f,0f,400f,400f); //圆心坐标((right-left)/2,(bottom-top)/2)
canvas.drawArc(rect,0,60,true,paint);
//圆点位置,开始角度,扫过的角度,是否要焦点圆心,画笔。注意:结束角度 = sweepAngle + startAngle;
5.绘制矩形
代码:
int left = getLeft();
int right = getRight();
int top = getTop();
int bottom = getBottom();
canvas.drawRect(left,top,right,bottom,paint);
6.绘制路径
代码:
paint.setColor(Color.GREEN);
Path p = new Path();
p.moveTo(100, 100); //第一个点的坐标
p.lineTo(200, 50); //第二个点的坐标
p.lineTo(300, 100); //第三个点的坐标
p.lineTo(200,400); //第四个点的坐标
canvas.drawPath(p,paint);