1
1
package com.alamkanak.weekview
2
2
3
3
import android.graphics.Canvas
4
+ import android.graphics.Paint
5
+ import android.graphics.Rect
6
+ import android.graphics.RectF
7
+ import kotlin.math.roundToInt
4
8
5
9
internal class HeaderRowDrawer <T : Any >(
6
10
private val view : WeekView <T >,
@@ -14,9 +18,50 @@ internal class HeaderRowDrawer<T : Any>(
14
18
val width = view.width.toFloat()
15
19
canvas.drawRect(0f , 0f , width, config.headerHeight, config.headerBackgroundPaint)
16
20
21
+ if (config.showWeekNumber) {
22
+ canvas.drawWeekNumber(drawingContext)
23
+ }
24
+
17
25
if (config.showHeaderRowBottomLine) {
18
- val top = config.headerHeight - config.headerRowBottomLineWidth
19
- canvas.drawLine(0f , top , width, top , config.headerRowBottomLinePaint)
26
+ val y = config.headerHeight - config.headerRowBottomLineWidth
27
+ canvas.drawLine(0f , y , width, y , config.headerRowBottomLinePaint)
20
28
}
21
29
}
30
+
31
+ private fun Canvas.drawWeekNumber (drawingContext : DrawingContext ) {
32
+ val weekNumber = drawingContext.dateRange.first().weekOfYear.toString()
33
+
34
+ val bounds = config.weekNumberBounds
35
+ val textPaint = config.weekNumberTextPaint
36
+
37
+ val textHeight = textPaint.textHeight
38
+ val textOffset = (textHeight / 2f ).roundToInt() - textPaint.descent().roundToInt()
39
+
40
+ val width = textPaint.getTextBounds(" 52" ).width() * 2.5f
41
+ val height = textHeight * 1.5f
42
+
43
+ val backgroundRect = RectF (
44
+ bounds.centerX() - width / 2f ,
45
+ bounds.centerY() - height / 2f ,
46
+ bounds.centerX() + width / 2f ,
47
+ bounds.centerY() + height / 2f
48
+ )
49
+
50
+ drawRect(bounds, config.headerBackgroundPaint)
51
+
52
+ val backgroundPaint = config.weekNumberBackgroundPaint
53
+ val radius = config.weekNumberBackgroundCornerRadius.toFloat()
54
+ drawRoundRect(backgroundRect, radius, radius, backgroundPaint)
55
+
56
+ drawText(weekNumber, bounds.centerX(), bounds.centerY() + textOffset, textPaint)
57
+ }
58
+ }
59
+
60
+ internal val Paint .textHeight: Int
61
+ get() = (descent() - ascent()).roundToInt()
62
+
63
+ internal fun Paint.getTextBounds (text : String ): Rect {
64
+ val rect = Rect ()
65
+ getTextBounds(text, 0 , text.length, rect)
66
+ return rect
22
67
}
0 commit comments