https://github.com/huanghaibin-dev/CalendarViewjava
/** * 定製高仿魅族日曆界面,按你的想象力繪製出各類各樣的界面 * Created by huanghaibin on 2017/11/15. */
public class MeiZuMonthView extends MonthView {
/** * 繪製選中的日子 * * @param canvas canvas * @param calendar 日曆日曆calendar * @param x 日曆Card x起點座標 * @param y 日曆Card y起點座標 * @param hasScheme hasScheme 非標記的日期 * @return 返回true 則繪製onDrawScheme,由於這裏背景色不是是互斥的,因此返回true */
@Override
protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
canvas.drawRect(x + mPadding, y + mPadding, x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSelectedPaint);
return true;
}
/** * 繪製標記的事件日子 * * @param canvas canvas * @param calendar 日曆calendar * @param x 日曆Card x起點座標 * @param y 日曆Card y起點座標 */
@Override
protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
canvas.drawCircle(x + mItemWidth - mPadding - mRadio / 2, y + mPadding + mRadio, mRadio, mSchemeBasicPaint);
canvas.drawText(calendar.getScheme(),
x + mItemWidth - mPadding - mRadio / 2 - getTextWidth(calendar.getScheme()) / 2,
y + mPadding + mSchemeBaseLine, mTextPaint);
}
/** * 繪製文本 * * @param canvas canvas * @param calendar 日曆calendar * @param x 日曆Card x起點座標 * @param y 日曆Card y起點座標 * @param hasScheme 是不是標記的日期 * @param isSelected 是否選中 */
@Override
protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
int cx = x + mItemWidth / 2;
int top = y - mItemHeight / 6;
boolean isInRange = isInRange(calendar);
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() && isInRange ? mSchemeTextPaint : mOtherMonthTextPaint);
canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + y + mItemHeight / 10, mCurMonthLunarTextPaint);
} else {
canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
calendar.isCurrentDay() ? mCurDayTextPaint :
calendar.isCurrentMonth() && isInRange ? mCurMonthTextPaint : mOtherMonthTextPaint);
canvas.drawText(calendar.getLunar(), cx, mTextBaseLine + y + mItemHeight / 10,
calendar.isCurrentDay() && isInRange ? mCurDayLunarTextPaint :
calendar.isCurrentMonth() ? mCurMonthLunarTextPaint : mOtherMonthLunarTextPaint);
}
}
}
複製代碼
<attr name="month_view" format="string" /><!--自定義月視圖路徑-->
<attr name="week_view" format="string" /> <!--自定義周視圖路徑-->
app:month_view="com.haibin.calendarviewproject.MeiZuCalendarCardView"
app:week_view="com.haibin.calendarviewproject.MeiZuWeekView"
複製代碼
mCalendarView.setWeekView(MeizuWeekView.class);
mCalendarView.setMonthView(MeizuMonthView.class);
複製代碼
CalendarView.scrollToCalendar();
CalendarView.scrollToNext();
CalendarView.scrollToPre();
CalendarView.scrollToXXX();
複製代碼
app:week_start_with="mon、sun、sat"
CalendarView.setWeekStarWithSun();
CalendarView.setWeekStarWithMon();
CalendarView.setWeekStarWithSat();
複製代碼
public class CustomRangeMonthView extends RangeMonthView{
}
public class CustomRangeWeekView extends RangeWeekView{
}
複製代碼
而後你須要設置選擇模式爲範圍模式:select_mode="range_mode"git
酒店式日曆場景固然是不能從昨天開始訂房的,也不能無限期訂房,因此你須要靜態或動態設置日曆範圍、精確到具體某一天!!!github
<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" />
CalendarView.setRange(int minYear, int minYearMonth, int minYearDay,
int maxYear, int maxYearMonth, int maxYearDay)
複製代碼
//設置日期攔截事件
mCalendarView.setOnCalendarInterceptListener(new CalendarView.OnCalendarInterceptListener() {
@Override
public boolean onCalendarIntercept(Calendar calendar) {
//這裏寫攔截條件,返回true表明攔截
return calendar.isWeekend();
}
@Override
public void onCalendarInterceptClick(Calendar calendar, boolean isClick) {
//todo 點擊攔截的日期回調
}
});
複製代碼
boolean isInRange = isInRange(calendar);//日期是否在範圍內,超出範圍的能夠置灰
boolean isEnable = !onCalendarIntercept(calendar);//日期是否可用,沒有被攔截,被攔截的能夠置灰
複製代碼
也許你只須要像原生日曆那樣就夠了,但原生日曆那奇怪且十分不友好的style,受到theme的影響,各類頭疼,使用此控件,你只須要簡簡單單定製月視圖就夠了,CalendarView 能很是簡單就高仿各類日曆UIcanvas
CalendarView 提供了 setSchemeDate(Map<String, Calendar> mSchemeDates) 這個十分高效的API用來動態標記事務,即時你的數據量達到數千、數萬、數十萬,都不會對UI渲染形成影響bash
日曆類 Calendar 提供了許多十分有用的APIapp
boolean isWeekend();//判斷是否是週末,能夠用不一樣的畫筆繪製週末的樣式
int getWeek();//獲取星期
String getSolarTerm();//獲取24節氣,能夠用不一樣顏色標記不一樣節日
String getGregorianFestival();//獲取公曆節日,自由判斷,把節日換上喜歡的顏色
String getTraditionFestival();//獲取傳統節日
boolean isLeapYear();//是不是閏年
int getLeapMonth();//獲取閏月
boolean isSameMonth(Calendar calendar);//是否相同月
int compareTo(Calendar calendar);//畢竟日期大小 -1 0 1
long getTimeInMillis();//獲取時間戳
int differ(Calendar calendar);//日期運算,相差多少天
複製代碼