Canvas—路径的绘制

  • Post author:
  • Post category:其他


绘制路径

绘制路径的方法为:

void drawPath(Path path,Paint paint);

1.直线路径

Path类有个close方法,当绘制的直线没有闭合的时候,调用close方法会将收尾连接起来。moveTo方法表示该条直线的起点,lineTo表示该条直线的终点。下面看一下自行闭合路径和没有闭合路径而调用close方法的情形。

Path path=new Path();
        path.moveTo(400,50);
        path.lineTo(400,150);
        path.moveTo(400,150);
        path.lineTo(800,150);
        path.moveTo(800,150);
        path.lineTo(400,50);
        canvas.drawPath(path,mPaint);

        Path path2=new Path();
        path2.moveTo(400,200);
        path2.lineTo(400,400);
        path2.lineTo(800,400);
        path2.close();
        canvas.drawPath(path2,mPaint);

2.绘制矩形路径

void addRect (float left, float top, float right, float bottom, Path.Direction dir)
void addRect (RectF rect, Path.Direction dir)

参数中的矩形和之前没有区别,就是多了一个Path.Direction,这个参数表示绘制的矩形路径是按照顺时针还是逆时针创建的。

Path.Direction.CCW 逆时针创建

Path.Direction.CW 顺时针创建

 Path pathRect1=new Path();
        pathRect1.addRect(50,200,150,250,Path.Direction.CCW);  //逆时针创建
        canvas.drawPath(pathRect1,mPaint);

        Path pathRect2=new Path();
        pathRect2.addRect(50,260,150,310,Path.Direction.CW);  //顺时针创建
        canvas.drawPath(pathRect2,mPaint);

从绘制的结果来看,都是两个矩形,没有什么不一样的地方,但是如果我们在这个路径上绘制文字,那么得到的效果是不一样的。

Path pathRect1=new Path();
        pathRect1.addRect(50,500,550,700,Path.Direction.CCW);  //逆时针创建
        canvas.drawPath(pathRect1,mPaint);

        Path pathRect2=new Path();
        pathRect2.addRect(600,500,1100,700,Path.Direction.CW);  //顺时针创建
        canvas.drawPath(pathRect2,mPaint);

        //在路径上绘制文字
        String notice="你说你有点难追";
        canvas.drawTextOnPath(notice,pathRect1,0,20,mPaint);
        canvas.drawTextOnPath(notice,pathRect2,0,20,mPaint);

3.绘制圆角矩形路径

绘制圆角路径提供了四种方法:

void addRoundRect(float left,float top,float right,float bottom,float []radii,Path.Direction dir)
void addRoundRect(float left,float top,float right,float bottom,float rx,float ry,Path.Direction dir)
void addRoundRect (RectF rect, float[] radii, Path.Direction dir)
void addRoundRect (RectF rect, float rx, float ry, Path.Direction dir)

前两个方法必须在API大于等于21的时候才能使用,一般用后两个。

//绘制圆角矩形路径
        Path roundRectPath1=new Path();
        Path roundRectPath2=new Path();

        float []pts={20,20,30,30,40,40,20,20};

        //API大于等于21使用
        //roundRectPath1.addRoundRect(50,750,550,950,20,20, Path.Direction.CCW);
        //roundRectPath2.addRoundRect(600,750,900,900,pts,Path.Direction.CW);
        RectF rectF1=new RectF(50,750,550,950);
        RectF rectF2=new RectF(600,750,900,900);

        roundRectPath1.addRoundRect(rectF1,20,20, Path.Direction.CCW);
        roundRectPath2.addRoundRect(rectF2,pts, Path.Direction.CW);

        canvas.drawPath(roundRectPath1,mPaint);
        canvas.drawPath(roundRectPath2,mPaint);

4.绘制圆形路径

void addCircle (float x, float y, float radius, Path.Direction dir)
//绘制圆形路径
        Path circlePath1=new Path();
        Path circlePath2=new Path();

        circlePath1.addCircle(300,1200,100, Path.Direction.CCW);
        circlePath2.addCircle(500,1200,100, Path.Direction.CW);

        canvas.drawPath(circlePath1,mPaint);
        canvas.drawPath(circlePath2,mPaint);

5.椭圆路径

void addOval (RectF oval, Path.Direction dir)
Path ovalPath=new Path();

        //API大于等于21时使用
        //ovalPath.addOval(50,1350,400,1500, Path.Direction.CCW);
        RectF rectF=new RectF(50,1350,400,1500);
        ovalPath.addOval(rectF, Path.Direction.CCW);
        canvas.drawPath(ovalPath,mPaint);

6.绘制弧线路径

void addArc (RectF oval, float startAngle, float sweepAngle)
//绘制弧线路径
        Path arcPath1=new Path();
        Path arcPath2=new Path();
        //API大于等于21时可用
        //arcPath.addArc(500,1350,750,1600,0,270);
        RectF rectF3=new RectF(500,1350,750,1600);
        RectF rectF4=new RectF(800,1350,1050,1600);
        arcPath1.addArc(rectF3,0,270);
        arcPath2.addArc(rectF4,0,-270);

        canvas.drawPath(arcPath1,mPaint);
        canvas.drawPath(arcPath2,mPaint);

至此,上述的路径绘制来一个效果图:

这里写图片描述



版权声明:本文为RainMcCom原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。