在學習android中圖形圖像處理技術這部分內容時,對繪制圓弧函數canvas.drawArc()的用法、參數含義及畫圖原理很是不理解,在網上搜索了一些,加上自己的理解,在此做個小總結,作為學習過程中的一個小腳印:
一. 代碼及相應的運行結果
1.填充圓弧但不含圓心(注意:代碼變動部分加粗紅色給出,下同)
(1)代碼
1 /***********配置畫筆*************/ 2 Paint paint=new Paint(); //采用默認設置創建一個畫筆 3 paint.setAntiAlias(true);//使用抗鋸齒功能 4 paint.setColor(0xFFA4C739); //設置畫筆的顏色為綠色 5 paint.setStyle(Paint.Style.FILL);//設置畫筆類型為填充 6 /***********繪制圓弧*************/ 7 RectF rectf_head=new RectF(10, 10, 100, 100);//確定外切矩形范圍 8 rectf_head.offset(100, 20);//使rectf_head所確定的矩形向右偏移100像素,向下偏移20像素 9 canvas.drawArc(rectf_head, -10, -160, false, paint);//繪制圓弧,不含圓心
(2) 運行結果

2.填充圓弧並且含圓心
(1)代碼
1 /***********配置畫筆*************/ 2 Paint paint=new Paint(); //采用默認設置創建一個畫筆 3 paint.setAntiAlias(true);//使用抗鋸齒功能 4 paint.setColor(0xFFA4C739); //設置畫筆的顏色為綠色 5 paint.setStyle(Paint.Style.FILL);//設置畫筆類型為填充 6 /***********繪制圓弧*************/ 7 RectF rectf_head=new RectF(10, 10, 100, 100);//確定外切矩形范圍 8 rectf_head.offset(100, 20);//使rectf_head所確定的矩形向右偏移100像素,向下偏移20像素 9 canvas.drawArc(rectf_head, -10, -160, true, paint);//繪制圓弧,含圓心
(2)運行效果

3.不填充圓弧含圓心
(1)代碼
1 /***********配置畫筆*************/ 2 Paint paint=new Paint(); //采用默認設置創建一個畫筆 3 paint.setAntiAlias(true);//使用抗鋸齒功能 4 paint.setColor(0xFFA4C739); //設置畫筆的顏色為綠色 5 paint.setStyle(Paint.Style.STROKE);//設置畫筆類型為STROKE(個人感覺是描邊的意思) 6 /***********繪制圓弧*************/ 7 RectF rectf_head=new RectF(10, 10, 100, 100);//確定外切矩形范圍 8 rectf_head.offset(100, 20);//使rectf_head所確定的矩形向右偏移100像素,向下偏移20像素 9 canvas.drawArc(rectf_head, -10, -160, true, paint);//繪制圓弧,含圓心
(2)運行效果

4.不填充圓弧不含圓心
(1)代碼
1 /***********配置畫筆*************/ 2 Paint paint=new Paint(); //采用默認設置創建一個畫筆 3 paint.setAntiAlias(true);//使用抗鋸齒功能 4 paint.setColor(0xFFA4C739); //設置畫筆的顏色為綠色 5 paint.setStyle(Paint.Style.STROKE);//設置畫筆類型為STROKE類型(個人感覺是描邊的意思) 6 /***********繪制圓弧*************/ 7 RectF rectf_head=new RectF(10, 10, 100, 100);//確定外切矩形范圍 8 rectf_head.offset(100, 20);//使rectf_head所確定的矩形向右偏移100像素,向下偏移20像素 9 canvas.drawArc(rectf_head, -10, -160, false, paint);//繪制圓弧,不含圓心
(2)運行效果

5. 總結
由上面的例子可以看出,是否包含圓心是有函數drawArc()第四個參數決定的(true:包含;false:不包含)。圓弧是否為填充由畫筆的類型決定的
paint.setStyle()的參數決定的(Paint.Style.STROKE:描邊(個人的翻譯,未必准確);Paint.Style.FILL:填充;Paint.Style.FILL_AND_STROKE:既填充又描邊)。
二.繪制圓弧函數詳細分析--drawArc()
1.官方API:
public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)
- Draw the specified arc, which will be scaled to fit inside the specified oval. If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs slightly from SkPath::arcTo, which treats the sweep angle mod 360.
-
- 參數:(英文)
-
oval- The bounds of oval used to define the shape and size of the arc -
startAngle- Starting angle (in degrees) where the arc begins -
sweepAngle- Sweep angle (in degrees) measured clockwise -
useCenter- If true, include the center of the oval in the arc, and close it if it is being stroked. This will draw a wedge -
paint- The paint used to draw the arc
- 參數:(中文)
-
oval- 用於確定圓弧形狀與尺寸的橢圓邊界(即橢圓外切矩形) -
startAngle- 開始角度( 以時鍾3點的方向為0°,逆時針為正方向) -
sweepAngle- 掃過角度( 以時鍾3點的方向為0°,逆時針為正方向) -
useCenter- 是否包含圓心 -
paint- 繪制圓弧的畫筆 - 2.繪制圓弧的原理
-
- 由RectF(float left, float top, float right, float bottom)得到一個矩形,此虛擬矩形內切繪制一個橢圓(如果長和寬相等,則為圓)。

- 以矩形的中心為圓心,以時鍾3點的方向為0°,逆時針為正方向,從0°正方向旋轉startAngle 度,和橢圓相交得到一條直線和一個交點。
- 從這條直線開始,正方向旋轉sweepAngle度,得到另一條直線和交點,這樣就得到了一個兩交點之間的 圓弧。
