@@ -15,8 +15,14 @@ import kotlin.math.min |
15 | 15 | * date : 2019/08/12 |
16 | 16 | * desc : 学习计划view |
17 | 17 | * </pre> |
| 18 | + * |
| 19 | + * 这个是 类似rv的那个view,时间轴 |
18 | 20 | */ |
19 | | -class StudyPlanProgressView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { |
| 21 | +class StudyPlanProgressView @JvmOverloads constructor( |
| 22 | + context: Context, |
| 23 | + attrs: AttributeSet? = null, |
| 24 | + defStyleAttr: Int = 0 |
| 25 | +) : View(context, attrs, defStyleAttr) { |
20 | 26 | private var mWidth = 0 |
21 | 27 | private var mHeight = 0 |
22 | 28 |
|
@@ -51,14 +57,34 @@ class StudyPlanProgressView @JvmOverloads constructor(context: Context, attrs: A |
51 | 57 |
|
52 | 58 | private fun initAttr(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { |
53 | 59 | val typedArray = context.obtainStyledAttributes(attrs, R.styleable.StudyPlanProgressView) |
54 | | - iconUncheckedBitmapRes = (typedArray.getDrawable(R.styleable.StudyPlanProgressView_sppv_iconUnchecked) as? BitmapDrawable)?.bitmap |
55 | | - iconCheckedBitmapRes = (typedArray.getDrawable(R.styleable.StudyPlanProgressView_sppv_iconChecked) as? BitmapDrawable)?.bitmap |
56 | | - iconSize = typedArray.getDimensionPixelOffset(R.styleable.StudyPlanProgressView_sppv_iconSize, dp2px(14f).toInt()) |
57 | | - progressWidth = typedArray.getDimensionPixelOffset(R.styleable.StudyPlanProgressView_sppv_progressWidth, dp2px(2f).toInt()) |
58 | | - uncheckedProgressColor = typedArray.getColor(R.styleable.StudyPlanProgressView_sppv_uncheckedProgressColor, uncheckedProgressColor) |
59 | | - checkedProgressColor = typedArray.getColor(R.styleable.StudyPlanProgressView_sppv_checkedProgressColor, checkedProgressColor) |
60 | | - textSize = typedArray.getDimensionPixelSize(R.styleable.StudyPlanProgressView_sppv_textSize, sp2px(11)) |
61 | | - textColor = typedArray.getColor(R.styleable.StudyPlanProgressView_sppv_textColor, checkedProgressColor) |
| 60 | + iconUncheckedBitmapRes = |
| 61 | + (typedArray.getDrawable(R.styleable.StudyPlanProgressView_sppv_iconUnchecked) as? BitmapDrawable)?.bitmap |
| 62 | + iconCheckedBitmapRes = |
| 63 | + (typedArray.getDrawable(R.styleable.StudyPlanProgressView_sppv_iconChecked) as? BitmapDrawable)?.bitmap |
| 64 | + iconSize = typedArray.getDimensionPixelOffset( |
| 65 | + R.styleable.StudyPlanProgressView_sppv_iconSize, |
| 66 | + dp2px(14f).toInt() |
| 67 | + ) |
| 68 | + progressWidth = typedArray.getDimensionPixelOffset( |
| 69 | + R.styleable.StudyPlanProgressView_sppv_progressWidth, |
| 70 | + dp2px(2f).toInt() |
| 71 | + ) |
| 72 | + uncheckedProgressColor = typedArray.getColor( |
| 73 | + R.styleable.StudyPlanProgressView_sppv_uncheckedProgressColor, |
| 74 | + uncheckedProgressColor |
| 75 | + ) |
| 76 | + checkedProgressColor = typedArray.getColor( |
| 77 | + R.styleable.StudyPlanProgressView_sppv_checkedProgressColor, |
| 78 | + checkedProgressColor |
| 79 | + ) |
| 80 | + textSize = typedArray.getDimensionPixelSize( |
| 81 | + R.styleable.StudyPlanProgressView_sppv_textSize, |
| 82 | + sp2px(11) |
| 83 | + ) |
| 84 | + textColor = typedArray.getColor( |
| 85 | + R.styleable.StudyPlanProgressView_sppv_textColor, |
| 86 | + checkedProgressColor |
| 87 | + ) |
62 | 88 |
|
63 | 89 | typedArray.recycle() |
64 | 90 | } |
@@ -84,7 +110,8 @@ class StudyPlanProgressView @JvmOverloads constructor(context: Context, attrs: A |
84 | 110 | mWidth = w |
85 | 111 | mHeight = h |
86 | 112 |
|
87 | | - cellWidth = (mWidth - marginLeftAndRight * 2 - iconSize * ALL_POINT_SIZE) / (ALL_POINT_SIZE - 1) |
| 113 | + cellWidth = |
| 114 | + (mWidth - marginLeftAndRight * 2 - iconSize * ALL_POINT_SIZE) / (ALL_POINT_SIZE - 1) |
88 | 115 | } |
89 | 116 |
|
90 | 117 | override fun onDraw(canvas: Canvas) { |
@@ -96,13 +123,31 @@ class StudyPlanProgressView @JvmOverloads constructor(context: Context, attrs: A |
96 | 123 | private fun drawProgressLine(canvas: Canvas) { |
97 | 124 | val lineStart = (marginLeftAndRight + iconSize / 2).toFloat() |
98 | 125 | linePaint.color = uncheckedProgressColor |
99 | | - canvas.drawLine(lineStart, (mHeight / 2).toFloat(), (mWidth - marginLeftAndRight - iconSize / 2).toFloat(), (mHeight / 2).toFloat(), linePaint) |
| 126 | + //这个是花基本的颜色 |
| 127 | + canvas.drawLine( |
| 128 | + lineStart, |
| 129 | + (mHeight / 2).toFloat(), |
| 130 | + (mWidth - marginLeftAndRight - iconSize / 2).toFloat(), |
| 131 | + (mHeight / 2).toFloat(), |
| 132 | + linePaint |
| 133 | + ) |
100 | 134 | if (getDataSize() == 0) return |
101 | 135 | val lineEnd = lineStart + (getDataSize() - 1) * (cellWidth + iconSize) |
102 | 136 | linePaint.color = checkedProgressColor |
103 | | - canvas.drawLine(lineStart, (mHeight / 2).toFloat(), lineEnd, (mHeight / 2).toFloat(), linePaint) |
| 137 | + |
| 138 | + //这个是选中的颜色 |
| 139 | + canvas.drawLine( |
| 140 | + lineStart, |
| 141 | + (mHeight / 2).toFloat(), |
| 142 | + lineEnd, |
| 143 | + (mHeight / 2).toFloat(), |
| 144 | + linePaint |
| 145 | + ) |
104 | 146 | } |
105 | 147 |
|
| 148 | + /** |
| 149 | + * 这个直接是不透明的图片覆盖的 |
| 150 | + */ |
106 | 151 | private fun drawProgress(canvas: Canvas) { |
107 | 152 | if (iconUncheckedBitmapRes == null || iconCheckedBitmapRes == null) return |
108 | 153 | for (i in 0 until ALL_POINT_SIZE) { |
@@ -120,21 +165,34 @@ class StudyPlanProgressView @JvmOverloads constructor(context: Context, attrs: A |
120 | 165 | } else false |
121 | 166 | } |
122 | 167 |
|
| 168 | + /** |
| 169 | + * 按照这个图片为基准绘制了 文字 |
| 170 | + * |
| 171 | + * textPaint.textAlign = Paint.Align.CENTER 这个属性有捡到了 |
| 172 | + */ |
123 | 173 | private fun drawText(canvas: Canvas, position: Int) { |
124 | 174 | if (getDataSize() == 0) return |
125 | 175 | if (position < getDataSize()) { |
126 | 176 | val textWidth = textPaint.measureText(dates[position].content).toInt() |
127 | 177 | val isTop = position % 2 == 0 |
128 | | - val left = marginLeftAndRight + iconSize / 2 + (cellWidth + iconSize) * position - textWidth / 2 |
| 178 | + // fixme 这个公式不完全一样了 |
| 179 | + val left = |
| 180 | + marginLeftAndRight + iconSize / 2 + (cellWidth + iconSize) * position - textWidth / 2 |
129 | 181 | textRectF.left = left |
130 | 182 | textRectF.right = left + textWidth |
131 | 183 | textRectF.top = if (isTop) 0 else (mHeight + iconSize) / 2 |
132 | 184 | textRectF.bottom = if (isTop) (mHeight - iconSize) / 2 else mHeight |
133 | 185 |
|
134 | 186 | val fontMetrics = textPaint.fontMetricsInt |
135 | | - val baseline = (textRectF.bottom + textRectF.top - fontMetrics.bottom - fontMetrics.top) / 2 |
| 187 | + val baseline = |
| 188 | + (textRectF.bottom + textRectF.top - fontMetrics.bottom - fontMetrics.top) / 2 |
136 | 189 | //文字绘制到整个布局的中心位置 |
137 | | - canvas.drawText(dates[position].content, textRectF.centerX().toFloat(), baseline.toFloat(), textPaint) |
| 190 | + canvas.drawText( |
| 191 | + dates[position].content, |
| 192 | + textRectF.centerX().toFloat(), |
| 193 | + baseline.toFloat(), |
| 194 | + textPaint |
| 195 | + ) |
138 | 196 | } |
139 | 197 | } |
140 | 198 |
|
@@ -150,13 +208,17 @@ class StudyPlanProgressView @JvmOverloads constructor(context: Context, attrs: A |
150 | 208 | private fun getDataSize() = min(dates.size, 7) |
151 | 209 |
|
152 | 210 | fun dp2px(dpVal: Float): Float { |
153 | | - return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
154 | | - dpVal, resources.displayMetrics) |
| 211 | + return TypedValue.applyDimension( |
| 212 | + TypedValue.COMPLEX_UNIT_DIP, |
| 213 | + dpVal, resources.displayMetrics |
| 214 | + ) |
155 | 215 | } |
156 | 216 |
|
157 | 217 | fun sp2px(spVal: Int): Int { |
158 | | - return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, |
159 | | - spVal.toFloat(), resources.displayMetrics).toInt() |
| 218 | + return TypedValue.applyDimension( |
| 219 | + TypedValue.COMPLEX_UNIT_SP, |
| 220 | + spVal.toFloat(), resources.displayMetrics |
| 221 | + ).toInt() |
160 | 222 | } |
161 | 223 |
|
162 | 224 | class ProgressData(var content: String, var isFinished: Boolean) |
|