# 倒霉的菜鸟

 博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅  :: 管理

在上一篇随笔中， 我们用到了画弧的方法，

 1     /**
2      * <p>
3      * Draw the specified arc, which will be scaled to fit inside the specified oval.
4      * </p>
5      * <p>
6      * If the start angle is negative or >= 360, the start angle is treated as start angle modulo
7      * 360.
8      * </p>
9      * <p>
10      * If the sweep angle is >= 360, then the oval is drawn completely. Note that this differs
11      * slightly from SkPath::arcTo, which treats the sweep angle modulo 360. If the sweep angle is
12      * negative, the sweep angle is treated as sweep angle modulo 360
13      * </p>
14      * <p>
15      * The arc is drawn clockwise. An angle of 0 degrees correspond to the geometric angle of 0
16      * degrees (3 o'clock on a watch.)
17      * </p>
18      *
19      * @param startAngle Starting angle (in degrees) where the arc begins
20      * @param sweepAngle Sweep angle (in degrees) measured clockwise
21      * @param useCenter If true, include the center of the oval in the arc, and close it if it is
22      *            being stroked. This will draw a wedge
23      * @param paint The paint used to draw the arc
24      */
25     public void drawArc(float left, float top, float right, float bottom, float startAngle,
26             float sweepAngle, boolean useCenter, @NonNull Paint paint) {
27         super.drawArc(left, top, right, bottom, startAngle, sweepAngle, useCenter, paint);
28     }

 1 private val RADIUS = 150f.toPx
2 private val SWEEP_ANGLES = arrayListOf(50f, 90f,30f, 120f,70f)
3 private val COLORS = arrayListOf("#FF6200EE", "#FF03DAC5","#FF000000", "#FFBB86FC","#FF0080")
4 private val OFF_LENGTH= 20f.toPx
5 class PieView(context: Context, attributeSet: AttributeSet): View(context, attributeSet) {
6
7     val paint = Paint(Paint.ANTI_ALIAS_FLAG)
8     override fun onDraw(canvas: Canvas) {
9         var startAngle = 0f
10         SWEEP_ANGLES.forEachIndexed { index, fl ->
11             paint.color = Color.parseColor(COLORS[index])
12             //偏移的两种方式
13 //            if(index == 0){
14 //                //先保存之前的canvas
15 //                canvas.save()
16 //                //canvas偏移
19 //                )
20 //            }
22 //                startAngle, fl, true, paint
23 //            )
24 //            if (index == 0){
25 //                //偏移完之后再还原回来
26 //                canvas.restore()
27 //            }
28             if(index == 0){
33                     startAngle, fl, true, paint
34                 )
35             }else {
37                     startAngle, fl, true, paint
38                 )
39             }
40             startAngle += fl
41         }
42     }
43 }

posted on 2021-10-14 18:35  倒霉的菜鸟  阅读(59)  评论(0编辑  收藏  举报