为了账号安全,请及时绑定邮箱和手机立即绑定

Android 高级UI8 Canvas的使用

标签:
Android

1.Canvas基本绘制

1.1drawLine 绘制直线
public class MyView extends View {    public MyView(Context context) {        super(context);
    }    public MyView(Context context,
            @Nullable AttributeSet attrs) {        super(context, attrs);        /**
         *Paint最好在结构体中绘制
         *这里创建
         */
        //Paint paint = new Paint();
    }    @Override
    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //绘制直线
        /**
         * Paint最好在结构体中绘制
         * 避免在onDraw中重复绘制
         */
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(5);

        canvas.drawLine(0, 0, 300, 300, paint);
    }
}

webp

1.2drawLines 绘制虚线
    Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(5);        //canvas.drawLine(0, 0, 300, 300, paint);
        float[] pts = {0,0,100,100,200,200,300,300};
        canvas.drawLines(pts,paint);
1.3drawPoints 绘制点

webp

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(10);        //canvas.drawLine(0, 0, 300, 300, paint);
        float[] pts = {0,0,100,100,200,200,300,300};        //canvas.drawLines(pts,paint);
        canvas.drawPoints(pts,paint);

webp

1.4drawRect 绘制矩形
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(10);
        RectF rectF = new RectF(100,100,400,500);
        canvas.drawRect(rectF,paint);

webp

1.5drawRoundRect 绘制圆角矩形
      Paint paint = new Paint();
      paint.setColor(Color.RED);
      paint.setStyle(Style.FILL);
      paint.setStrokeWidth(10);
      RectF rectF = new RectF(100,100,400,500);      /**
       * x-radius , y-radius圆角的半径
       * 半径30
       */
      canvas.drawRoundRect(rectF,30,30,paint);

webp

1.6drawCircle 绘制圆形
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(10);        /**
         * @param cx 圆心点x坐标
         * @param cy 圆心点y坐标
         * @param radius 圆点半径
         * @param paint The paint used to draw the circle
         */
        canvas.drawCircle(200,200,100,paint);

webp

1.7drawOval绘制椭圆
        Paint paint = new Paint();
        paint.setColor(Color.RED);        /**
         * 设置空心圆
         */
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);


        RectF rectF = new RectF(100,100,400,500);

        canvas.drawRect(rectF,paint);

        paint.setColor(Color.GREEN);        /**
         * 默认内切圆
         */
        canvas.drawOval(rectF,paint);

webp

1.8drawArc绘制弧线
     Paint paint = new Paint();
        paint.setColor(Color.RED);        /**
         * 设置空心圆
         */
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);

        RectF rectF = new RectF(100, 100, 400, 500);

        canvas.drawRect(rectF, paint);

        paint.setColor(Color.GREEN);        /**
         * @param oval The bounds of oval used to define the shape and size of the arc
         * @param startAngle 起始角度,相对X轴正方向
         * @param sweepAngle 画多少角度的弧度
         * @param useCenter boolean,false:只有一个纯弧线 true:闭合的边
         * @param paint The paint used to draw the arc
         */
        canvas.drawArc(rectF,0,90,true,paint);

webp

1.9drawPath 绘制路径
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);

        Path path = new Path();
        path.moveTo(100, 100);//画笔落笔的位置
        //移动
        path.lineTo(200,100);
        path.lineTo(200,200);



        canvas.drawPath(path, paint);        //圆角矩形路径

        Path path2 = new Path();
        path2.moveTo(300, 100);//画笔落笔的位置
        //移动
        path2.lineTo(400,100);
        path2.lineTo(400,200);        //自动闭合
        path2.close();

        canvas.drawPath(path2, paint);

webp

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);

        Path path = new Path();


        RectF rectF = new RectF(100, 100, 400, 500);        //圆角矩形路径
        float radius[] ={10,10,10,10,10,10,90,90};
        path.addRoundRect(rectF,radius,Direction.CCW);
        canvas.drawPath(path,paint);

webp

1.10区域迭代器RegionIterator的使用
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);

        RectF rectF = new RectF(100, 100, 400, 500);

        Region region = new Region(100, 100, 200, 200);


        Path path = new Path();
        path.addOval(rectF,Direction.CCW);

        Region region1 = new Region();        //path的矩形与椭圆取交集的结果
        region1.setPath(path,region);        //创建一块矩形的区域
        //结合区域迭代器的使用
        RegionIterator iterator = new RegionIterator(region1);

        Rect rect = new Rect();        while (iterator.next(rect)) {
            canvas.drawRect(rect, paint);
        }

webp

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(1);

        RectF rectF = new RectF(100, 100, 400, 500);

        Region region = new Region(100, 100, 400, 500);


        Path path = new Path();
        path.addOval(rectF,Direction.CCW);

        Region region1 = new Region();        //path的矩形与椭圆取交集的结果
        region1.setPath(path,region);        //创建一块矩形的区域
        //结合区域迭代器的使用
        RegionIterator iterator = new RegionIterator(region1);

        Rect rect = new Rect();        while (iterator.next(rect)) {
            canvas.drawRect(rect, paint);
        }

webp

注意:灰色是屏幕截图的原因

2.Canvas变换技巧

2.1translate 平移
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);



        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);

        RectF rectF = new RectF(100, 100, 400, 500);
        canvas.drawRect(rectF, paint);
        paint.setColor(Color.BLUE);        //将画布平移
        canvas.translate(50, 50);        //当canvas执行drawXXX的时候就会创建一个新的画布,但是还是会沿用之前设置的平移变换。不可逆的。(save和restore)
        canvas.drawRect(rectF, paint);

webp

2.2scale缩放
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);
        RectF r = new RectF(0,0,400,500);
        canvas.drawRect(r,paint);
        paint.setColor(Color.BLUE);        /**
         * sx,sy:分别对x/y方向的一个缩放系数
         * 画布的缩放会导致里面所有的绘制的东西否会有一个缩放的效果
         */
        canvas.scale(1.5f,0.5f);
        canvas.drawRect(r,paint);

webp

2.3rotate旋转
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);
    
        RectF rectF = new RectF(200, 200, 400, 500);
        canvas.drawRect(rectF, paint);
        paint.setColor(Color.BLUE);        /**
         * canvas.rotate(45,200, 200);
         * 以(200,200)为原点旋转
         */
        canvas.rotate(45, 200, 200);
        canvas.drawRect(rectF, paint);

webp

2.4skew斜拉画布
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);

        RectF r = new RectF(100,100,400,500);
        canvas.drawRect(r,paint);
        paint.setColor(Color.BLUE);        //sx,sy倾斜度:X轴方向上倾斜60度,tan60=根号3
        canvas.skew(1.73f,0);
        canvas.drawRect(r,paint);

webp

2.5clipRect裁剪
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(15);
        RectF r = new RectF(200,200,400,500);
        canvas.drawRect(r,paint);
        paint.setColor(Color.BLUE);
        canvas.clipRect(new Rect(250,250,300,400));
        canvas.drawColor(Color.GREEN);

webp



作者:香沙小熊
链接:https://www.jianshu.com/p/13073ae50e4d


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消