【Android自定義View】繪圖之Path篇(二)

歡子發表於2019-05-10

前言

上一篇,我們說了繪製基本的幾何圖形,這一篇我們說說繪製路徑(Path)

這裡主要用到的方法是 canvas.drawPath(path, paint);

  • 1.直線

主要用到的方法

moveTo(float x, float y) 起始點

lineTo(float x, float y)當前點直線的結束點,也是下條直線的起始點

close()閉合

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setStyle(Paint.Style.FILL);

        Path path = new Path();
        path.moveTo(20, 20); //設定起始點
        path.lineTo(300, 100);//第1條直線
        path.lineTo(300, 300);//第2條直線
        path.close();//閉合
        canvas.drawPath(path, paint);
複製程式碼

image.png

  • 2.矩形

addRect(RectF rect, Direction dir)

addRect(float left, float top, float right, float bottom, Direction dir)

RectFfloat left, float top, float right, float bottom是一樣的,Direction有2種方式,CWCCW,其中CW表示順時針,CCW表示逆時針

        Path ccw = new Path();
        RectF rect1 =  new RectF(50, 50, 300, 300);
        ccw.addRect(rect1, Path.Direction.CCW);//逆時針

        Path cw = new Path();
        RectF rect2 =  new RectF(350, 50, 600, 300);
        cw.addRect(rect2, Path.Direction.CW);//順時針

        canvas.drawPath(ccw, paint);
        canvas.drawPath(cw, paint);
複製程式碼

image.png
看上去好像是一樣的?其實是過程不一樣,結果一樣,細節的話,我們後面再說。

  • 3.圓角矩形

addRoundRect(RectF rect, float rx, float ry, Direction dir)

addRoundRect(float left, float top, float right, float bottom, float rx, float ry, Direction dir)

addRoundRect(RectF rect, float[] radii, Direction dir)

addRoundRect(float left, float top, float right, float bottom, float[] radii,Direction dir)

其中,radii只能傳入8個數值,多於8個不會有效果,rx表示x方向的半徑,ry表示y方向的半徑

        Path ccw = new Path();
        RectF rect1 = new RectF(50, 50, 500, 500);
        float[] radii = {5, 20, 50, 100, 150, 200, 250, 300};
        ccw.addRoundRect(rect1, radii, Path.Direction.CCW);


        Path cw = new Path();
        RectF rect2 = new RectF(550, 50, 1000, 500);
        cw.addRoundRect(rect2, 10, 100, Path.Direction.CCW);

        canvas.drawPath(ccw, paint);

        paint.setColor(Color.BLUE);
        canvas.drawPath(cw, paint);
複製程式碼

image.png

  • 4.圓形

addCircle(float x, float y, float radius, Direction dir)

        Path cw = new Path();
        cw.addCircle(200, 200, 100, Path.Direction.CW);
        canvas.drawPath(cw, paint);
複製程式碼

image.png

  • 5.橢圓

addOval(RectF oval, Direction dir)

addOval(float left, float top, float right, float bottom, Direction dir)

        Path cw = new Path();
        RectF rect1 = new RectF(50, 50, 500, 300);
        cw.addOval(rect1, Path.Direction.CW);
        canvas.drawPath(cw, paint);
複製程式碼

image.png

  • 6.弧形

addArc(RectF oval, float startAngle, float sweepAngle)

addArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle)

startAngle 開始的角度,X軸正方向為0度

sweepAngel 持續的度數

        Path cw = new Path();
        RectF rect1 = new RectF(50, 50, 500, 300);
        cw.addArc(rect1, 0, 180);
        canvas.drawPath(cw, paint);
複製程式碼

image.png

  • 7.帶軌跡的文字

drawTextOnPath(String text, Path path, float hOffset,float vOffset, Paint paint)

drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)

index 表示開始位置

count 表示結束位置,可以對文字進行擷取

hOffset 表示繪製位置離開始位置的偏移量

vOffset 表示繪製位置離開路徑的偏移量

        String text = "ABCDEFGHIJKLMNOPQRSTYVWXYZ";
        paint.setTextSize(72);

        Path ccw = new Path();
        RectF rect1 = new RectF(50, 50, 500, 500);
        ccw.addRect(rect1, Path.Direction.CCW);//逆時針

        Path cw = new Path();
        RectF rect2 = new RectF(650, 50, 1100, 500);
        cw.addRect(rect2, Path.Direction.CW);//順時針

        canvas.drawPath(ccw, paint);
        canvas.drawPath(cw, paint);

        canvas.drawTextOnPath(text, ccw, 0, 0, paint);

        canvas.drawTextOnPath(text, cw, 0, 0, paint);
複製程式碼

image.png
這裡就可以明顯的看出CCWCW的區別了

修改hOffsetvOffset

        String text = "ABCDEFGHIJKLMNOPQRSTYVWXYZ";
        paint.setTextSize(72);

        Path ccw = new Path();
        RectF rect1 = new RectF(50, 50, 500, 500);
        ccw.addRect(rect1, Path.Direction.CCW);//逆時針

        Path cw = new Path();
        RectF rect2 = new RectF(650, 50, 1100, 500);
        cw.addRect(rect2, Path.Direction.CW);//順時針

        canvas.drawPath(ccw, paint);
        canvas.drawPath(cw, paint);

        canvas.drawTextOnPath(text, ccw, 100, 0, paint);

        canvas.drawTextOnPath(text, cw, 0, 100, paint);
複製程式碼

image.png

可以看到,hOffset 表示繪製位置離開始位置的偏移量,vOffset 表示繪製位置離開路徑的偏移量,vOffset 越大,離圓心越近。

你的認可,是我堅持更新部落格的動力,如果覺得有用,就請點個贊,謝謝

相關文章