自定義View入門-繪製基礎(1)

前言

說道自定義View,咱們必定會想到,自定義View的繪製流程html

  • 測量階段(measure)
  • 佈局階段(layout)
  • 繪製階段(draw)

咱們看到的一些炫酷的view效果,都是在繪製方法裏去實現的, 也就是draw(Canvas), 咱們先放下 測量與佈局, 先從繪製基礎開始學起。android

詳解

說到ondraw(Canvas)方法,不得不提PaintCanvas。咱們先來看Paintcanvas

1.Paint

Paint就是"畫筆",咱們先去看下Paint類的源碼解釋:微信

**
 * The Paint class holds the style and color information about how to draw
 * geometries, text and bitmaps.
 */

Paint類能夠畫幾何圖形,文本與bitmap。 Paint類方法比較多, 這裏拿Paint.Style舉例:佈局

  • Paint.Style.FILL:填充內部
  • Paint.Style.FILL_AND_STROKE :填充內部和描邊
  • Paint.Style.STROKE :描邊
2.Canvas

(1).定義 Canvas就是「畫布」,咱們先去看下Canvas類的源碼解釋:google

* The Canvas class holds the "draw" calls. To draw something, you need
 * 4 basic components: A Bitmap to hold the pixels, a Canvas to host
 * the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
 * Path, text, Bitmap), and a paint (to describe the colors and styles for the
 * drawing).
  • 承載像素的位圖
  • 持有繪畫方法調用的畫布
  • 描述畫圖顏色和風格的畫筆
  • 畫圖的類型。

(2).繪製方法3d

方法比較多了,這裏我就隨便舉幾個例子:code

  • 畫線
Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(20);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawLine(200,200,450,200,paint);

  • 畫矩形
Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(50);
        paint.setStyle(Paint.Style.FILL  );
        canvas.drawRect(100,100,200,200,paint);

  • 畫扇形-140度
Paint paint=new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(50);
        paint.setStyle(Paint.Style.FILL  );
        canvas.drawArc(100,100,400,400,0,140,false,paint);

更多的方法以及方法的含義能夠去下面的API地址去看! API地址component

今天就講到這裏 ,繪製基礎還有一個很是重要的類,Paht(路徑)類,下一節講一下。 但願對你們有所幫助!orm

你們能夠關注個人微信公衆號:「秦子帥」一個有質量、有態度的公衆號!

公衆號

相關文章
相關標籤/搜索