【Android开发--新手必看篇】依赖框架CalendarView的使用

狄飞鹏
2023-12-01

Android笔记

​ ——CalendarView
若对该知识点有更多想了解的,欢迎私信博主~~

依赖框架:CalendarView

一:优势及作用
  1. 基于Canvas绘制,极速性能
  2. 热插拔思想,任意定制周视图、月视图,即插即用!
  3. 支持单选、多选、范围选择、国内手机日历默认自动选择等选择模式
  4. 支持静态、动态设置周起始,一行代码搞定
  5. 支持静态、动态设置日历项高度、日历填充模式
  6. 支持设置任意日期范围、任意拦截日期
  7. 支持多点触控、手指平滑切换过渡,拒绝界面抖动
  8. 类NestedScrolling特性,嵌套滚动
  9. 支持英语、繁体、简体,任意定制实现
二:CalendarView的配置
  1. 分build.gradle中添加(注入依赖)

    dependencies {
    	implementation 'com.haibin:calendarview:3.6.2'
    }
    
    注:若报错
    Error: Program type already present: android.support.v4.app.FragmentTransitionCompat21$2
    dependencies {
    	implementation 'com.android.support:support-v4:27.1.1'//再添加这句
    }
    
三:使用
  1. 布局

    <com.haibin.calendarview.CalendarLayout
            android:id="@+id/calendarLayout"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:orientation="vertical">
    
            <com.haibin.calendarview.CalendarView
                android:id="@+id/calendarView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#fff"
                app:calendar_padding="10dp"
                app:month_view="com.example.myapplication5.MyMonthView"//自定义
                app:select_mode="multi_mode"
                app:selected_theme_color="#029A9A"
                app:year_view_day_text_size="9sp"
                app:year_view_month_text_size="20sp"
                app:year_view_scheme_color="#f17706" />
    </com.haibin.calendarview.CalendarLayout>
    
    此时已经可以使用
  2. CalendarView的优点在于可以完全的自定义样式

    package com.example.myapplication5;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    
    import com.haibin.calendarview.Calendar;
    import com.haibin.calendarview.MultiMonthView;
    
    public class MyMonthView extends MultiMonthView {
        private int mRadius;
    
        public MyMonthView(Context context) {
            super(context);
        }
    
        @Override
        protected void onPreviewHook() {
            mRadius = Math.min(mItemWidth, mItemHeight) / 5 * 2;
            //FILL 内部填充
            //STROKE 描边
            //FILL_AND_STROKE 内部填充+描边
            mSchemePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        }
    
        @Override
        protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelectedPre, boolean isSelectedNext) {
            int cx = x + mItemWidth / 2;
            int cy = y + mItemHeight / 2;
    
            //可点开onDrawSelected自行查看参数说明
            if (isSelectedPre) {
                if (isSelectedNext) {
                    canvas.drawRect(x, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
                } else {//最后一个,the last
                    canvas.drawRect(x, cy - mRadius, cx, cy + mRadius, mSelectedPaint);
                    canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
                }
            } else {
                if (isSelectedNext) {
                    canvas.drawRect(cx, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
                }
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
            }
            return false;
        }
    
        @Override
        protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y, boolean isSelected) {
            int cx = x + mItemWidth / 2;
            int cy = y + mItemHeight / 2;
            canvas.drawCircle(cx, cy, mRadius, mSchemePaint);
        }
    
        @Override
        protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
            //建立文字原点
            float baselineY = mTextBaseLine + y;
            int cx = x + mItemWidth / 2;
            //判断当前日期是否在本月中
            boolean isInRange = isInRange(calendar);
            //判断当前日期是否可用
            boolean isEnable = !onCalendarIntercept(calendar);
            //如果该日期被选中,添加选中样式
            if (isSelected) {
                canvas.drawText(String.valueOf(calendar.getDay()),
                        cx,
                        baselineY,
                        mSelectTextPaint);
            }
            //如果没被选中但是是标注日期
            else if (hasScheme) {
                canvas.drawText(String.valueOf(calendar.getDay()),
                        cx,
                        baselineY,
                        calendar.isCurrentDay() ? mCurDayTextPaint :
                                calendar.isCurrentMonth() && isInRange && isEnable ? mSchemeTextPaint : mOtherMonthTextPaint);
            }
            //其他
            else {
                canvas.drawText(String.valueOf(calendar.getDay()),
                        cx,
                        baselineY,
                        calendar.isCurrentDay() ? mCurDayTextPaint :
                                calendar.isCurrentMonth() && isInRange && isEnable ? mCurMonthTextPaint : mOtherMonthTextPaint);
            }
        }
    }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M7VlAUjr-1600865687033)(…\image\2020-08-07_145537.png)]

  3. 给日历添加标注

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    
        Map<String, Calendar> map = new HashMap<>();
        map.put(getSchemeCalendar(2020,8,5,0xFF40db25,"H1").toString(),
                getSchemeCalendar(2020,8,5,0xFF40db25,"H1"));
    }
    
    //制作方法,用于以后向日历中添加标注
    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
            Calendar calendar = new Calendar();
            calendar.setYear(year);
            calendar.setMonth(month);
            calendar.setDay(day);
            calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
            calendar.setScheme(text);
            return calendar;
    }
    

官方文档:

  1. https://github.com/huanghaibin-dev/CalendarView
  2. 中文文档:https://github.com/huanghaibin-dev/CalendarView/blob/master/QUESTION_ZH.md
 类似资料: