自定義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填充內(nèi)部,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.BUTT,Cap.SQUARE,Cap.ROUND
setStrokeJoin(Paint.Join join) 線段連接樣式 Join.MITER,Join.Round,Join.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,轉(zhuǎn)角不為90的情況
  • setPathEffect(PathEffect effect)路徑樣式
方法 名稱 取值說明
CornerPathEffect 圓形拐角效果 public CornerPathEffect(float radius)radius:即當前連接兩條直線所使用的圓的半徑
DashPathEffect 虛線效果 public DashPathEffect(float intervals[], float phase)intervals[]:表示組成虛線的各個線段的長度;整條虛線就是由intervals[]中這些基本線段循環(huán)組成的。比如,我們定義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 求和路徑效應(yīng) public SumPathEffect(PathEffect first, PathEffect second)兩種效果的兩條路徑重疊顯示

Path shape:表示印章路徑,比如我們下面示例中的三角形加右上角一個點;
float advance:表示兩個印章路徑間的距離,很容易理解,印章間距離越大,間距就越大。
float phase:路徑繪制偏移距離,與上面DashPathEffect中的float phase參數(shù)意義相同
Style style:表示在遇到轉(zhuǎn)角時,如何操作印章以使轉(zhuǎn)角平滑過渡,取值有:Style.ROTATE,Style.MORPH,Style.TRANSLATE;Style.ROTATE表示通過旋轉(zhuǎn)印章來過渡轉(zhuǎn)角;Style.MORPH表示通過變形印章來過渡轉(zhuǎn)角;Style.TRANSLATE表示通過位移來過渡轉(zhuǎn)角

        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離散路徑效應(yīng)
        Paint paint = getPaint();
        Path path = new Path();
//畫出原始路徑
        path.moveTo(100,600);
        path.lineTo(400,100);
        path.lineTo(700,600);
        canvas.drawPath(path,paint);

//構(gòu)建印章路徑
        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);

//僅應(yīng)用圓角特效的路徑
        canvas.translate(0,200);
        CornerPathEffect cornerPathEffect = new CornerPathEffect(100);
        paint.setPathEffect(cornerPathEffect);
        canvas.drawPath(path,paint);

//僅應(yīng)用虛線特效的路徑
        canvas.translate(0,200);
        DashPathEffect dashPathEffect = new DashPathEffect(new float[]{2,5,10,10},0);
        paint.setPathEffect(dashPathEffect);
        canvas.drawPath(path,paint);

//利用ComposePathEffect先應(yīng)用圓角特效,再應(yīng)用虛線特效
        canvas.translate(0,200);
        ComposePathEffect composePathEffect = new ComposePathEffect(dashPathEffect,cornerPathEffect);
        paint.setPathEffect(composePathEffect);
        canvas.drawPath(path,paint);

//利用SumPathEffect,分別將圓角特效應(yīng)用于原始路徑,然后將生成的兩條特效路徑合并
        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.字體相關(guān)

方法 名稱 取值說明
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相對于文字的位置,而不是要繪制的文字對應(yīng)起點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測量相關(guān)

方法 名稱 取值說明
setShader(Shader shader) 作色器 https://www.cnblogs.com/tianzhijiexian/p/4298660.html
setShadowLayer(float radius, float dx, float dy, int shadowColor) 陰影 http://www.itdecent.cn/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() 清除陰影 清除陰影
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容