那么多轮子都没有满足需求的,只能自己造。
自定义Android日历,只有一个CalendarView,非常小巧。核心代码400行左右,主要就是画。
CSDN: https://blog.csdn.net/lylddingHFFW/article/details/88227283
GitHub: https://github.com/lyldding/CalendarView
功能:支持左右滑动切换月份,上下滑动切换年份,设置背景,边框,周末或周一为第一天,显示其他月份日期等功能。 具体参考Api或者自定义。
/**
* 显示选中日期
*/
private boolean isShowSelectDate = true;
private int selectBackgroundColor = 0xff1157cb;
private int selectTextColor = Color.RED;
/**
* 显示当前日期
*/
private boolean isShowCurrentDate = true;
private int currentBackgroundColor = 0xff1157cb;
private int currentTextColor = Color.RED;
/**
* 显示边框
*/
private boolean isShowBorder = false;
private int borderColor = Color.GRAY;
/**
* 切换上下月按钮时滑动
*/
private boolean isBtnSwitchMonthScroll = true;
/**
* 显示其他月份日期
*/
private boolean isContainOtherMonthDate = true;
private int otherMonthTextColor = Color.LTGRAY;
/**
* 周日为第一天,false 周一为第一天
*/
private boolean isSundayAtFirst = true;
/**
* 当前月日期颜色
*/
private int currentMonthDayTextColor = Color.BLACK;
/**
* 年月标题的字体size dp
*/
private int titleTextSize = 15;
/**
* 整体星期标题的背景色
*/
private int weekBackgroundColor = 0xff88baff;
/**
* dp
*/
private int weekTextSize = 9;
/**
* 整体日历的背景色
*/
private int background = Color.WHITE;
/**
* 整体日历的背景圆角dp
*/
private int backgroundRadius = 3;
/**
* dp
*/
private int dayTextSize = 12;
大致思路:(主要分为5个区域)
1,初始化时计算每个区域每个Item的具体位置,并缓存。
2,红色年月为显示区域,在更新时同步更新其他四个区域的日期,并缓存。
3,处理移动和绘制。
代码简述:(具体看源码吧,核心代码400行左右)
1,计算各个Item的具体位置,并更新月份信息
private void computeData() {
mViewRectF.set(0 + mStrokeWidth / 2f, 0 + mStrokeWidth / 2f, mViewWidth - mStrokeWidth / 2f, mViewHeight - mStrokeWidth / 2f);
mViewWidth = (int) mViewRectF.width();
mItemWidth = mViewWidth / CalendarUtils.WEEK_COLUMN;
computeTitleData();
computeWeekData();
computeDayData();
updateMonthData(mCurrentYear, mCurrentMonth, Type.NONE);
}
2,绘制各个部分:背景,年月和切换按钮,星期,日期。
protected void onDraw(Canvas canvas) {
mIsDrawing = true;
drawBackground(canvas);
drawDataStr(canvas);
drawSwitchButton(canvas);
drawWeek(canvas);
drawAllDay(canvas);
drawOuterLine(canvas);
mIsDrawing = false;
}