自定义View之画笔Paint

2022-05-23  本文已影响0人  Small_Cake

参考:https://blog.csdn.net/harvic880925/article/details/51010839

1.基本属性

方法 名称 取值说明
setAntiAlias(boolean aa) 抗锯齿 trueflase
setColor(@ColorInt int color) 颜色 Color.RED,Color.GREEN...
setStyle(Style style); 填充 FILL填充内部,STROKE仅描边,FILL_AND_STROKE
setStrokeWidth(float width); 笔宽 1234567...Max
setShadowLayer (float radius, float dx, float dy, int color) 阴影 radius阴影的倾斜度dx水平位移dy垂直位移
setPathEffect(PathEffect effect) 路径样式 ComposePathEffect, CornerPathEffect, DashPathEffect, DiscretePathEffect, PathDashPathEffect, SumPathEffect

2.特殊属性

方法 名称 取值说明
setStrokeCap(Paint.Cap cap) 线帽 Cap.BUTTCap.SQUARECap.ROUND
setStrokeJoin(Paint.Join join) 线段连接样式 Join.MITERJoin.RoundJoin.BEVEL
setPathEffect(PathEffect effect) 路径样式 取值类型是所有派生自PathEffect的子类
        Paint paint = new Paint();
        paint.setStrokeWidth(80);
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);

        paint.setStrokeCap(Paint.Cap.BUTT);
        canvas.drawLine(100,200,400,200,paint);
        paint.setStrokeCap(Paint.Cap.SQUARE);
        canvas.drawLine(100,400,400,400,paint);
        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawLine(100,600,400,600,paint);

        //垂直画出x=100这条线
        paint.reset();
        paint.setStrokeWidth(2);
        paint.setColor(Color.RED);
        canvas.drawLine(100,100,100,800,paint);
        canvas.drawLine(400,100,400,800,paint);

加上线帽后头尾都多了一帽子
        Paint paint = new Paint();
        paint.setStrokeWidth(100);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);

        Path path  = new Path();
        path.moveTo(100,200);
        path.lineTo(400,200);
        path.lineTo(400,400);
        paint.setStrokeJoin(Paint.Join.MITER);
        canvas.drawPath(path,paint);

        path.moveTo(100,500);
        path.lineTo(400,500);
        path.lineTo(400,700);
        paint.setStrokeJoin(Paint.Join.ROUND);
        canvas.drawPath(path,paint);

        path.moveTo(100,800);
        path.lineTo(400,800);
        path.lineTo(400,1000);
        paint.setStrokeJoin(Paint.Join.BEVEL);
        canvas.drawPath(path,paint);
分别为MITER,ROUND,BEVEL
分别为MITER,ROUND,BEVEL,转角不为90的情况
方法 名称 取值说明
CornerPathEffect 圆形拐角效果 public CornerPathEffect(float radius)radius:即当前连接两条直线所使用的圆的半径
DashPathEffect 虚线效果 public DashPathEffect(float intervals[], float phase)intervals[]:表示组成虚线的各个线段的长度;整条虚线就是由intervals[]中这些基本线段循环组成的。比如,我们定义new float[] {20,10};那这个虚线段就是由两段线段组成的,第一个可见的线段长为20,每二个线段不可见,长度为10。
DiscretePathEffect 离散路径效果 public DiscretePathEffect(float segmentLength, float deviation)其中segmentLength表示将原来的路径切成多长的线段,deviation表示被切成的每个小线段的偏移距离
PathDashPathEffect 印章路径效果 public PathDashPathEffect(Path shape, float advance, float phase,Style style)
ComposePathEffect 合成路径效果 public ComposePathEffect(PathEffect outerpe, PathEffect innerpe)显示两种效果的一条组合路径
SumPathEffect 求和路径效应 public SumPathEffect(PathEffect first, PathEffect second)两种效果的两条路径重叠显示

