1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cn.flightfeather.thirdapp.view.calendarview;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
 
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author riku
 * 2019.3.29
 * 精美进度风格
 */
 
public class ProgressMonthView extends MonthView {
 
    private Paint mProgressPaint = new Paint();
    private Paint mTagPaint = new Paint();
    private Paint mNoneProgressPaint = new Paint();
    private List<Integer> colorList = new ArrayList<>();
    private int mRadius;
    private int tagRadius;
 
    public ProgressMonthView(Context context) {
        super(context);
        mProgressPaint.setAntiAlias(true);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(dipToPx(context, 2.2f));
        mProgressPaint.setColor(0xBBf54a00);
 
        mTagPaint.setAntiAlias(true);
        mTagPaint.setStyle(Paint.Style.FILL);
//        mTagPaint.setStrokeWidth(dipToPx(context, 2.2f));
        mTagPaint.setColor(Color.RED);
 
        colorList.add(0xBBf54a00);
        colorList.add(0xF700F5BC);
 
        mNoneProgressPaint.setAntiAlias(true);
        mNoneProgressPaint.setStyle(Paint.Style.STROKE);
        mNoneProgressPaint.setStrokeWidth(dipToPx(context, 2.2f));
        mNoneProgressPaint.setColor(0x90CfCfCf);
 
        tagRadius = dipToPx(context, 1f);
    }
 
 
 
    @Override
    protected void onPreviewHook() {
        mRadius = Math.min(mItemWidth, mItemHeight) / 11 * 4;
 
    }
 
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        int tempRadius = (int) (mRadius - mProgressPaint.getStrokeWidth() - dipToPx(getContext(), 2f));
        canvas.drawCircle(cx, cy, tempRadius, mSelectedPaint);
        return true;
    }
 
    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
 
        int index = 0;
        int progress = 0;
        RectF progressRectF = new RectF(cx - mRadius, cy - mRadius, cx + mRadius, cy + mRadius);
        for (Calendar.Scheme s : calendar.getSchemes()) {
            //type == 1,画进度条
            if (s.getType() == 1) {
                progress = Integer.parseInt(s.getScheme());
                mProgressPaint.setColor(colorList.get(index % 2));
                int angle = getAngle(progress);
                canvas.drawArc(progressRectF, -90, angle, false, mProgressPaint);
                RectF noneRectF = new RectF(cx - mRadius, cy - mRadius, cx + mRadius, cy + mRadius);
                canvas.drawArc(noneRectF, angle - 90, 360 - angle, false, mNoneProgressPaint);
            }
            //type == 2,画标记
            else if (s.getType() == 2) {
                Paint.FontMetrics fontMetrics = mSelectTextPaint.getFontMetrics();
                float textBottomY = mTextBaseLine + y + fontMetrics.bottom;
                float cardBottomY = y + mItemHeight;
//                float tagCy = (textBottomY + cardBottomY) / 2;
                float tagCy = (textBottomY + 4);
                canvas.drawCircle((float) cx, tagCy, tagRadius, mTagPaint);
            }
            index++;
        }
 
    }
 
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float baselineY = mTextBaseLine + y;
        int cx = x + mItemWidth / 2;
 
        if (isSelected) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    mSelectTextPaint);
        } else if (hasScheme) {
            canvas.drawText(String.valueOf(calendar.getDay()),
                    cx,
                    baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
 
        } else {
            canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }
 
    /**
     * 获取角度
     *
     * @param progress 进度
     * @return 获取角度
     */
    private static int getAngle(int progress) {
        return (int) (progress * 3.6);
    }
 
 
    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}