Android自定義View之draw系方法

Canvas是Android繪制的基礎(chǔ),onDraw方法提供Canvas類來(lái)幫助開(kāi)發(fā)者繪制內(nèi)容,為了加強(qiáng)自己的印象做的總結(jié),并且相當(dāng)于自己的一份文檔。

繪制的基本函數(shù)介紹:

1,drawCircle(float cx, float cy, float radius, Paint paint)     畫(huà)圓

2,drawRect(float left, float top, float right, float bottom,  Paint paint)   畫(huà)矩形

3,drawPoint(float x, float y, Paint paint)  畫(huà)點(diǎn)

4,drawPoints( float[] pts, int offset, int count, Paint paint)   畫(huà)多個(gè)點(diǎn)

5,drawOval(float left, float top, float right, float bottom, Paint paint)  畫(huà)橢圓

6,drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 畫(huà)線

7,drawLines( float[] pts,  Paint paint) 畫(huà)多條線

8,drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)  圓角矩形

9,drawArc(float left, float top, float right, float bottom, float startAngle,
        float sweepAngle, boolean useCenter, Paint paint)   畫(huà)弧形或者扇形

10,drawPath(@NonNull Path path, @NonNull Paint paint)  畫(huà)路徑

11,drawBitmap(Bitmap bitmap, float x, float y, Paint paint) 畫(huà)圖

12,drawText(String text, float x, float y, Paint paint) 畫(huà)文字

一,圓

image.png

     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     p.setStrokeWidth(20);
     canvas.drawCircle(100,100,50,p);

坐標(biāo)是如何計(jì)算的呢?以圓為例子其他等同,可以看一下我寫(xiě)的關(guān)于View坐標(biāo)系的總結(jié)。


image.png

二, 矩形

image.png

  @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     p.setStrokeWidth(20);
     canvas.drawRect(100,100,300,300,p);
}

三, 點(diǎn)

image.png
 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     p.setStrokeCap(Paint.Cap.ROUND);
     p.setStrokeWidth(20);
     canvas.drawPoint(100,100,p);
}

四,多點(diǎn)

image.png
 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     float[] points = {50, 50, 100, 100, 150, 150};
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     p.setStrokeCap(Paint.Cap.ROUND);
     p.setStrokeWidth(20);
     canvas.drawPoints(points,0,6,p);
}

五,橢圓

image.png

 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     canvas.drawOval(100,100,300,500,p);
}

六,線

image.png

 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     canvas.drawLine(100,100,300,300,p);
}

七,多線

image.png
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     float[] points ={100,100,300,300,300,100,100,300,500,200,600,300,600,300,700,100};
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     canvas.drawLines(points,p);
}

八,畫(huà)圓角矩形

image.png
 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     canvas.drawRoundRect(100,100,800,500,30,30,p);
}

九,畫(huà)弧或扇形

 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     //startAngle從橢圓的三點(diǎn)鐘方向開(kāi)始,順時(shí)針畫(huà)角度,負(fù)數(shù)則為逆時(shí)針,也就是說(shuō)圓心的橫向直徑右側(cè)頂點(diǎn),sweepAngle是角度,360是一圈userCenter是代表是否連接圓心,也就是扇形
     canvas.drawArc(100,100,600,300,0,270,false,p);
}
image.png

參數(shù)改為連接圓心

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     //startAngle從橢圓的三點(diǎn)鐘方向開(kāi)始,順時(shí)針畫(huà)角度,負(fù)數(shù)則為逆時(shí)針,也就是說(shuō)圓心的橫向直徑右側(cè)頂點(diǎn),sweepAngle是角度,360是一圈userCenter是代表是否連接圓心,也就是扇形
     canvas.drawArc(100,100,600,300,0,270,true,p);
}

話說(shuō),如果加上顏色有沒(méi)有點(diǎn)餅狀圖的意思

十,畫(huà)路徑
先來(lái)一個(gè)自畫(huà)像,總結(jié)一下基本的drawpath相關(guān)函數(shù)

image.png

   @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     Path path = new Path();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
    path.addRoundRect(100,100,400,300,30,30,Path.Direction.CCW);
    RectF rect1 = new RectF(150,150,200,200);
    path.arcTo(rect1,-180,180,true);
    RectF rect2 = new RectF(300,150,350,200);
    path.arcTo(rect2,-180,180,true);
    path.arcTo(175,100,325,250,30,120,true);
    path.moveTo(250,300);
    path.lineTo(250,600);
    path.arcTo(100,600,400,800,-180,180,true);
    path.moveTo(100,450);
    path.lineTo(400,450);
    canvas.drawPath(path,p);
}

Path相關(guān)API

1,path.add+圖形

