自定义View之画笔Paint

参考: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的子类
  • setStrokeCap(Paint.Cap cap)线帽
        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);

加上线帽后头尾都多了一帽子
  • setStrokeJoin(Paint.Join join)线段连接样式
    Paint paint = new Paint();
    paint.setStrokeWidth(8);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);

      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);
    
        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的情况
  • setPathEffect(PathEffect effect)路径样式
方法 名称 取值说明
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() 清除阴影 清除阴影
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容