Path shape:表示印章路径,比如我们下面示例中的三角形加右上角一个点;
float advance:表示两个印章路径间的距离,很容易理解,印章间距离越大,间距就越大。
float phase:路径绘制偏移距离,与上面DashPathEffect中的float phase参数意义相同
Style style:表示在遇到转角时,如何操作印章以使转角平滑过渡,取值有:Style.ROTATE,Style.MORPH,Style.TRANSLATE;Style.ROTATE表示通过旋转印章来过渡转角;Style.MORPH表示通过变形印章来过渡转角;Style.TRANSLATE表示通过位移来过渡转角

        Path path = new Path();
        path.moveTo(100,600);
        path.lineTo(400,100);
        path.lineTo(700,600);

        canvas.drawPath(path,paint);

        paint.setColor(Color.RED);
        paint.setPathEffect(new CornerPathEffect(100));
        canvas.drawPath(path,paint);

        paint.setColor(Color.LTGRAY);
        paint.setPathEffect(new CornerPathEffect(300));
        canvas.drawPath(path,paint);
CornerPathEffect圆形拐角
        Path path = new Path();
        path.moveTo(100,600);
        path.lineTo(300,100);
        path.lineTo(600,600);

        canvas.drawPath(path,paint);
        paint.setColor(Color.RED);

        //使用DashPathEffect画线段
        paint.setPathEffect(new DashPathEffect(new float[]{20,10,100,100},0));
        canvas.translate(20,0);
        canvas.drawPath(path,paint);

        //画同一条线段,偏移值为15
        paint.setPathEffect(new DashPathEffect(new float[]{20,10,50,100},15));
        paint.setColor(Color.BLACK);
        canvas.translate(20,0);
        canvas.drawPath(path,paint);
DashPathEffect虚线段
        Paint paint = getPaint();
        Path path = getPath();
//第一条原生Path
        canvas.drawPath(path,paint);
//第二条Path
        canvas.translate(0,200);
        paint.setPathEffect(new DiscretePathEffect(2,5));
        canvas.drawPath(path,paint);
//第三条Path
        canvas.translate(0,200);
        paint.setPathEffect(new DiscretePathEffect(6,5));
        canvas.drawPath(path,paint);
//第四条Path
        canvas.translate(0,200);
        paint.setPathEffect(new DiscretePathEffect(6,15));
        canvas.drawPath(path,paint);
DiscretePathEffect离散路径效应
        Paint paint = getPaint();
        Path path = new Path();
//画出原始路径
        path.moveTo(100,600);
        path.lineTo(400,100);
        path.lineTo(700,600);
        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,100);
        paint.setPathEffect(new PathDashPathEffect(stampPath,35,0, PathDashPathEffect.Style.TRANSLATE));
        canvas.drawPath(path,paint);
PathDashPathEffect印章路径效果
       //画原始路径
        Paint paint = getPaint();
        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);
ComposePathEffect和SumPathEffect

3.字体相关

方法 名称 取值说明
setTextSize(float textSize) 文字大小 float
setFakeBoldText(boolean fakeBoldText) 粗体文字 boolean
setStrikeThruText(boolean strikeThruText) 删除线 boolean
setUnderlineText(boolean underlineText) 下划线 boolean
setTextAlign(Paint.Align align) 文字基线位置 Align.LEFT,Align.CENTER,Align.RIGHT,这里的位置是说的绘制起点x,y相对于文字的位置,而不是要绘制的文字对应起点x,y的位置
setTextScaleX(float scaleX) 水平拉伸 float
setTextSkewX(float skewX) 水平倾斜度 float
setTypeface(Typeface typeface) 字体样式 android.graphics.Typeface
setLinearText(boolean linearText) 文本线性缓存 boolean开启后会影响显示速度
setSubpixelText(boolean subpixelText) 亚像素 可以增加字体清晰度,实测效果无明显变化

4.图像处理和measure测量相关

方法 名称 取值说明
setShader(Shader shader) 作色器 https://www.cnblogs.com/tianzhijiexian/p/4298660.html
setShadowLayer(float radius, float dx, float dy, int shadowColor) 阴影 https://www.jianshu.com/p/2f1024f9c554
setDither(boolean dither) 防抖动 https://blog.csdn.net/lovexieyuan520/article/details/50732023
setColorFilter(ColorFilter filter) 颜色过滤 https://blog.csdn.net/aigestudio/article/details/41316141
setXfermode(Xfermode xfermode) 图像混合模式 https://www.cnblogs.com/tianzhijiexian/p/4297172.html
setFilterBitmap(boolean filter) 位图滤波处理 http://www.360doc.com/content/16/0821/22/26833809_584860347.shtml
clearShadowLayer() 清除阴影 清除阴影
上一篇下一篇

猜你喜欢

热点阅读