例如上文肖像的臉就是添加的一個(gè)圓角矩形,代碼如下

path.addRoundRect(100,100,400,300,30,30,Path.Direction.CCW);

類似的還有其他的圖形,在這里不贅述了。


image.png

2,path.lineTo(x,y)
從參數(shù)可以看到函數(shù)需要一個(gè)坐標(biāo),也就是你要把線畫(huà)到哪里,以當(dāng)前坐標(biāo)為起點(diǎn)向目標(biāo)點(diǎn)畫(huà)線,目標(biāo)即函數(shù)的x,y。

這里存在一個(gè)疑點(diǎn),哪里是當(dāng)前的位置,添加路徑的時(shí)候我怎么知道繪制的終點(diǎn)也就是上個(gè)閉合圖形的結(jié)束點(diǎn)在哪里?

弧形的起始點(diǎn),我們剛才已經(jīng)說(shuō)過(guò)了,是3點(diǎn)鐘方向,順時(shí)針畫(huà)弧線,那矩形呢?做個(gè)小驗(yàn)證吧。

image.png
path.addRect(100,100,400,300,Path.Direction.CW);
     path.lineTo(200,200);//畫(huà)線
     canvas.drawPath(path,p);

可以看到左上頂點(diǎn),很符合邏輯。無(wú)論是CW順時(shí)針和CCW逆時(shí)針都是一樣的。
3,path.moveTo(x,y)
移動(dòng)當(dāng)前繪制點(diǎn),到一個(gè)x,y坐標(biāo)。相當(dāng)于你抬起畫(huà)筆,移動(dòng)到一個(gè)你想要的位置,不再贅述了。

4,path.quadTo & path.cubicTo 畫(huà)曲線

quadTo(float dx1, float dy1, float dx2, float dy2)
cubicTo(float x1, float y1, float x2, float y2,float x3, float y3)

5,path. arcTo 追加弧度

arcTo(float left, float top, float right, float bottom, float startAngle,
        float sweepAngle, boolean forceMoveTo)

前面的參數(shù)很熟悉了,主要說(shuō)一下forceMoveTo這個(gè)boolean值,如果為false,他會(huì)從當(dāng)前位置拖到你開(kāi)始畫(huà)弧度的位置。

舉個(gè)例子:

image.png
     path.addRoundRect(100,100,400,300,30,30,Path.Direction.CCW);
     path.arcTo(150,150,300,300,0,180,false); //畫(huà)弧
     canvas.drawPath(path,p);

我的代碼展示了我要畫(huà)一個(gè)圓角矩形和一個(gè)弧形利用drawpath的方式,
你可以看到紅色箭頭指向的線條是從結(jié)束的畫(huà)筆位置拖到三點(diǎn)鐘的弧形畫(huà)筆方向。forceMoveTo可以看到我們?cè)O(shè)置了false,當(dāng)我們?cè)O(shè)置為true時(shí)就不會(huì)拖動(dòng)過(guò)來(lái)了。我想你已經(jīng)明白了。

image.png

臨時(shí)小結(jié)
上面提到的path相關(guān)函數(shù),有一些都有一個(gè)兄弟函數(shù),函數(shù)名稱頭加r,具體是什么樣子呢,

path.rLlineTo
path.rMoveTo
path.rQuadTo
path.rCubicTo

他們的含義就是不加r的函數(shù)是應(yīng)用的絕對(duì)坐標(biāo),而加r的相對(duì)的是當(dāng)前坐標(biāo)點(diǎn)。

image.png
image.png
     path.addRoundRect(100,100,400,300,30,30,Path.Direction.CCW);
     path.lineTo(200,200);//畫(huà)線

看到了嗎?他是相對(duì)于View坐標(biāo)系的話線,也就是說(shuō)從圖形圓角矩形的結(jié)束點(diǎn)向View坐標(biāo)系的200,200畫(huà)點(diǎn),然后我們看一下把函數(shù)改為rLineTo:

image.png
 path.rLineTo(200,200);//畫(huà)線

看到這里你應(yīng)該已經(jīng)明白了。他是相對(duì)于圓角矩形繪制結(jié)束點(diǎn)的200,200。其他函數(shù)等同,不再贅述了。

6,path.close() 封閉開(kāi)口

image.png
 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     Path path = new Path();
     canvas.drawColor(Color.YELLOW);
     p.setStyle(Paint.Style.STROKE);
     path.lineTo(400,0);//畫(huà)線
     path.close();
     path.arcTo(150,150,300,300,0,180,true);
     path.close();
     canvas.drawPath(path,p);

第一個(gè)例子畫(huà)了兩條線,封閉了開(kāi)口變?yōu)槿切危诙€(gè)例子封閉了弧形,變?yōu)榘雸A形。本質(zhì)上就是為我們畫(huà)了一條線。

