——CalendarView
【若对该知识点有更多想了解的,欢迎私信博主~~】
分build.gradle中添加(注入依赖)
dependencies {
implementation 'com.haibin:calendarview:3.6.2'
}
dependencies {
implementation 'com.android.support:support-v4:27.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>
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)]
给日历添加标注
@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;
}
官方文档: