【Android】【绘图】Paint和Canvas的基本API
//Canvas和Paint初步使用://自定义一个类,继承View//在构造方法中创建一个画笔//在onDraw方法中,可以获得一个画布//画笔APIPaint paint = new Paint();paint.setAntiAlias(true); //抗锯齿paint.setStyle(Paint.Style.FILL); //填充paint.setSt...
·
//Canvas和Paint初步使用:
//自定义一个类,继承View
//在构造方法中创建一个画笔
//在onDraw方法中,可以获得一个画布
//画笔API
Paint paint = new Paint();
paint.setAntiAlias(true); //抗锯齿
paint.setStyle(Paint.Style.FILL); //填充
paint.setStyle(Paint.Style.STROKE); //描边
paint.setStyle(Paint.Style.FILL_AND_STROKE); //填充并描边
paint.setStrokeWidth(5); //画笔宽度
paint.setColor(ColorUtil.WHITE); //画笔颜色
paint.setStrokeCap(Paint.Cap.ROUND); //圆角线头
paint.setColorFilter(new LightingColorFilter(ColorUtil.LIGHT_BLUE, 0)); //设置颜色过滤器
//画布API
//基本操作
canvas.drawARGB(0x99, 0xFF, 0xFF, 0x00); //画布染色
canvas.drawColor(ColorUtil.LIGHT_BLUE, PorterDuff.Mode.ADD); //画布染色(颜色混合)
canvas.drawPoint(100, 100, paint); //画点
canvas.drawLine(100, 100, 400, 400, paint); //画线
canvas.drawLines(new float[]{0, 0, 600, 600}, paint); //画线
canvas.drawLines(new float[]{200, 200, 700, 400, 200, 300, 700, 400}, 0, 8, paint); //画多条线
canvas.drawRect(100, 100, 600, 600, paint); //画矩形
canvas.drawRoundRect(100, 100, 600, 600, 20, 20, paint); //画圆角矩形
canvas.drawCircle(350, 350, 250, paint); //画圆
canvas.drawOval(100, 100, 600, 300, paint); //画椭圆
canvas.drawArc(100, 100, 600, 600, 0, -90, false, paint); //画弧
canvas.drawBitmap(bitmap, 300, 300, paint); //画位图
canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), new Rect(0, 0, 800, 400), paint); //画位图,第一个Rect指定绘制范围,第二个Rect指定绘制位置
//画任意几何线条
Path path = new Path();
path.addArc(200, 200, 400, 400, -225, 225);
path.arcTo(400, 200, 600, 400, -180, 225, false);
path.lineTo(400, 542);
canvas.drawPath(path, paint);
//通过Picture绘制
//Picture对象不存储实际像素,只保存绘制过程
Picture picture = new Picture();
Canvas c = picture.beginRecording(400, 400);
c.drawLine(100, 100, 400, 400, paint);
picture.endRecording();
canvas.drawPicture(picture);
canvas.drawPicture(picture, new Rect(0, 0, 400, 400));
//通过PictureDrawable绘制
PictureDrawable pd = new PictureDrawable(picture);
pd.setBounds(new Rect(0, 0, 400, 400));
pd.draw(canvas);
//绘制文字
paint.setTextSize(ViewUtil.toPx(getContext(), 60));
paint.setTypeface(FontUtil.getTypeface(this, FontUtil.Font.LIBIAN));
canvas.drawText("Hello", 300, 300, paint); //左下角坐标
//沿着指定路径绘制文字
Path path = new Path();
path.addArc(200, 200, 400, 400, -225, 225);
path.arcTo(400, 200, 600, 400, -180, 225, false);
path.lineTo(400, 542);
paint.setTextSize(ViewUtil.toPx(getContext(), 20));
paint.setTypeface(Typeface.create(Typeface.MONOSPACE, Typeface.NORMAL));
canvas.drawPath(path, paint);
canvas.drawTextOnPath("Hello Hello Hello", path, 300, 300, paint);
//通过Color清空画布
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//通过Paint清空画布
paint.setColor(Color.TRANSPARENT);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
更多推荐
已为社区贡献3条内容
所有评论(0)