十一,Bitmap圖片

image.png
 @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
     Paint p = new Paint();
     canvas.drawColor(Color.YELLOW);    
     Bitmap bitmap =BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)     
     canvas.drawBitmap(bitmap,100,100,p);
}

十一,Text文字

image.png
     canvas.drawColor(Color.YELLOW);
     p.setTextSize(50);
     canvas.drawText("大家好我是aidl",100,100,p);

在我的學(xué)習(xí)過(guò)程中,我跟隨了大神的個(gè)人博客,博客地址在本文的下方,有需要直接去學(xué)習(xí)就好了,還有我的View坐標(biāo)系地址也在本文的下方,在我學(xué)習(xí)的博客中有練習(xí),下面上兩個(gè)練習(xí),代碼還是很拙,但是基本的實(shí)現(xiàn)了效果,下面貼一下完整代碼和效果。

public class MyViewCanvasDraw extends View {


//步長(zhǎng)
private static final int STEP = 50;

//文字?jǐn)?shù)組
private static final String[] texts = {"阿里","騰訊","百度","小米","今日頭條"};

//直方圖值
private static final int[] values = {20,50,30,100,400};

//畫(huà)筆
private Paint p;


public MyViewCanvasDraw(Context context) {
    super(context);
}


public MyViewCanvasDraw(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}


public MyViewCanvasDraw(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

{
    p=new Paint();
    p.setColor(Color.WHITE);
    p.setStrokeWidth(3);
}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //繪制背景
    canvas.drawRGB(86,109,120);

    /**畫(huà)坐標(biāo)軸-----------------**/
    int width =getWidth();

    int height =getHeight();

    //以View的寬的10%來(lái)控制間距畫(huà)線
    int PointPadding = width/10;

    float[] points ={PointPadding,PointPadding,PointPadding,height-PointPadding,PointPadding,height-PointPadding,width-PointPadding,height-PointPadding};

    canvas.drawLines(points,p);
    /**-------------------------**/

    /**畫(huà)直方圖-------------------**/
    p.setStyle(Paint.Style.FILL);
    p.setColor(Color.parseColor("#8BC34A"));
    p.setAntiAlias(true);

    for (int i=0;i<values.length;i++){

        int left = PointPadding+STEP*(i+1)+I*PointPadding;

        int right= left+PointPadding;

        int top = height-PointPadding-values[I];

        int bottom = height-PointPadding;

        canvas.drawRect(left,top,right,bottom,p);

    }
    /**-------------------------**/

    p.setTextSize(30);

    p.setColor(Color.WHITE);
    /**開(kāi)始繪制文字-------------------**/
    for (int i=0;i<texts.length;i++){

        int left = PointPadding+STEP*(i+1)+I*PointPadding;

        int bottom = height-PointPadding;

        canvas.drawText(texts[i],left+PointPadding/2-p.measureText(texts[i])/2,bottom+STEP,p);
    }
    /**-------------------------**/




}

}
image.png

還有很多需要優(yōu)化的地方,是一個(gè)簡(jiǎn)單的直方圖的例子,最后感謝大神的無(wú)私奉獻(xiàn),下方我將貼上原文地址,給更多需要學(xué)習(xí)的人。

hencoder大神帶路不迷路傳送門(mén)

View坐標(biāo)系傳送門(mén)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 【Android 自定義View之繪圖】 基礎(chǔ)圖形的繪制 一、Paint與Canvas 繪圖需要兩個(gè)工具,筆和紙。...
    Rtia閱讀 12,163評(píng)論 5 34
  • 系列文章之 Android中自定義View(一)系列文章之 Android中自定義View(二)系列文章之 And...
    YoungerDev閱讀 4,628評(píng)論 3 11
  • 為什么選擇自定義View來(lái)做起頭?可能是因?yàn)槲易钣谐删透械氖虑槌舜笠粍偨佑|編程時(shí)就進(jìn)入了ACM之外,就是...
    孤獨(dú)的傷逝閱讀 879評(píng)論 1 5
  • 前言 自定義View是Android開(kāi)發(fā)者必須了解的基礎(chǔ);而Canvas類的使用在自定義View繪制中發(fā)揮著非常重...
    Carson帶你學(xué)安卓閱讀 29,390評(píng)論 7 220
  • 1、晚上出去夜宵,跟往常不一樣,腸粉加了一個(gè)雞蛋,做生意的小哥哥打開(kāi)雞蛋,我就發(fā)現(xiàn)是一個(gè)雙黃蛋,我倆傻樂(lè),幸運(yùn)的象...
    小花兒飛閱讀 289評(píng)論 1 0

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