public class CutsomView extends View{
private Paint paint;
public CutsomView(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* Canvas的方法:
* .drawArc 绘制弧
* .drawCircle 绘制圆
* .drawLine 绘制线
* .drawLines 绘制多条线
* .drawOval 绘制椭圆
* .drawPoint 绘制一个点
* .drawPoints 绘制多个点
* .drawRect 绘制矩形
* .drawRoundRect 绘制圆角矩形
*/
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 设置画布为白色
canvas.drawColor(Color.WHITE);
// 创建一支笔
paint = new Paint();
// 设置抗锯齿
paint.setAntiAlias(true);
// 设置画笔的宽度
paint.setStrokeWidth(3);
// 设置画笔的样式为描边
paint.setStyle(Style.STROKE);
// 设置画笔的颜色
paint.setColor(Color.BLUE);
// 使用画布绘制图像
canvas.drawCircle(50, 50, 30, paint);
paint.setColor(Color.YELLOW);
canvas.drawCircle(100, 50, 30, paint);
paint.setColor(Color.BLACK);
canvas.drawCircle(150, 50, 30, paint);
paint.setColor(Color.GREEN);
canvas.drawCircle(75, 90, 30, paint);
paint.setColor(Color.RED);
canvas.drawCircle(125, 90, 30, paint);
}
}
开发游戏时可能会用到
public class CostomView extends View {
private Paint paint;
public CostomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
// 设置抗锯齿
paint.setAntiAlias(true);
// 设置画笔的颜色
paint.setColor(Color.RED);
// 设置要绘制的文本大小
paint.setTextSize(35);
canvas.drawText("这是绘制出来的文本", 100, 100, paint);
}
}
public class CustomView extends View {
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 图片工厂解析资源文件
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawBitmap(bitmap, 0, 30, paint);
Rect src = new Rect(20,20,20,20);
Rect dst = new Rect(10, 10, 10, 10);
// 裁剪图片
//canvas.drawBitmap(bitmap, src, dst, paint);
if(bitmap!=null){
bitmap.recycle();
}
}
}
public class CustomView extends View{
private Paint paint;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
/**
* Path类
* .addArc 添加弧形路径
* .addCircle 添加圆形路径
* .addOval 添加椭圆形路径
* .addRect 添加矩形路径
* .addRoundRect 添加圆角矩形路径
* .moveTo 设置开始绘制直线的起始点
* .lineTo 在moveTo()方法设置的起始点与该方法指定的结束点之间话一条直线
* 如果在调用该方法之前没有使用moveTo()方法设置起始点,那么将从(0, 0)
* 点开始绘制
* .quadTo 用于根据指定的参数绘制一条线段轨迹
* .close 闭合路径
*/
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
// 设置抗锯齿
paint.setAntiAlias(true);
// 设置画笔宽度
paint.setStrokeWidth(3);
// 设置画笔的颜色
paint.setColor(Color.RED);
// 设置绘制文本的大小
paint.setTextSize(18);
// 设置填充方式为描边
paint.setStyle(Style.STROKE);
// 绘制圆形路径
Path pathCircle = new Path();
// 添加逆时针的圆形路径
pathCircle.addCircle(70, 70, 40, Path.Direction.CCW);
// 绘制圆形路径
canvas.drawPath(pathCircle, paint);
// 绘制折线路径
Path pathLine = new Path();
pathLine.moveTo(150, 100);
pathLine.lineTo(200, 45);
pathLine.lineTo(250, 100);
pathLine.lineTo(300, 80);
canvas.drawPath(pathLine, paint);
// 绘制三角形路径
Path pathTriangle = new Path();
pathTriangle.moveTo(350, 80);
pathTriangle.lineTo(400, 30);
pathTriangle.lineTo(450, 80);
// 闭合路径
pathTriangle.close();
canvas.drawPath(pathTriangle, paint);
// 绘制路径的环形文字
String str = "只要推力足够大,板砖也能飞上天";
Path pathText = new Path();
// 添加顺时针的圆形路径
pathText.addCircle(550, 100, 48, Path.Direction.CW);
paint.setStyle(Style.FILL);
canvas.drawTextOnPath(str, pathText, 0, -18, paint);
}
}