Paint类的其它函数

来源:互联网 发布:淘宝店铺被释放怎么办 编辑:程序博客网 时间:2024/05/29 06:37

在之前我们已经接触了Paint类的一些方法,下面是一些之前没有接触过的方法:
setStrokeCap(Paint.Cap cap)
设置线冒样式,取值有Cap.ROUND(圆形线冒)、Cap.SQUARE(方形线冒)、Paint.Cap.BUTT(无线冒)

下面是这些方法的简单使用:

1.setStrokeCap(Paint.Cap cap)

设置线冒样式,取值有Cap.ROUND(圆形线冒)、Cap.SQUARE(方形线冒)、Paint.Cap.BUTT(无线冒)
直接用一个例子来体验一下:

  //设置线冒模式        paint.setStrokeWidth(20);        //无线冒        paint.setStrokeCap(Paint.Cap.BUTT);        canvas.drawLine(50,50,450,50,paint);        //圆形线冒        paint.setColor(Color.GREEN);        paint.setStrokeCap(Paint.Cap.ROUND);        canvas.drawLine(50,150,450,150,paint);        //方形线冒        paint.setColor(Color.BLUE);        paint.setStrokeCap(Paint.Cap.SQUARE);        canvas.drawLine(50,250,450,250,paint);        //绘制垂直的线        paint.setStrokeWidth(2);        paint.setColor(Color.BLACK);        canvas.drawLine(50,0,50,800,paint);

效果如下:
这里写图片描述

2.setStrokeJoin(Paint.Join join)
设置线条之间的连接方式,参数取值有:

Join.MITER(结合处为锐角)
Join.Round(结合处为圆弧)
Join.BEVEL(结合处为直线)

  //设置线条之间的连接方式        paint.setStrokeWidth(30);        paint.setStrokeJoin(Paint.Join.MITER);        Path path1=new Path();        path1.moveTo(100,100);        path1.lineTo(400,100);        path1.lineTo(400,400);        canvas.drawPath(path1,paint);        paint.setStrokeJoin(Paint.Join.BEVEL);        Path path2=new Path();        path2.moveTo(100,200);        path2.lineTo(300,200);        path2.lineTo(300,300);        canvas.drawPath(path2,paint);        paint.setStrokeJoin(Paint.Join.ROUND);        Path path3=new Path();        path3.moveTo(100,300);        path3.lineTo(200,300);        path3.lineTo(200,500);        canvas.drawPath(path3,paint);

效果如下:
这里写图片描述

3.setPathEffect(PathEffect effect)

设置路径样式;取值类型是所有派生自PathEffect的子类

(1)CornerPathEffect—-圆形拐角效果

public CornerPathEffect(float radius) 

它只有一个参数radius:即当前连接两条直线所使用的圆的半径。
通过一个例子看一下效果:

 paint.setStrokeWidth(2);        Path path=new Path();        path.moveTo(100,600);        path.lineTo(400,100);        path.lineTo(800,700);        //不加拐角        canvas.drawPath(path,paint);        //圆形拐角的半径为100的情况        paint.setPathEffect(new CornerPathEffect(100));        paint.setColor(Color.GREEN);        canvas.drawPath(path,paint);        //圆形拐角的半径为200的情况        paint.setPathEffect(new CornerPathEffect(200));        paint.setColor(Color.BLUE);        canvas.drawPath(path,paint);

效果如下:
这里写图片描述

(2)DashPathEffect—-虚线效果
函数声明如下:

public DashPathEffect(float intervals[], float phase) 

intervals[]:表示组成虚线的各个线段的长度;整条虚线就是由intervals[]中这些基本线段循环组成的。比如,我们定义new float[] {20,10};那这个虚线段就是由两段线段组成的,第一个可见的线段长为20,每二个线段不可见,长度为10;对于intervals[]数组的有两个限定:
*长度必须大于等于2;因为必须有一个实线段和一个空线段来组成虚线。
个数必须为偶数,如果是基数,最后一个数字将被忽略;这个很好理解,因为一组虚线的组成必然是一个实线和一个空线成对组成的。*

phase:开始绘制的偏移值

  paint.setStrokeWidth(2);        Path path=new Path();        path.moveTo(100,400);        path.lineTo(300,100);        path.lineTo(500,600);        //偏移量为0        paint.setPathEffect(new DashPathEffect(new float[]{20,10},0));        canvas.drawPath(path,paint);        //偏移量为15        canvas.translate(0,100);        paint.setColor(Color.GREEN);        paint.setPathEffect(new DashPathEffect(new float[]{20,10},15));        canvas.drawPath(path,paint);        //偏移量为30        canvas.translate(0,100);        paint.setColor(Color.BLACK);        paint.setPathEffect(new DashPathEffect(new float[]{40,20,20,10},30));        canvas.drawPath(path,paint);

效果如下:
这里写图片描述

(3)DiscretePathEffect——离散路径效果

构造函数如下:

public DiscretePathEffect(float segmentLength, float deviation)  

