记⼀次Android第三⽅⽇历控件CalendarView的使⽤
⼀、⽂章背景
⽤过两个⽇历控件的库,有⽤viewpager实现的,也有⽤canvas实现的。在实际使⽤过程中,发现使⽤canvas实现的calendarView切换下⼀⽉和下⼀年这种操作时切换更流畅。
我这⾥主要记录上⼀年和下⼀年的使⽤,其他功能⼤家可以参考第三⽅库的使⽤
⼆、⽇历控件的使⽤
2.1、实现MonthView
package com.wyy.usecalendarview
t.Context
aphics.Canvas
aphics.Paint
aphics.RectF
ontent.ContextCompat
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.MonthView
class ScheduleMonthView(context: Context) : MonthView(context) {
/**
* 背景圆点
*/
private val mPointPaint = Paint()
/**
* 今天的背景⾊
*/
private val mCurrentDayPaint = Paint()
/**
* 圆点半径
*/
private var mPointRadius = 3f
init {
mCurrentDayPaint.isAntiAlias = true
mCurrentDayPaint.style = Paint.Style.FILL
mPointPaint.isAntiAlias = true
mPointPaint.style = Paint.Style.FILL
}
/**
* 绘制选中的⽇⼦(这个绘制会覆盖标记点的显⽰)
*
* @param canvas    canvas
* @param calendar  ⽇历⽇历calendar
* @param x        ⽇历Card x起点坐标
* @param y        ⽇历Card y起点坐标
* @param hasScheme hasScheme ⾮标记的⽇期
* @return 返回true 则绘制onDrawScheme,因为这⾥背景⾊不是是互斥的,所以返回true
*/
override fun onDrawSelected(canvas: Canvas, calendar: Calendar, x: Int, y: Int, hasScheme: Boolean): Boolean {
if (!calendar.isCurrentDay) {
mSelectedPaint.style = Paint.Style.STROKE
mSelectedPaint.strokeWidth = 2f
val rectF = Float(), y.toFloat(), (x + mItemWidth).toFloat(), (y + mItemHeight).toFloat())
canvas.drawRoundRect(rectF, 16f, 16f, mSelectedPaint)
}
return true
}
/**
* 绘制标记的事件⽇⼦(只绘制⾮当天的标记点,避免重复绘制)
*
* @param canvas  canvas
* @param calendar ⽇历calendar
* @param x        ⽇历Card x起点坐标
* @param y        ⽇历Card y起点坐标
*/
override fun onDrawScheme(canvas: Canvas, calendar: Calendar, x: Int, y: Int) {
if (!calendar.isCurrentDay) {
canvas.drawCircle((x + mItemWidth / 2).toFloat(), (y + mItemHeight - 13).toFloat(), mPointRadius, mPointPaint)
}
}
/**
* 绘制⽂本(如果今天有标记点,这⾥还需要绘制当天的标记点)
*
* @param canvas    canvas
* @param calendar  ⽇历calendar
* @param x          ⽇历Card x起点坐标
* @param y          ⽇历Card y起点坐标
* @param hasScheme  是否是标记的⽇期
* @param isSelected 是否选中
*/
override fun onDrawText(canvas: Canvas?, calendar: Calendar?, x: Int, y: Int, hasScheme: Boolean, isSelected: Boolean) {        val cx = x + mItemWidth / 2
calendar?.let {
canvas?.let { cit ->
if (it.isCurrentDay) {
val rectF = Float(), y.toFloat(), (x + mItemWidth).toFloat(), (y + mItemHeight).toFloat())
cit.drawRoundRect(rectF, 16f, 16f, mCurrentDayPaint)
if (hasScheme) {
//  绘制当天的标记点
cit.drawCircle(
(x + mItemWidth / 2).toFloat(),
(y + mItemHeight - 13).toFloat(),
mPointRadius,
mPointPaint
)
}
}
控件的使用
val drawPaint = if (isSelected && !it.isCurrentDay) {
mSelectTextPaint
} else if (hasScheme) {
if (it.isCurrentMonth) mSchemeTextPaint else mOtherMonthTextPaint
} else {
if (it.isCurrentDay) mCurDayTextPaint else if (it.isCurrentMonth) mCurMonthTextPaint else mOtherMonthTextPaint                }
cit.drawText(String(), cx.toFloat(), mTextBaseLine + y, drawPaint)
}
}
}
}
2.2、布局代码使⽤⽰例:
<com.haibin.calendarview.CalendarView
android:id="@+id/cv_calendar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:calendar_height="40dp"
app:current_day_text_color="@color/black"
app:current_month_text_color="@color/black"
app:day_text_size="14sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintRight_toRightOf="parent"
app:month_view="com.wyy.usecalendarview.ScheduleMonthView"
app:other_month_text_color="@color/teal_700"
app:selected_text_color="@color/black"
app:selected_theme_color="@color/purple_700"
app:week_background="@android:color/transparent"
app:week_bar_height="40dp"
app:week_text_color="@color/black"
app:week_text_size="14sp"
app:month_view_show_mode="mode_all"
app:select_mode="single_mode" />
2.3、注册⽇历的事件监听
2.3.1、监听⽉份切换
mDataBinding.cvCalendar.setOnMonthChangeListener(this)
2.3.2、监听选择事件
mDataBinding.cvCalendar.setOnCalendarSelectListener(this)
2.4、实现⽇历的监听事件
2.4.1、⽉份切换的实现
override fun onMonthChange(year: Int, month: Int) {
supportActionBar?.title = getString(R.string.title, year, month)
}
2.4.2、选择事件的实现
/**
* 没⽤到
*/
override fun onCalendarOutOfRange(calendar: Calendar?) {}
/**
* ⽤户选择的监听
*/
override fun onCalendarSelect(calendar: Calendar?, isClick: Boolean) {
Log.d(
"wyy",
"Selector year is ${calendar?.year},month is ${calendar?.month},day is ${calendar?.day} and isClick is $isClick"        )
}
2.5、操作⽇历控件
2.5.1、上⼀年
hViewPager.apply {                currentItem -= 12
}
2.5.2、下⼀年
hViewPager.apply {                currentItem += 12
}
2.6、demo

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。