draw colored background behind event titles

This commit is contained in:
tibbi 2018-03-25 20:25:48 +02:00
parent edd3bd0737
commit b38e8ffbdb

View file

@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.views
import android.content.Context import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Paint import android.graphics.Paint
import android.graphics.RectF
import android.text.TextPaint import android.text.TextPaint
import android.text.TextUtils import android.text.TextUtils
import android.util.AttributeSet import android.util.AttributeSet
@ -20,6 +21,9 @@ import org.joda.time.DateTime
// used in the Monthly view fragment, 1 view per screen // used in the Monthly view fragment, 1 view per screen
class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) { 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 paint: Paint
private var eventTitlePaint: TextPaint private var eventTitlePaint: TextPaint
private var dayWidth = 0f private var dayWidth = 0f
@ -30,7 +34,8 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
private var weekDaysLetterHeight = 0 private var weekDaysLetterHeight = 0
private var eventTitleHeight = 0 private var eventTitleHeight = 0
private var currDayOfWeek = 0 private var currDayOfWeek = 0
private var eventTitlePadding = 0 private var smallPadding = 0
private var bgRectF = RectF()
private var dayLetters = ArrayList<String>() private var dayLetters = ArrayList<String>()
private var days = ArrayList<DayMonthly>() private var days = ArrayList<DayMonthly>()
private var dayVerticalOffsets = SparseIntArray() private var dayVerticalOffsets = SparseIntArray()
@ -42,9 +47,9 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
textColor = context.config.textColor textColor = context.config.textColor
weakTextColor = textColor.adjustAlpha(LOW_ALPHA) 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) val normalTextSize = resources.getDimensionPixelSize(R.dimen.normal_text_size)
weekDaysLetterHeight = 2 * normalTextSize weekDaysLetterHeight = normalTextSize * 2
paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = textColor 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) val smallerTextSize = resources.getDimensionPixelSize(R.dimen.smaller_text_size)
eventTitleHeight = smallerTextSize + 2 * eventTitlePadding eventTitleHeight = smallerTextSize
eventTitlePaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply { eventTitlePaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
color = textColor color = textColor
textSize = smallerTextSize.toFloat() textSize = smallerTextSize.toFloat()
@ -73,14 +78,16 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
dayVerticalOffsets.clear()
if (dayWidth == 0f) { if (dayWidth == 0f) {
dayWidth = canvas.width / 7f dayWidth = canvas.width / 7f
} }
if (dayHeight == 0f) { if (dayHeight == 0f) {
dayHeight = (canvas.height - weekDaysLetterHeight) / 6f dayHeight = (canvas.height - weekDaysLetterHeight) / ROW_COUNT.toFloat()
} }
// week day letters
for (i in 0..6) { for (i in 0..6) {
val xPos = (i + 1) * dayWidth - dayWidth / 2 val xPos = (i + 1) * dayWidth - dayWidth / 2
var weekDayLetterPaint = paint var weekDayLetterPaint = paint
@ -91,22 +98,33 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
} }
var curId = 0 var curId = 0
for (y in 0..5) { for (y in 0 until ROW_COUNT) {
for (x in 0..6) { for (x in 0..6) {
val day = days.getOrNull(curId) val day = days.getOrNull(curId)
if (day != null) { if (day != null) {
dayVerticalOffsets.put(day.indexOnMonthView, dayVerticalOffsets[day.indexOnMonthView] + weekDaysLetterHeight)
val xPos = x * dayWidth val xPos = x * dayWidth
val yPos = y * dayHeight + weekDaysLetterHeight val yPos = y * dayHeight + dayVerticalOffsets[day.indexOnMonthView]
val xPosCenter = xPos + dayWidth / 2 val xPosCenter = xPos + dayWidth / 2
if (day.isToday) { if (day.isToday) {
canvas.drawCircle(xPosCenter, yPos + paint.textSize * 0.7f, paint.textSize * 0.75f, getCirclePaint(day)) 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)) canvas.drawText(day.value.toString(), xPosCenter, yPos + paint.textSize, getTextPaint(day))
dayVerticalOffsets.put(day.indexOnMonthView, weekDaysLetterHeight)
day.dayEvents.forEach { day.dayEvents.forEach {
drawEventTitle(it.title, canvas, xPos, yPos + dayVerticalOffsets[day.indexOnMonthView]) val verticalOffset = dayVerticalOffsets[day.indexOnMonthView]
dayVerticalOffsets.put(day.indexOnMonthView, dayVerticalOffsets[day.indexOnMonthView] + eventTitleHeight)
// 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++ 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) { private fun drawEventTitle(title: String, canvas: Canvas, x: Float, y: Float, eventColor: Int) {
val ellipsized = TextUtils.ellipsize(title, eventTitlePaint, dayWidth - 2 * eventTitlePadding, TextUtils.TruncateAt.END) val ellipsized = TextUtils.ellipsize(title, eventTitlePaint, dayWidth - smallPadding * 4, TextUtils.TruncateAt.END)
canvas.drawText(title, 0, ellipsized.length, x + eventTitlePadding, y, eventTitlePaint) canvas.drawText(title, 0, ellipsized.length, x + smallPadding * 2, y, getEventTitlePaint(eventColor))
} }
private fun getTextPaint(day: DayMonthly): Paint { private fun getTextPaint(day: DayMonthly): Paint {
@ -138,6 +156,12 @@ class MonthView(context: Context, attrs: AttributeSet, defStyle: Int) : View(con
return curPaint return curPaint
} }
private fun getEventTitlePaint(color: Int): Paint {
val curPaint = Paint(eventTitlePaint)
curPaint.color = color.getContrastColor()
return curPaint
}
private fun getCirclePaint(day: DayMonthly): Paint { private fun getCirclePaint(day: DayMonthly): Paint {
val curPaint = Paint(paint) val curPaint = Paint(paint)
var paintColor = primaryColor var paintColor = primaryColor