惯例:先上图后说话,谢谢各位伙伴的支持! 有你们是我的福分!
欢迎一起讨论和学习,QQ:732258496 微信:15520726587
package nsuperseller.grasp.com.my_paint.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;
/**
* Created by Administrator on 2016/9/2.
*/
public class MyViewPath extends View {
Context m_context;
public MyViewPath(Context context) {
super(context);
m_context = context;
}
//重写OnDraw()函数,在每次重绘时自主实现绘图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// paintRect(canvas);//画圆
// paintRoundRect(canvas);//
//paintRound(canvas);//圆形路径
// paintOval(canvas);//椭圆
// paintAngle(canvas);//弧
setPaint(canvas);
}
/*画三角形-----1、直线路径*/
private void paintSanjiao(Canvas canvas) {
//设置画笔基本属性
Paint paint = new Paint();
paint.setAntiAlias(true);//抗锯齿功能
paint.setColor(Color.RED);//设置画笔的颜色
paint.setStyle(Paint.Style.STROKE);//设置填充的样式
paint.setStrokeWidth(5);//设置画笔的宽度
Path path = new Path();
path.moveTo(10, 10);//设置起始点
path.lineTo(10, 100);//第一条直线的终点,也是第二条直线的起点
path.lineTo(300, 100);//画第二条直线
// path.lineTo(500,100);//画第三条直线
path.close();
canvas.drawPath(path, paint);
}
/*矩形路径 依据路径写出文字*/
private void paintRect(Canvas canvas) {
//设置画笔基本属性
Paint paint = new Paint();
paint.setAntiAlias(true);//抗锯齿功能
paint.setColor(Color.RED);//设置画笔的颜色
paint.setStyle(Paint.Style.STROKE);//设置填充的样式
paint.setStrokeWidth(5);//设置画笔的宽度
//先创建两个大小一样的矩阵
//第一个逆向生成 Path.Direction.CCW
Path path1 = new Path();
RectF rectF1 = new RectF(50, 50, 240, 200);
path1.addRect(rectF1, Path.Direction.CCW);
//第二个顺时生成
Path path2 = new Path();
RectF rectF2 = new RectF(290, 50, 480, 200);
path2.addRect(rectF2, Path.Direction.CW);
//先画出这两个路径
canvas.drawPath(path1, paint);
canvas.drawPath(path2, paint);
//依据路径写出文字
String text = "我爱我自己,我爱我生命!我是最棒的!";
paint.setColor(Color.GREEN);
paint.setTextSize(20);
canvas.drawTextOnPath(text, path1, 8, 10, paint);
canvas.drawTextOnPath(text, path2, 0, 18, paint);
}
//圆角矩形路径
private void paintRoundRect(Canvas canvas) {
//设置画笔基本属性
Paint paint = new Paint();
paint.setAntiAlias(true);//抗锯齿功能
paint.setColor(Color.RED);//设置画笔的颜色
paint.setStyle(Paint.Style.STROKE);//设置填充的样式
paint.setStrokeWidth(5);//设置画笔的宽度
Path path = new Path();
RectF rect = new RectF(50, 50, 24, 200);
path.addRoundRect(rect, 10, 15, Path.Direction.CCW);
RectF rectF1 = new RectF(290, 50, 480, 200);
float radii[] = {10, 15, 20, 25, 30, 35, 40, 45};
path.addRoundRect(rectF1, radii, Path.Direction.CCW);
canvas.drawPath(path, paint);
}
//圆形路径
private void paintRound(Canvas canvas) {
//设置画笔基本属性
Paint paint = new Paint();
paint.setAntiAlias(true);//抗锯齿
paint.setStyle(Paint.Style.STROKE);//设置填充的样式
paint.setColor(Color.RED);//设置画颜色
paint.setStrokeWidth(5);//设置画笔的宽度
Path path = new Path();
path.addCircle(200, 200, 100, Path.Direction.CCW);
canvas.drawPath(path, paint);
}
//椭圆路径
private void paintOval(Canvas canvas) {
//设置画笔基本属性
Paint paint = new Paint();
paint.setAntiAlias(true);//抗锯齿
paint.setStyle(Paint.Style.STROKE);//设置填充的样式
paint.setColor(Color.RED);//设置画颜色
paint.setStrokeWidth(5);//设置画笔的宽度
Path path = new Path();
RectF rect = new RectF(50, 50, 240, 200);
path.addOval(rect, Path.Direction.CCW);
canvas.drawPath(path, paint);
}
//弧路径
private void paintAngle(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
Path path = new Path();
RectF rectF = new RectF(50, 50, 240, 200);
path.addArc(rectF, 0, 200);
canvas.drawPath(path, paint);
}
//线段轨迹
private void setPaint(Canvas canvas) {
//普通设置
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(40);
paint.setTextAlign(Paint.Align.CENTER);
//样式设置
paint.setFakeBoldText(true);//粗体
paint.setUnderlineText(true);//下划线
paint.setTextSkewX((float) -0.25);//设置字体水平倾斜度,普通斜体字是-0.25
paint.setStrikeThruText(true);//设置带有删除线效果
//其他设置
paint.setTextScaleX(2);
canvas.drawText("我爱我自己,我爱我生命!我是最棒的!", 10, 300, paint);
}
}