赞
踩
Android开发,经常会使用到日历开发,实现方式有自定义或者第三方框架基础上修改。这里推荐使用第三方框架基础上修改。com.haibin.calendarview.CalendarView,为此本demo亦是在此框架基础上自定义样式,实现理想的日历。实现的功能有:
1、自定义星期标题
2、标记对应日程提醒,如图日期提醒标记---铃铛
3、节假日与节气文字自定义
4、自定义日历两边文字显色,例如星期日与星期六那两列的公历日期文字为淡红色
5、自定义日历网格线
效果如下图
- //日历 android 引入
- implementation 'com.haibin:calendarview:3.6.8'
-
- //日历 androidx 引入
- implementation 'com.haibin:calendarview:3.7.1'
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="CalendarView">
-
- <attr name="calendar_padding" format="dimension" /><!--日历内部左右padding-->
- <attr name="calendar_padding_left" format="dimension" /><!--日历内部左padding-->
- <attr name="calendar_padding_right" format="dimension" /><!--日历内部右padding-->
-
-
- <attr name="week_background" format="color" /><!--周背景-->
- <attr name="week_line_background" format="color" /><!--线条颜色-->
- <attr name="week_text_color" format="color" /><!--周栏字体颜色-->
- <attr name="week_text_size" format="dimension" /><!--周栏字体大小-->
- <attr name="week_line_margin" format="dimension" /><!--线条margin-->
-
- <attr name="month_view" format="string" /><!--完全自定义月视图-->
- <attr name="week_view" format="string" /> <!--完全自定义周视图-->
- <attr name="year_view" format="string" /> <!--完全自定义年视图-->
- <attr name="week_bar_height" format="dimension" /> <!--星期栏的高度-->
- <attr name="week_bar_view" format="string" /> <!--如果需要的话使用自定义星期栏-->
-
- <attr name="scheme_text" format="string" />
- <attr name="day_text_size" format="dimension" />
- <attr name="lunar_text_size" format="dimension" />
- <attr name="calendar_height" format="dimension" />
- <attr name="calendar_match_parent" format="boolean" /> <!-- 全屏日历 -->
- <attr name="scheme_text_color" format="color" />
- <attr name="scheme_month_text_color" format="color" />
- <attr name="scheme_lunar_text_color" format="color" />
- <attr name="scheme_theme_color" format="color" />
-
- <attr name="selected_theme_color" format="color" />
- <attr name="selected_text_color" format="color" />
- <attr name="selected_lunar_text_color" format="color" />
-
- <attr name="current_day_text_color" format="color" />
- <attr name="current_day_lunar_text_color" format="color" />
- <attr name="current_month_text_color" format="color" />
- <attr name="other_month_text_color" format="color" />
- <attr name="current_month_lunar_text_color" format="color" />
- <attr name="other_month_lunar_text_color" format="color" />
-
-
- <!-- 年视图相关 -->
- <attr name="year_view_month_text_size" format="dimension" />
- <attr name="year_view_day_text_size" format="dimension" />
- <attr name="year_view_month_text_color" format="color" />
- <attr name="year_view_current_day_text_color" format="color" />
- <attr name="year_view_day_text_color" format="color" />
- <attr name="year_view_select_text_color" format="color" />
- <attr name="year_view_scheme_color" format="color" />
- <attr name="year_view_background" format="color" />
- <attr name="year_view_month_height" format="dimension" />
- <attr name="year_view_week_height" format="dimension" />
- <attr name="year_view_week_text_size" format="dimension" />
- <attr name="year_view_week_text_color" format="color" />
-
- <attr name="year_view_padding" format="dimension" />
- <attr name="year_view_padding_left" format="dimension" />
- <attr name="year_view_padding_right" format="dimension" />
-
- <attr name="year_view_month_padding_top" format="dimension" />
- <attr name="year_view_month_padding_left" format="dimension" />
- <attr name="year_view_month_padding_right" format="dimension" />
- <attr name="year_view_month_padding_bottom" format="dimension" />
-
- <!--日期范围-->
- <attr name="min_year" format="integer" />
- <attr name="max_year" format="integer" />
- <attr name="min_year_month" format="integer" />
- <attr name="max_year_month" format="integer" />
- <attr name="min_year_day" format="integer" />
- <attr name="max_year_day" format="integer" />
-
-
- <!--月视图是否可滚动-->
- <attr name="month_view_scrollable" format="boolean" />
- <!--周视图是否可滚动-->
- <attr name="week_view_scrollable" format="boolean" />
- <!--年视图是否可滚动-->
- <attr name="year_view_scrollable" format="boolean" />
-
- <!-- 月份显示模式 -->
- <attr name="month_view_show_mode">
- <enum name="mode_all" value="0" />
- <enum name="mode_only_current" value="1" />
- <enum name="mode_fix" value="2" />
- </attr>
-
- <!-- 自定义周起始 -->
- <attr name="week_start_with">
- <enum name="sun" value="1" />
- <enum name="mon" value="2" />
- <enum name="sat" value="7" />
- </attr>
-
- <!-- 自定义选择模式 -->
- <attr name="select_mode">
- <enum name="default_mode" value="0" />
- <enum name="single_mode" value="1" />
- <enum name="range_mode" value="2" />
- <enum name="multi_mode" value="3" />
- </attr>
-
- <!-- when select_mode = multi_mode -->
- <attr name="max_multi_select_size" format="integer" />
-
- <!-- when select_mode = range_mode -->
- <attr name="min_select_range" format="integer" />
- <attr name="max_select_range" format="integer" />
-
- <!-- auto select day -->
- <attr name="month_view_auto_select_day">
- <enum name="first_day_of_month" value="0" />
- <enum name="last_select_day" value="1" />
- <enum name="last_select_day_ignore_current" value="2" />
- </attr>
- </declare-styleable>
-
-
- <declare-styleable name="CalendarLayout">
- <attr name="default_status">
- <enum name="expand" value="0" />
- <enum name="shrink" value="1" />
- </attr>
-
-
- <!-- 手势模式 -->
- <attr name="gesture_mode">
- <enum name="default_mode" value="0" />
- <!--<enum name="only_calendar" value="1" />-->
- <enum name="disabled" value="2" />
- </attr>
-
- <attr name="calendar_show_mode">
- <enum name="both_month_week_view" value="0" />
- <enum name="only_week_view" value="1" />
- <enum name="only_month_view" value="2" />
- </attr>
- <attr name="calendar_content_view_id" format="integer" />
- </declare-styleable>
-
-
- </resources>
- //日历 android 引入
- implementation 'com.haibin:calendarview:3.6.8'
-
- //日历 androidx 引入
- implementation 'com.haibin:calendarview:3.7.1'
- <com.haibin.calendarview.CalendarView
- android:id="@+id/mCalendarView"
- android:layout_width="416dp"
- android:layout_height="@dimen/dimen_322"
- android:layout_marginLeft="@dimen/dp_2"
- android:layout_marginRight="@dimen/dp_2"
- android:layout_marginBottom="@dimen/dp_2"
- android:background="@drawable/data_cl_bg"
- app:calendar_height="@dimen/dp_50"
- app:current_day_lunar_text_color="#2856AC"
- app:current_day_text_color="#2856AC"
- app:min_year="2023"
- app:month_view="csu.xiaoya.robotApp.ui.activity.homepage.calendar.CustomMonthView"
- app:other_month_lunar_text_color="@color/white"
- app:other_month_text_color="@color/white"
- app:scheme_lunar_text_color="@color/color_grey_9"
- app:scheme_text_color="@color/color333"
- app:select_mode="default_mode"
- app:selected_lunar_text_color="@color/white"
- app:selected_text_color="@color/white"
- app:selected_theme_color="#4868C3"
- app:week_background="#172B7C"
- app:week_bar_height="@dimen/dp_24"
- app:week_bar_view="csu.xiaoya.robotApp.ui.activity.homepage.calendar.CustomWeekBar"
- app:week_text_color="@color/white" />
由于自定义view跟日历界面在同一目录下
故引用如下:
app:month_view="csu.xiaoya.robotApp.ui.activity.homepage.calendar.CustomMonthView"
app:week_bar_view="csu.xiaoya.robotApp.ui.activity.homepage.calendar.CustomWeekBar"
-
- /**
- * 标记
- * 节假日与节气
- */
- public class CustomMonthView extends MonthView {
-
- private int mRadius;
-
- /**
- * 自定义魅族标记的文本画笔
- */
- private Paint mTextPaint = new Paint();
-
-
- /**
- * 24节气与
- * 节假日画笔
- */
- private Paint mSolarTermTextPaint = new Paint();
-
- /**
- * 网格画笔
- */
- private Paint mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
-
- /**
- * 背景圆点
- */
- private Paint mPointPaint = new Paint();
-
- /**
- * 今天的背景色
- */
- private Paint mCurrentDayPaint = new Paint();
-
- /**
- * 圆点半径
- */
- private float mPointRadius;
-
- private int mPadding;
-
- private float mCircleRadius;
- /**
- * 自定义背景
- */
- private Paint mSchemeBasicPaint = new Paint();
-
- private float mSchemeBaseLine;
- private int currentMonth = 0;//当前月份
-
- public CustomMonthView(Context context) {
- super(context);
-
- mTextPaint.setTextSize(dipToPx(context, 8));
- mTextPaint.setColor(0xffffffff);
- mTextPaint.setAntiAlias(true);
- mTextPaint.setFakeBoldText(true);
-
- //* Typeface.BOLD //粗体
- //* Typeface.BOLD_ITALIC //粗斜体
- //* Typeface.ITALIC //斜体
- //* Typeface.NORMAL //常规
- mSolarTermTextPaint.setColor(0xffE92F3A);
- Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
- mSolarTermTextPaint.setTypeface(font);
- mSolarTermTextPaint.setAntiAlias(true);
- mSolarTermTextPaint.setTextAlign(Paint.Align.CENTER);
-
- mSchemeBasicPaint.setAntiAlias(true);
- mSchemeBasicPaint.setStyle(Paint.Style.FILL);
- mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
- mSchemeBasicPaint.setFakeBoldText(true);
- mSchemeBasicPaint.setColor(Color.parseColor("#00ffffff"));
-
-
- mCurrentDayPaint.setAntiAlias(true);
- mCurrentDayPaint.setStyle(Paint.Style.FILL);
- mCurrentDayPaint.setColor(0xFFffffff);
-
- mPointPaint.setAntiAlias(true);
- mPointPaint.setStyle(Paint.Style.FILL);
- mPointPaint.setTextAlign(Paint.Align.CENTER);
- mPointPaint.setColor(Color.RED);
-
- mRectPaint.setStyle(Paint.Style.STROKE);
- mRectPaint.setStrokeWidth(dipToPx(context, 0.5f));
- mRectPaint.setColor(0x88DBDDE4);
-
- mCircleRadius = dipToPx(getContext(), 7);
-
- mPadding = dipToPx(getContext(), 8);
-
- mPointRadius = dipToPx(context, 2);
-
- Paint.FontMetrics metrics = mSchemeBasicPaint.getFontMetrics();
- mSchemeBaseLine = mCircleRadius - metrics.descent + (metrics.bottom - metrics.top) / 2 + dipToPx(getContext(), 1);
-
- }
-
-
- @Override
- protected void onPreviewHook() {
- mSolarTermTextPaint.setTextSize(mCurMonthLunarTextPaint.getTextSize());
- mRadius = Math.min(mItemWidth, mItemHeight) / 11 * 5;
- }
-
-
- @Override
- protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
- int cx = x + mItemWidth * 1;
- int cy = y + mItemHeight * 1;
- canvas.drawRect(x, y, cx, cy, mSelectedPaint);
- // canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
- return true;
- }
-
- @Override
- protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
-
- // boolean isSelected = isSelected(calendar);
- // if (isSelected) {
- // mPointPaint.setColor(Color.WHITE);
- // } else {
- // mPointPaint.setColor(Color.BLUE);
- // }
- // canvas.drawCircle(x + mItemWidth / 2, y + mItemHeight - 3 * mPadding, mPointRadius, mPointPaint);
- }
-
- @SuppressWarnings("IntegerDivisionInFloatingPointContext")
- @Override
- protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
- int cx = x + mItemWidth / 2;
- int cy = y + mItemHeight / 2;
- int top = y - mItemHeight / 6;
-
- canvas.drawRect(x, y, x + mItemWidth, y + mItemHeight, mRectPaint);
-
- if (hasScheme) {
- canvas.drawCircle(x + mItemWidth - mPadding - mCircleRadius / 2, y + mPadding + mCircleRadius, mCircleRadius, mSchemeBasicPaint);
- mTextPaint.setColor(calendar.getSchemeColor());
- //可通过修改坐标值调整tag位置
- canvas.drawText(calendar.getScheme(), x + mItemWidth - mPadding - mCircleRadius, y + mPadding + mSchemeBaseLine-16, mTextPaint);
- }
-
- //当然可以换成其它对应的画笔就不麻烦,
- if (calendar.isWeekend() && calendar.isCurrentMonth()) {
- mCurMonthTextPaint.setColor(0xFFE30003);
- mCurMonthLunarTextPaint.setColor(0xFF999999);
-
- mSchemeTextPaint.setColor(0xFFE30003);
- mSchemeLunarTextPaint.setColor(0xFF999999);
- // mOtherMonthLunarTextPaint.setColor(0xFF489dff);
- // mOtherMonthTextPaint.setColor(0xFF489dff);
- } else {
- mCurMonthTextPaint.setColor(0xff333333);
- mCurMonthLunarTextPaint.setColor(0xFF999999);
-
- mSchemeTextPaint.setColor(0xff333333);
- mSchemeLunarTextPaint.setColor(0xFF999999);
-
- // mOtherMonthTextPaint.setColor(0xFFe1e1e1);
- // mOtherMonthLunarTextPaint.setColor(0xFFe1e1e1);
- }
- // Log.d("节假日", "======onDrawText: =======" + calendar.getTraditionFestival());
- if (isSelected) {
- canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top, mSelectTextPaint);
- canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + y + mItemHeight / 10, mSelectedLunarTextPaint);
- } else if (hasScheme) {
-
- canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top, calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
-
- canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + y + mItemHeight / 10, !TextUtils.isEmpty(calendar.getSolarTerm() + calendar.getTraditionFestival() + calendar.getGregorianFestival()) ? mSolarTermTextPaint : mSchemeLunarTextPaint);
- } else {
- canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top, calendar.isCurrentDay() ? mCurDayTextPaint : calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
-
- canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + y + mItemHeight / 10, calendar.isCurrentDay() ? mCurDayLunarTextPaint : calendar.isCurrentMonth() ? !TextUtils.isEmpty(calendar.getSolarTerm() + calendar.getTraditionFestival() + calendar.getGregorianFestival()) ? mSolarTermTextPaint : mCurMonthLunarTextPaint : mOtherMonthLunarTextPaint);
- }
- //当日
- if (calendar.isCurrentDay() && !calendar.isCurrentMonth()) {
- canvas.drawCircle(cx, cy, mRadius, mCurrentDayPaint);
- canvas.drawText(String.valueOf(calendar.getDay()), cx, cy, mCurrentDayPaint);
- }
- }
-
- /**
- * dp转px
- *
- * @param context context
- * @param dpValue dp
- * @return px
- */
- private static int dipToPx(Context context, float dpValue) {
- final float scale = context.getResources().getDisplayMetrics().density;
- return (int) (dpValue * scale + 0.5f);
- }
- }
-
- /**
- * 自定义日历
- * 周布局
- */
- public class CustomWeekBar extends WeekBar {
-
- private int mPreSelectedIndex;
-
- public CustomWeekBar(Context context) {
- super(context);
- LayoutInflater.from(context).inflate(R.layout.custom_week_bar, this, true);
- setBackgroundColor(Color.WHITE);
- }
-
- @Override
- protected void onDateSelected(Calendar calendar, int weekStart, boolean isClick) {
- getChildAt(mPreSelectedIndex).setSelected(false);
- int viewIndex = getViewIndexByCalendar(calendar, weekStart);
- getChildAt(viewIndex).setSelected(true);
- mPreSelectedIndex = viewIndex;
- }
-
- /**
- * 当周起始发生变化,使用自定义布局需要重写这个方法,避免出问题
- *
- * @param weekStart 周起始
- */
- @Override
- protected void onWeekStartChange(int weekStart) {
- for (int i = 0; i < getChildCount(); i++) {
- ((TextView) getChildAt(i)).setText(getWeekString(i, weekStart));
- }
- }
-
- /**
- * 或者周文本,这个方法仅供父类使用
- *
- * @param index index
- * @param weekStart weekStart
- * @return 或者周文本
- */
- private String getWeekString(int index, int weekStart) {
- String[] weeks = getContext().getResources().getStringArray(R.array.chinese_week_string_array);
-
- if (weekStart == 1) {
- return weeks[index];
- }
- if (weekStart == 2) {
- return weeks[index == 6 ? 0 : index + 1];
- }
- return weeks[index == 0 ? 6 : index - 1];
- }
- }
- // 设置日程消息提醒标记
- Map<String, Calendar> map = new HashMap<>();
- map.put(getSchemeCalendar(2024, 2, 23, 0x00ffffff, "声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/543232推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。