第一个参数表示原来的路径被切成多长的线段。这个值越小,则切成的线段越多;反之,这个值越大,则切成的线段越少。
第二个参数表示每条线段 可以偏移的距离值。越大,越显得凌乱;值越小,线段的可偏移量越小。
下面看 一个例子:

 paint.setStrokeWidth(3);        Path path=getPath();        //绘制原来的路径        canvas.drawPath(path,paint);        canvas.translate(0,200);        paint.setPathEffect(new DiscretePathEffect(2,5));        canvas.drawPath(path,paint);        canvas.translate(0,200);        paint.setPathEffect(new DiscretePathEffect(6,5));        canvas.drawPath(path,paint);        canvas.translate(0,200);        paint.setPathEffect(new DiscretePathEffect(6,15));        canvas.drawPath(path,paint);
 private Path getPath(){        Path path = new Path();        // 定义路径的起点        path.moveTo(0, 0);        // 定义路径的各个点        for (int i = 0; i <= 40; i++) {            path.lineTo(i*35, (float) (Math.random() * 150));        }        return path;    }

效果如下:
这里写图片描述

(4)PathDashPathEffect—印章路径效果

作用就是:将另一个路径绘制成的印章沿着指定路径绘制。

public PathDashPathEffect(Path shape, float advance, float phase,Style style)

参数的含义如下:
Path shape:表示印章路径
float advance:印章路径之间的距离,这个值越大,就表示之间的间隔越大
float phase:路径绘制偏移距离
Style style:表示遇到转角时,如何操作印章以使转角平滑过渡。取值有三个:Style.ROTATE,Style.MORPH,Style.TRANSLATE。Style.ROTATE表示通过旋转印章来过渡转角;Style.MORPH表示通过变形印章来过渡转角;Style.TRANSLATE表示通过位移来过渡转角。

Paint paint = getPaint();  //画出原始路径  Path path  = new Path();  path.moveTo(100,600);  path.lineTo(400,100);  path.lineTo(700,900);  canvas.drawPath(path,paint);  //构建印章路径  Path stampPath  = new Path();  stampPath.moveTo(0,20);  stampPath.lineTo(10,0);  stampPath.lineTo(20,20);  stampPath.close();  stampPath.addCircle(0,0,3, Path.Direction.CCW);  //使用印章路径效果  canvas.translate(0,200);  paint.setPathEffect(new PathDashPathEffect(stampPath,35,0, PathDashPathEffect.Style.TRANSLATE));  canvas.drawPath(path,paint);  

看一下效果:
这里写图片描述

看一下在Style不同的前提下,路径的转角处的变化:

 Path path = getPath();      canvas.drawPath(path,paint);      canvas.translate(0,200);      paint.setPathEffect(new PathDashPathEffect(getStampPath(),35,0, PathDashPathEffect.Style.MORPH));      canvas.drawPath(path,paint);      canvas.translate(0,200);      paint.setPathEffect(new PathDashPathEffect(getStampPath(),35,0, PathDashPathEffect.Style.ROTATE));      canvas.drawPath(path,paint);      canvas.translate(0,200);      paint.setPathEffect(new PathDashPathEffect(getStampPath(),35,0, PathDashPathEffect.Style.TRANSLATE));      canvas.drawPath(path,paint);  
 private Path getStampPath(){        Path path  = new Path();        path.moveTo(0,20);        path.lineTo(10,0);        path.lineTo(20,20);        path.close();        path.addCircle(0,0,3, Path.Direction.CCW);        return path;    }    private Path getPath(){        Path path = new Path();        // 定义路径的起点        path.moveTo(0, 0);        // 定义路径的各个点        for (int i = 0; i <= 40; i++) {            path.lineTo(i*35, (float) (Math.random() * 150));        }        return path;    }

三者的区别,从效果图上可以一眼看出:
这里写图片描述

(5)ComposePathEffect与SumPathEffect
这两个都是用来合并两个特效的,但两者是有区别的:
先看一下ComposePathEffect,它的构造函数如下:

public ComposePathEffect(PathEffect outerpe, PathEffect innerpe)

合并这两个特效是有先后顺序的,首先将inerpe作用于路径,然后在此基础上,将outerpe作用于路径。

看一下SumPathEffect的构造函数:

public SumPathEffect(PathEffect first, PathEffect second)  

分别将两个特效作用于路径,然后将得到的两条路径进行合并,作为终结效果。
通过例子来看一下:

Path path = getPath();  canvas.drawPath(path,paint);  //仅应用圆角特效的路径  canvas.translate(0,200);  CornerPathEffect cornerPathEffect = new CornerPathEffect(100);  paint.setPathEffect(cornerPathEffect);  canvas.drawPath(path,paint);  //仅应用虚线特效的路径  canvas.translate(0,200);  DashPathEffect dashPathEffect = new DashPathEffect(new float[]{2,5,10,10},0);  paint.setPathEffect(dashPathEffect);  canvas.drawPath(path,paint);  //利用ComposePathEffect先应用圆角特效,再应用虚线特效  canvas.translate(0,200);  ComposePathEffect composePathEffect = new ComposePathEffect(dashPathEffect,cornerPathEffect);  paint.setPathEffect(composePathEffect);  canvas.drawPath(path,paint);  //利用SumPathEffect,分别将圆角特效应用于原始路径,然后将生成的两条特效路径合并  canvas.translate(0,200);  paint.setStyle(Paint.Style.STROKE);  SumPathEffect sumPathEffect = new SumPathEffect(cornerPathEffect,dashPathEffect);  paint.setPathEffect(sumPathEffect);  canvas.drawPath(path,paint);  

得到的效果如下:
这里写图片描述