From b38e8ffbdb726899998e94cbfea377871ab28d62 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 25 Mar 2018 20:25:48 +0200 Subject: [PATCH] draw colored background behind event titles --- .../calendar/views/MonthView.kt | 50 ++++++++++++++----- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt index 82803be1c..cef900d44 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/views/MonthView.kt @@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.views import android.content.Context import android.graphics.Canvas import android.graphics.Paint +import android.graphics.RectF import android.text.TextPaint import android.text.TextUtils import android.util.AttributeSet @@ -20,6 +21,9 @@ import org.joda.time.DateTime // used in the Monthly view fragment, 1 view per screen class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) { + private val BG_CORNER_RADIUS = 4f + private val ROW_COUNT = 6 + private var paint: Paint private var eventTitlePaint: TextPaint private var dayWidth = 0f @@ -30,7 +34,8 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con private var weekDaysLetterHeight = 0 private var eventTitleHeight = 0 private var currDayOfWeek = 0 - private var eventTitlePadding = 0 + private var smallPadding = 0 + private var bgRectF = RectF() private var dayLetters = ArrayList() private var days = ArrayList() private var dayVerticalOffsets = SparseIntArray() @@ -42,9 +47,9 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con textColor = context.config.textColor weakTextColor = textColor.adjustAlpha(LOW_ALPHA) - eventTitlePadding = resources.getDimensionPixelSize(R.dimen.tiny_margin) + smallPadding = resources.displayMetrics.density.toInt() val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size) - weekDaysLetterHeight = 2 * normalTextSize + weekDaysLetterHeight = normalTextSize * 2 paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = textColor @@ -53,7 +58,7 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con } val smallerTextSize = resources.getDimensionPixelSize(R.dimen.smaller_text_size) - eventTitleHeight = smallerTextSize + 2 * eventTitlePadding + eventTitleHeight = smallerTextSize eventTitlePaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { color = textColor textSize = smallerTextSize.toFloat() @@ -73,14 +78,16 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con override fun onDraw(canvas: Canvas) { super.onDraw(canvas) + dayVerticalOffsets.clear() if (dayWidth == 0f) { dayWidth = canvas.width / 7f } if (dayHeight == 0f) { - dayHeight = (canvas.height - weekDaysLetterHeight) / 6f + dayHeight = (canvas.height - weekDaysLetterHeight) / ROW_COUNT.toFloat() } + // week day letters for (i in 0..6) { val xPos = (i + 1) * dayWidth - dayWidth / 2 var weekDayLetterPaint = paint @@ -91,22 +98,33 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con } var curId = 0 - for (y in 0..5) { + for (y in 0 until ROW_COUNT) { for (x in 0..6) { val day = days.getOrNull(curId) if (day != null) { + dayVerticalOffsets.put(day.indexOnMonthView, dayVerticalOffsets[day.indexOnMonthView] + weekDaysLetterHeight) val xPos = x * dayWidth - val yPos = y * dayHeight + weekDaysLetterHeight + val yPos = y * dayHeight + dayVerticalOffsets[day.indexOnMonthView] val xPosCenter = xPos + dayWidth / 2 if (day.isToday) { canvas.drawCircle(xPosCenter, yPos + paint.textSize * 0.7f, paint.textSize * 0.75f, getCirclePaint(day)) } canvas.drawText(day.value.toString(), xPosCenter, yPos + paint.textSize, getTextPaint(day)) - dayVerticalOffsets.put(day.indexOnMonthView, weekDaysLetterHeight) day.dayEvents.forEach { - drawEventTitle(it.title, canvas, xPos, yPos + dayVerticalOffsets[day.indexOnMonthView]) - dayVerticalOffsets.put(day.indexOnMonthView, dayVerticalOffsets[day.indexOnMonthView] + eventTitleHeight) + val verticalOffset = dayVerticalOffsets[day.indexOnMonthView] + + // background rectangle + val backgroundY = yPos + verticalOffset + val bgLeft = xPos + smallPadding + val bgTop = backgroundY + smallPadding - eventTitleHeight + val bgRight = xPos + dayWidth - smallPadding + val bgBottom = backgroundY + smallPadding * 2 + bgRectF.set(bgLeft, bgTop, bgRight, bgBottom) + canvas.drawRoundRect(bgRectF, BG_CORNER_RADIUS, BG_CORNER_RADIUS, getColoredPaint(it.color)) + + drawEventTitle(it.title, canvas, xPos, yPos + verticalOffset, it.color) + dayVerticalOffsets.put(day.indexOnMonthView, verticalOffset + eventTitleHeight + smallPadding * 2) } } curId++ @@ -114,9 +132,9 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con } } - private fun drawEventTitle(title: String, canvas: Canvas, x: Float, y: Float) { - val ellipsized = TextUtils.ellipsize(title, eventTitlePaint, dayWidth - 2 * eventTitlePadding, TextUtils.TruncateAt.END) - canvas.drawText(title, 0, ellipsized.length, x + eventTitlePadding, y, eventTitlePaint) + private fun drawEventTitle(title: String, canvas: Canvas, x: Float, y: Float, eventColor: Int) { + val ellipsized = TextUtils.ellipsize(title, eventTitlePaint, dayWidth - smallPadding * 4, TextUtils.TruncateAt.END) + canvas.drawText(title, 0, ellipsized.length, x + smallPadding * 2, y, getEventTitlePaint(eventColor)) } private fun getTextPaint(day: DayMonthly): Paint { @@ -138,6 +156,12 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con return curPaint } + private fun getEventTitlePaint(color: Int): Paint { + val curPaint = Paint(eventTitlePaint) + curPaint.color = color.getContrastColor() + return curPaint + } + private fun getCirclePaint(day: DayMonthly): Paint { val curPaint = Paint(paint) var paintColor = primaryColor