From 29e3929ab7fe2343a8bf56b71e609c65d38f30d3 Mon Sep 17 00:00:00 2001 From: huanghaibin Date: Thu, 11 Oct 2018 10:32:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E5=B9=B4=E8=A7=86=E5=9B=BE=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- README.md | 4 +- README_EN.md | 4 +- .../calendarviewproject/MainActivity.java | 20 +- app/src/main/res/layout/activity_main.xml | 11 +- app/src/main/res/values/styles.xml | 2 + calendarview/build.gradle | 4 +- .../haibin/calendarview/CalendarUtilTest.java | 120 ---- .../com/haibin/calendarview/BaseView.java | 1 - .../haibin/calendarview/CalendarLayout.java | 4 +- .../com/haibin/calendarview/CalendarUtil.java | 152 +---- .../com/haibin/calendarview/CalendarView.java | 4 +- .../calendarview/CalendarViewDelegate.java | 126 +++- .../haibin/calendarview/DefaultYearView.java | 91 +++ .../haibin/calendarview/MonthViewPager.java | 4 +- .../haibin/calendarview/YearRecyclerView.java | 16 +- .../com/haibin/calendarview/YearView.java | 547 ++++++++++++++---- ...{YearAdapter.java => YearViewAdapter.java} | 47 +- ...tLayout.java => YearViewSelectLayout.java} | 14 +- .../src/main/res/layout/cv_item_list_year.xml | 26 - .../res/layout/cv_layout_calendar_view.xml | 2 +- .../src/main/res/values-zh/strings.xml | 10 + calendarview/src/main/res/values/attrs.xml | 9 +- calendarview/src/main/res/values/strings.xml | 9 + gradle.properties | 4 +- 25 files changed, 750 insertions(+), 483 deletions(-) create mode 100644 calendarview/src/main/java/com/haibin/calendarview/DefaultYearView.java rename calendarview/src/main/java/com/haibin/calendarview/{YearAdapter.java => YearViewAdapter.java} (53%) rename calendarview/src/main/java/com/haibin/calendarview/{YearSelectLayout.java => YearViewSelectLayout.java} (93%) delete mode 100644 calendarview/src/main/res/layout/cv_item_list_year.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index c0f68edd..99202cc2 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/README.md b/README.md index be62a45d..a33f35c2 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,13 @@ Android上一个优雅、高度自定义、性能高效的日历控件,完美 ### Gradle ``` -compile 'com.haibin:calendarview:3.4.7' +compile 'com.haibin:calendarview:3.4.8' ``` ``` com.haibin calendarview - 3.4.7 + 3.4.8 pom ``` diff --git a/README_EN.md b/README_EN.md index 3b2008da..38b74892 100644 --- a/README_EN.md +++ b/README_EN.md @@ -5,13 +5,13 @@ This widget has obvious advantages of memory and efficiency, adapt to many scene ### Gradle ``` -compile 'com.haibin:calendarview:3.4.7' +compile 'com.haibin:calendarview:3.4.8' ``` ``` com.haibin calendarview - 3.4.7 + 3.4.8 pom ``` diff --git a/app/src/main/java/com/haibin/calendarviewproject/MainActivity.java b/app/src/main/java/com/haibin/calendarviewproject/MainActivity.java index 08d87b01..322069ed 100644 --- a/app/src/main/java/com/haibin/calendarviewproject/MainActivity.java +++ b/app/src/main/java/com/haibin/calendarviewproject/MainActivity.java @@ -16,11 +16,10 @@ import com.haibin.calendarviewproject.base.activity.BaseActivity; import com.haibin.calendarviewproject.colorful.ColorfulActivity; import com.haibin.calendarviewproject.custom.CustomActivity; -import com.haibin.calendarviewproject.custom.CustomMonthView; -import com.haibin.calendarviewproject.custom.CustomWeekBar; -import com.haibin.calendarviewproject.custom.CustomWeekView; import com.haibin.calendarviewproject.index.IndexActivity; import com.haibin.calendarviewproject.meizu.MeiZuActivity; +import com.haibin.calendarviewproject.meizu.MeiZuMonthView; +import com.haibin.calendarviewproject.meizu.MeizuWeekView; import com.haibin.calendarviewproject.pager.ViewPagerActivity; import com.haibin.calendarviewproject.progress.ProgressActivity; import com.haibin.calendarviewproject.range.RangeActivity; @@ -89,15 +88,6 @@ public void onClick(View v) { mTextMonthDay.setText(String.valueOf(mYear)); } }); - findViewById(R.id.fl_current).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - //mCalendarView.scrollToCurrent(); - //mCalendarView.scrollToCalendar(2018,7,14); - //Log.e("scrollToCurrent", " -- " + mCalendarView.getSelectedCalendar()); - - } - }); findViewById(R.id.iv_more).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -292,9 +282,9 @@ public void onClick(DialogInterface dialog, int which) { } break; case 4: - mCalendarView.setWeekView(CustomWeekView.class); - mCalendarView.setMonthView(CustomMonthView.class); - mCalendarView.setWeekBar(CustomWeekBar.class); + mCalendarView.setWeekView(MeizuWeekView.class); + mCalendarView.setMonthView(MeiZuMonthView.class); + mCalendarView.setWeekBar(EnglishWeekBar.class); break; case 5: mCalendarView.setAllMode(); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fc8f4c3f..b9beb7a6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -158,16 +158,19 @@ app:selected_theme_color="#80cfcfcf" app:week_background="#fff" app:week_bar_height="46dp" - app:month_view="com.haibin.calendarviewproject.meizu.MeiZuMonthView" - app:week_view="com.haibin.calendarviewproject.meizu.MeizuWeekView" - app:week_bar_view="com.haibin.calendarviewproject.EnglishWeekBar" + app:month_view="com.haibin.calendarviewproject.custom.CustomMonthView" + app:week_view="com.haibin.calendarviewproject.custom.CustomWeekView" + app:week_bar_view="com.haibin.calendarviewproject.custom.CustomWeekBar" app:week_line_background="#00000000" app:week_line_margin="16dp" app:week_start_with="mon" app:week_text_size="10sp" + app:year_view_week_height="0dp" + app:year_view_week_text_color="#666666" + app:year_view_week_text_size="7sp" app:year_view_background="#f6f6f6" app:year_view_day_text_color="#333333" - app:year_view_day_text_size="9sp" + app:year_view_day_text_size="8sp" app:year_view_month_text_color="#ff0000" app:year_view_month_text_size="20sp" app:year_view_scheme_color="#f17706" /> diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index d5e67d10..55a4ab03 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -8,6 +8,8 @@ @color/colorAccent false true + @null + true diff --git a/calendarview/build.gradle b/calendarview/build.gradle index b592c7e6..c13af74d 100644 --- a/calendarview/build.gradle +++ b/calendarview/build.gradle @@ -8,8 +8,8 @@ android { minSdkVersion 15 //noinspection OldTargetApi targetSdkVersion 23 - versionCode 347 - versionName "3.4.7" + versionCode 348 + versionName "3.4.8" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/calendarview/src/androidTest/java/com/haibin/calendarview/CalendarUtilTest.java b/calendarview/src/androidTest/java/com/haibin/calendarview/CalendarUtilTest.java index 9fd0df76..27369127 100644 --- a/calendarview/src/androidTest/java/com/haibin/calendarview/CalendarUtilTest.java +++ b/calendarview/src/androidTest/java/com/haibin/calendarview/CalendarUtilTest.java @@ -25,34 +25,6 @@ public void getMonthViewHeight() throws Exception { assertEquals(6, CalendarUtil.getMonthViewHeight(2018, 7, 1, 2)); } - /** - * 根据星期数和最小年份推算出该星期的第一天 - * - * @throws Exception Exception - */ - @Test - public void getFirstCalendarFromWeekCount() throws Exception { - Calendar calendar = new Calendar(); - calendar.setYear(2017); - calendar.setMonth(12); - calendar.setDay(30); - - Calendar firstCalendar = CalendarUtil.getFirstCalendarFromWeekCount(2018, 1, 1, 7); - assertEquals(calendar, firstCalendar); - - calendar.setYear(2017); - calendar.setMonth(12); - calendar.setDay(31); - firstCalendar = CalendarUtil.getFirstCalendarFromWeekCount(2018, 1, 1, 1); - assertEquals(calendar, firstCalendar); - - calendar.setYear(2018); - calendar.setMonth(1); - calendar.setDay(1); - firstCalendar = CalendarUtil.getFirstCalendarFromWeekCount(2018, 1, 1, 2); - assertEquals(calendar, firstCalendar); - } - /** * 根据星期数和最小日期推算出该星期的第一天 */ @@ -89,43 +61,6 @@ public void getFirstCalendarStartWithMinCalendar() throws Exception { assertEquals(calendar, firstCalendar); } - /** - * 获取两个年份之间一共有多少周,注意周起始周一、周日、周六 - * - * @throws Exception Exception - */ - @Test - public void getWeekCountBetweenYearAndYear() throws Exception { - int count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 12, 1); - assertEquals(53, count); - - - count = CalendarUtil.getWeekCountBetweenYearAndYear(2017, 1, 2018, 12, 1); - assertEquals(105, count); - - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 2, 1); - assertEquals(9, count); - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 2, 2); - assertEquals(9, count); - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 2, 7); - assertEquals(9, count); - - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 3, 1); - assertEquals(13, count); - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 3, 2); - assertEquals(13, count); - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 3, 7); - assertEquals(14, count); - - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 4, 1); - assertEquals(18, count); - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 4, 2); - assertEquals(18, count); - count = CalendarUtil.getWeekCountBetweenYearAndYear(2018, 1, 2018, 4, 7); - assertEquals(18, count); - } - - /** * 获取两个日期之间一共有多少周, * 注意周起始周一、周日、周六 @@ -377,59 +312,4 @@ public void differ() throws Exception { assertEquals(7,CalendarUtil.differ(calendar1,calendar2)); } - - - /** - * 根据日期获取两个年份中第几周,用来设置 WeekView currentItem - * - * @throws Exception Exception - */ - @Test - public void getWeekFromCalendarBetweenYearAndYear() throws Exception { - Calendar calendar = new Calendar(); - - calendar.setYear(2018); - calendar.setMonth(1); - calendar.setDay(13); - - int weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 2, 1); - assertEquals(2, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 1, 2); - assertEquals(2, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 1, 7); - assertEquals(3, weekIndex); - - calendar.setYear(2018); - calendar.setMonth(1); - calendar.setDay(31); - - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 1, 1); - assertEquals(5, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 1, 2); - assertEquals(5, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 1, 7); - assertEquals(5, weekIndex); - - calendar.setYear(2018); - calendar.setMonth(2); - calendar.setDay(13); - - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 2, 1); - assertEquals(3, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 2, 2); - assertEquals(3, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 2, 7); - assertEquals(3, weekIndex); - - calendar.setYear(2018); - calendar.setMonth(3); - calendar.setDay(17); - - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 2, 1); - assertEquals(7, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 2, 2); - assertEquals(7, weekIndex); - weekIndex = CalendarUtil.getWeekFromCalendarBetweenYearAndYear(calendar, 2018, 2, 7); - assertEquals(8, weekIndex); - } } \ No newline at end of file diff --git a/calendarview/src/main/java/com/haibin/calendarview/BaseView.java b/calendarview/src/main/java/com/haibin/calendarview/BaseView.java index 3bfc86b3..a5b89db3 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/BaseView.java +++ b/calendarview/src/main/java/com/haibin/calendarview/BaseView.java @@ -51,7 +51,6 @@ public abstract class BaseView extends View implements View.OnClickListener, Vie */ protected Paint mCurMonthLunarTextPaint = new Paint(); - /** * 当前月份农历文本颜色 */ diff --git a/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java b/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java index f6253dc8..edc544d7 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java +++ b/calendarview/src/main/java/com/haibin/calendarview/CalendarLayout.java @@ -100,7 +100,7 @@ public class CalendarLayout extends LinearLayout { /** * 年视图 */ - YearSelectLayout mYearView; + YearViewSelectLayout mYearView; /** * ContentView @@ -481,7 +481,7 @@ protected void onFinishInflate() { mMonthView = (MonthViewPager) findViewById(R.id.vp_month); mWeekPager = (WeekViewPager) findViewById(R.id.vp_week); mContentView = (ViewGroup) findViewById(mContentViewId); - mYearView = (YearSelectLayout) findViewById(R.id.selectLayout); + mYearView = (YearViewSelectLayout) findViewById(R.id.selectLayout); if (mContentView != null) { mContentView.setOverScrollMode(View.OVER_SCROLL_NEVER); } diff --git a/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java b/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java index ef390f37..bfd2de81 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java +++ b/calendarview/src/main/java/com/haibin/calendarview/CalendarUtil.java @@ -299,38 +299,6 @@ static int getWeekViewIndexFromCalendar(Calendar calendar, int weekStart) { return getWeekViewStartDiff(calendar.getYear(), calendar.getMonth(), calendar.getDay(), weekStart); } - - /** - * 获取两个年份之间一共有多少周,注意周起始周一、周日、周六 - * 测试通过 test pass - * - * @param minYear minYear 最小年份 - * @param minYearMonth maxYear 最小年份月份 - * @param maxYear maxYear 最大年份 - * @param maxYearMonth maxYear 最大年份月份 - * @param weekStart 周起始 - * @return 周数用于WeekViewPager itemCount - */ - @Deprecated - static int getWeekCountBetweenYearAndYear(int minYear, int minYearMonth, int maxYear, int maxYearMonth, int weekStart) { - java.util.Calendar date = java.util.Calendar.getInstance(); - date.set(minYear, minYearMonth - 1, 1); - long minTimeMills = date.getTimeInMillis();//给定时间戳 - int preDiff = getMonthViewStartDiff(minYear, minYearMonth, weekStart); - - date.set(maxYear, maxYearMonth - 1, getMonthDaysCount(maxYear, maxYearMonth)); - - long maxTimeMills = date.getTimeInMillis();//给定时间戳 - - int nextDiff = getMonthEndDiff(maxYear, maxYearMonth, weekStart); - - int count = preDiff + nextDiff; - - int c = (int) ((maxTimeMills - minTimeMills) / ONE_DAY) + 1; - count += c; - return count / 7; - } - /** * 是否在日期范围內 * 测试通过 test pass @@ -472,63 +440,6 @@ static Calendar getFirstCalendarStartWithMinCalendar(int minYear, int minYearMon } - /** - * 根据日期获取两个年份中第几周,用来设置 WeekView currentItem - * 测试通过 Test Pass - * - * @param calendar calendar - * @param minYear minYear - * @return 返回两个年份中第几周 the WeekView currentItem - */ - @Deprecated - static int getWeekFromCalendarBetweenYearAndYear(Calendar calendar, int minYear, int minYearMonth, int weekStar) { - java.util.Calendar date = java.util.Calendar.getInstance(); - date.set(minYear, minYearMonth - 1, 1);//起始日期 - long firstTimeMill = date.getTimeInMillis();//获得范围起始时间戳 - - int preDiff = getWeekViewStartDiff(minYear, minYearMonth, 1, weekStar);//范围起始的周偏移量 - - int weekStartDiff = getWeekViewStartDiff(calendar.getYear(), - calendar.getMonth(), - calendar.getDay(), - weekStar);//获取点击的日子在周视图的起始,为了兼容全球时区,最大日差为一天,如果周起始偏差weekStartDiff=0,则日期加1 - - date.set(calendar.getYear(), - calendar.getMonth() - 1, - weekStartDiff == 0 ? calendar.getDay() + 1 : calendar.getDay()); - - long curTimeMills = date.getTimeInMillis();//给定时间戳 - - int c = (int) ((curTimeMills - firstTimeMill) / ONE_DAY); - - int count = preDiff + c; - - return count / 7 + 1; - } - - - /** - * 是否在日期范围內 - * - * @param calendar calendar - * @param minYear minYear - * @param minYearMonth minYearMonth - * @param maxYear maxYear - * @param maxYearMonth maxYearMonth - * @return 是否在日期范围內 - */ - @Deprecated - static boolean isCalendarInRange(Calendar calendar, int minYear, int minYearMonth, int maxYear, int maxYearMonth) { - java.util.Calendar c = java.util.Calendar.getInstance(); - c.set(minYear, minYearMonth - 1, 1); - long minTime = c.getTimeInMillis(); - c.set(maxYear, maxYearMonth - 1, getMonthDaysCount(maxYear, maxYearMonth)); - long maxTime = c.getTimeInMillis(); - c.set(calendar.getYear(), calendar.getMonth() - 1, calendar.getDay()); - long curTime = c.getTimeInMillis(); - return curTime >= minTime && curTime <= maxTime; - } - /** * 是否在日期范围内 * @@ -559,45 +470,6 @@ static boolean isMonthInRange(int year, int month, int minYear, int minYearMonth !(year == maxYear && month > maxYearMonth); } - /** - * 根据星期数和最小年份推算出该星期的第一天 - * 测试通过 Test pass - * - * @param minYear 最小年份如2017 - * @param minYearMonth maxYear 最小年份月份,like : 2017-07 - * @param week 从最小年份minYear月minYearMonth 日1 开始的第几周 week > 0 - * @return 该星期的第一天日期 - */ - @Deprecated - static Calendar getFirstCalendarFromWeekCount(int minYear, int minYearMonth, int week, int weekStart) { - java.util.Calendar date = java.util.Calendar.getInstance(); - - date.set(minYear, minYearMonth - 1, 1);// - - long firstTimeMills = date.getTimeInMillis();//获得起始时间戳 - - - long weekTimeMills = (week - 1) * 7 * ONE_DAY; - - long timeCountMills = weekTimeMills + firstTimeMills; - - date.setTimeInMillis(timeCountMills); - - int startDiff = getWeekViewStartDiff(date.get(java.util.Calendar.YEAR), - date.get(java.util.Calendar.MONTH) + 1, - date.get(java.util.Calendar.DAY_OF_MONTH), weekStart); - - timeCountMills -= startDiff * ONE_DAY; - date.setTimeInMillis(timeCountMills); - - Calendar calendar = new Calendar(); - calendar.setYear(date.get(java.util.Calendar.YEAR)); - calendar.setMonth(date.get(java.util.Calendar.MONTH) + 1); - calendar.setDay(date.get(java.util.Calendar.DAY_OF_MONTH)); - - return calendar; - } - /** * 运算 calendar1 - calendar2 * test Pass @@ -614,11 +486,11 @@ static int differ(Calendar calendar1, Calendar calendar2) { } java.util.Calendar date = java.util.Calendar.getInstance(); - date.set(calendar1.getYear(), calendar1.getMonth() - 11, calendar1.getDay());// + date.set(calendar1.getYear(), calendar1.getMonth() - 1, calendar1.getDay());// long startTimeMills = date.getTimeInMillis();//获得起始时间戳 - date.set(calendar2.getYear(), calendar2.getMonth() - 11, calendar2.getDay());// + date.set(calendar2.getYear(), calendar2.getMonth() - 1, calendar2.getDay());// long endTimeMills = date.getTimeInMillis();//获得结束时间戳 @@ -842,26 +714,6 @@ private static int getWeekViewEndDiff(int year, int month, int day, int weekStar return week == 7 ? 6 : 7 - week - 1; } - /** - * 从月视图切换获得第一天的日期 - * - * @param position position - * @param delegate position - * @return 从月视图切换获得第一天的日期 - */ - @Deprecated - static Calendar getCalendarFromMonthViewPager(int position, CalendarViewDelegate delegate) { - Calendar calendar = new Calendar(); - calendar.setYear((position + delegate.getMinYearMonth() - 1) / 12 + delegate.getMinYear()); - calendar.setMonth((position + delegate.getMinYearMonth() - 1) % 12 + 1); - calendar.setDay(1); - calendar.setCurrentMonth(calendar.getYear() == delegate.getCurrentDay().getYear() && - calendar.getMonth() == delegate.getCurrentDay().getMonth()); - calendar.setCurrentDay(calendar.equals(delegate.getCurrentDay())); - LunarCalendar.setupLunarCalendar(calendar); - return calendar; - } - /** * 从月视图切换获得第一天的日期 diff --git a/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java b/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java index 6f16ce80..0c91c9f8 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java +++ b/calendarview/src/main/java/com/haibin/calendarview/CalendarView.java @@ -61,7 +61,7 @@ public class CalendarView extends FrameLayout { /** * 月份快速选取 */ - private YearSelectLayout mSelectLayout; + private YearViewSelectLayout mSelectLayout; /** * 星期栏 @@ -123,7 +123,7 @@ private void init(Context context) { mWeekPager.setLayoutParams(params); - mSelectLayout = (YearSelectLayout) findViewById(R.id.selectLayout); + mSelectLayout = (YearViewSelectLayout) findViewById(R.id.selectLayout); mSelectLayout.setBackgroundColor(mDelegate.getYearViewBackground()); mSelectLayout.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override diff --git a/calendarview/src/main/java/com/haibin/calendarview/CalendarViewDelegate.java b/calendarview/src/main/java/com/haibin/calendarview/CalendarViewDelegate.java index 3c41ec24..45786b36 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/CalendarViewDelegate.java +++ b/calendarview/src/main/java/com/haibin/calendarview/CalendarViewDelegate.java @@ -48,6 +48,12 @@ final class CalendarViewDelegate { */ static final int WEEK_START_WITH_SAT = 7; + + /** + * 周起始 + */ + private int mWeekStart; + /** * 全部显示 */ @@ -68,10 +74,6 @@ final class CalendarViewDelegate { private int mMonthViewShowMode; - /** - * 周起始 - */ - private int mWeekStart; /** * 默认选择模式 @@ -129,14 +131,29 @@ final class CalendarViewDelegate { * 年视图字体大小 */ private int mYearViewMonthTextSize, - mYearViewDayTextSize; + mYearViewDayTextSize, + mYearViewWeekTextSize; + + /** + * 年视图月份高度和周的高度 + */ + private int mYearViewMonthHeight, + mYearViewWeekHeight; + + /** + * 年视图一些margin和padding + */ + private int mYearViewPadding, + mYearViewMonthMarginTop, + mYearViewMonthMarginBottom; /** * 年视图字体和标记颜色 */ private int mYearViewMonthTextColor, mYearViewDayTextColor, - mYearViewSchemeTextColor; + mYearViewSchemeTextColor, + mYearViewWeekTextColor; /** * 星期栏的背景、线的背景、年份背景 @@ -181,6 +198,16 @@ final class CalendarViewDelegate { */ private Class mWeekViewClass; + /** + * 自定义年视图路径 + */ + private String mYearViewClassPath; + + /** + * 周视图类 + */ + private Class mYearViewClass; + /** * 自定义周栏路径 */ @@ -327,12 +354,15 @@ final class CalendarViewDelegate { mSchemeLunarTextColor = array.getColor(R.styleable.CalendarView_scheme_lunar_text_color, 0xFFe1e1e1); mSchemeThemeColor = array.getColor(R.styleable.CalendarView_scheme_theme_color, 0x50CFCFCF); mMonthViewClassPath = array.getString(R.styleable.CalendarView_month_view); - + mYearViewClassPath = array.getString(R.styleable.CalendarView_year_view); mWeekViewClassPath = array.getString(R.styleable.CalendarView_week_view); mWeekBarClassPath = array.getString(R.styleable.CalendarView_week_bar_view); - mWeekTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_week_text_size, CalendarUtil.dipToPx(context, 12)); - mWeekBarHeight = (int) array.getDimension(R.styleable.CalendarView_week_bar_height, CalendarUtil.dipToPx(context, 40)); - mWeekLineMargin = (int) array.getDimension(R.styleable.CalendarView_week_line_margin, CalendarUtil.dipToPx(context, 0)); + mWeekTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_week_text_size, + CalendarUtil.dipToPx(context, 12)); + mWeekBarHeight = (int) array.getDimension(R.styleable.CalendarView_week_bar_height, + CalendarUtil.dipToPx(context, 40)); + mWeekLineMargin = (int) array.getDimension(R.styleable.CalendarView_week_line_margin, + CalendarUtil.dipToPx(context, 0)); mSchemeText = array.getString(R.styleable.CalendarView_scheme_text); if (TextUtils.isEmpty(mSchemeText)) { @@ -375,16 +405,35 @@ final class CalendarViewDelegate { mMinYearDay = array.getInt(R.styleable.CalendarView_min_year_day, 1); mMaxYearDay = array.getInt(R.styleable.CalendarView_max_year_day, -1); - mDayTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_day_text_size, CalendarUtil.dipToPx(context, 16)); - mLunarTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_lunar_text_size, CalendarUtil.dipToPx(context, 10)); - mCalendarItemHeight = (int) array.getDimension(R.styleable.CalendarView_calendar_height, CalendarUtil.dipToPx(context, 56)); + mDayTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_day_text_size, + CalendarUtil.dipToPx(context, 16)); + mLunarTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_lunar_text_size, + CalendarUtil.dipToPx(context, 10)); + mCalendarItemHeight = (int) array.getDimension(R.styleable.CalendarView_calendar_height, + CalendarUtil.dipToPx(context, 56)); //年视图相关 - mYearViewMonthTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_year_view_month_text_size, CalendarUtil.dipToPx(context, 18)); - mYearViewDayTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_year_view_day_text_size, CalendarUtil.dipToPx(context, 8)); + mYearViewMonthTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_year_view_month_text_size, + CalendarUtil.dipToPx(context, 18)); + mYearViewDayTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_year_view_day_text_size, + CalendarUtil.dipToPx(context, 7)); mYearViewMonthTextColor = array.getColor(R.styleable.CalendarView_year_view_month_text_color, 0xFF111111); mYearViewDayTextColor = array.getColor(R.styleable.CalendarView_year_view_day_text_color, 0xFF111111); mYearViewSchemeTextColor = array.getColor(R.styleable.CalendarView_year_view_scheme_color, mSchemeThemeColor); + mYearViewWeekTextColor = array.getColor(R.styleable.CalendarView_year_view_week_text_color, 0xFF333333); + mYearViewWeekTextSize = array.getDimensionPixelSize(R.styleable.CalendarView_year_view_week_text_size, + CalendarUtil.dipToPx(context, 8)); + mYearViewMonthHeight = array.getDimensionPixelSize(R.styleable.CalendarView_year_view_month_height, + CalendarUtil.dipToPx(context, 32)); + mYearViewWeekHeight = array.getDimensionPixelSize(R.styleable.CalendarView_year_view_week_height, + CalendarUtil.dipToPx(context, 0)); + + mYearViewPadding = (int) array.getDimension(R.styleable.CalendarView_year_view_padding, + CalendarUtil.dipToPx(context, 6)); + mYearViewMonthMarginTop = (int) array.getDimension(R.styleable.CalendarView_year_view_month_margin_top, + CalendarUtil.dipToPx(context, 4)); + mYearViewMonthMarginBottom = (int) array.getDimension(R.styleable.CalendarView_year_view_month_margin_bottom, + CalendarUtil.dipToPx(context, 4)); if (mMinYear <= MIN_YEAR) mMinYear = 1971; if (mMaxYear >= MAX_YEAR) mMaxYear = 2055; @@ -401,8 +450,10 @@ private void init() { mCurrentDate.setCurrentDay(true); LunarCalendar.setupLunarCalendar(mCurrentDate); setRange(mMinYear, mMinYearMonth, mMaxYear, mMaxYearMonth); + + try { - if (TextUtils.isEmpty(mMonthViewClassPath)) { + if (!TextUtils.isEmpty(mWeekBarClassPath)) { mWeekBarClass = Class.forName(mWeekBarClassPath); } else { mWeekBarClass = WeekBar.class; @@ -410,6 +461,17 @@ private void init() { } catch (Exception e) { e.printStackTrace(); } + + try { + if (!TextUtils.isEmpty(mYearViewClassPath)) { + mYearViewClass = Class.forName(mYearViewClassPath); + } else { + mYearViewClass = DefaultYearView.class; + } + } catch (Exception e) { + e.printStackTrace(); + } + if (TextUtils.isEmpty(mMonthViewClassPath) || TextUtils.isEmpty(mWeekViewClassPath)) { mMonthViewClass = DefaultMonthView.class; mWeekViewClass = DefaultWeekView.class; @@ -542,6 +604,10 @@ Class getWeekBarClass() { return mWeekBarClass; } + Class getYearViewClass() { + return mYearViewClass; + } + int getWeekBarHeight() { return mWeekBarHeight; } @@ -587,6 +653,34 @@ int getYearViewMonthTextColor() { return mYearViewMonthTextColor; } + int getYearViewWeekTextSize() { + return mYearViewWeekTextSize; + } + + int getYearViewWeekTextColor() { + return mYearViewWeekTextColor; + } + + int getYearViewPadding() { + return mYearViewPadding; + } + + int getYearViewMonthMarginTop() { + return mYearViewMonthMarginTop; + } + + int getYearViewMonthMarginBottom() { + return mYearViewMonthMarginBottom; + } + + int getYearViewWeekHeight() { + return mYearViewWeekHeight; + } + + int getYearViewMonthHeight() { + return mYearViewMonthHeight; + } + int getYearViewDayTextColor() { return mYearViewDayTextColor; } diff --git a/calendarview/src/main/java/com/haibin/calendarview/DefaultYearView.java b/calendarview/src/main/java/com/haibin/calendarview/DefaultYearView.java new file mode 100644 index 00000000..7f0df25c --- /dev/null +++ b/calendarview/src/main/java/com/haibin/calendarview/DefaultYearView.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2016 huanghaibin_dev + * WebSite https://github.com/MiracleTimes-Dev + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.haibin.calendarview; + +import android.content.Context; +import android.graphics.Canvas; + +/** + * 默认年视图 + * Created by huanghaibin on 2018/10/9. + */ + +public class DefaultYearView extends YearView { + + private int mTextPadding; + + public DefaultYearView(Context context) { + super(context); + mTextPadding = CalendarUtil.dipToPx(context, 3); + } + + @Override + protected void onDrawMonth(Canvas canvas, int year, int month, int x, int y, int width, int height) { + + String text = getContext() + .getResources() + .getStringArray(R.array.month_string_array)[month - 1]; + + canvas.drawText(text, + x + mItemWidth / 2 - mTextPadding, + y + mMonthTextBaseLine, + mMonthTextPaint); + } + + @Override + protected void onDrawWeek(Canvas canvas, int week, int x, int y, int width, int height) { + String text = getContext().getResources().getStringArray(R.array.year_view_week_string_array)[week]; + canvas.drawText(text, + x + width / 2, + y + mWeekTextBaseLine, + mWeekTextPaint); + } + + + @Override + protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) { + return false; + } + + @Override + protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) { + + } + + @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; + + if (isSelected) { + canvas.drawText(String.valueOf(calendar.getDay()), + cx, + baselineY, + hasScheme ? mSchemeTextPaint : mSelectTextPaint); + } else if (hasScheme) { + canvas.drawText(String.valueOf(calendar.getDay()), + cx, + baselineY, + calendar.isCurrentDay() ? mCurDayTextPaint : + calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint); + + } else { + canvas.drawText(String.valueOf(calendar.getDay()), cx, baselineY, + calendar.isCurrentDay() ? mCurDayTextPaint : + calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint); + } + } +} diff --git a/calendarview/src/main/java/com/haibin/calendarview/MonthViewPager.java b/calendarview/src/main/java/com/haibin/calendarview/MonthViewPager.java index f093356f..338b952a 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/MonthViewPager.java +++ b/calendarview/src/main/java/com/haibin/calendarview/MonthViewPager.java @@ -303,7 +303,9 @@ void scrollToCalendar(int year, int month, int day, boolean smoothScroll) { mParentLayout.updateSelectWeek(week); } - + if (mDelegate.mCalendarSelectListener != null) { + mDelegate.mCalendarSelectListener.onCalendarSelect(calendar, false); + } if (mDelegate.mInnerListener != null) { mDelegate.mInnerListener.onMonthDateSelected(calendar, false); } diff --git a/calendarview/src/main/java/com/haibin/calendarview/YearRecyclerView.java b/calendarview/src/main/java/com/haibin/calendarview/YearRecyclerView.java index bd357c46..9421fa53 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/YearRecyclerView.java +++ b/calendarview/src/main/java/com/haibin/calendarview/YearRecyclerView.java @@ -26,7 +26,7 @@ */ public final class YearRecyclerView extends RecyclerView { private CalendarViewDelegate mDelegate; - private YearAdapter mAdapter; + private YearViewAdapter mAdapter; private OnMonthSelectedListener mListener; public YearRecyclerView(Context context) { @@ -35,7 +35,7 @@ public YearRecyclerView(Context context) { public YearRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); - mAdapter = new YearAdapter(context); + mAdapter = new YearViewAdapter(context); setLayoutManager(new GridLayoutManager(context, 3)); setAdapter(mAdapter); mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() { @@ -57,7 +57,12 @@ public void onItemClick(int position, long itemId) { }); } - void setup(CalendarViewDelegate delegate) { + /** + * 设置 + * + * @param delegate delegate + */ + final void setup(CalendarViewDelegate delegate) { this.mDelegate = delegate; this.mAdapter.setup(delegate); } @@ -103,8 +108,9 @@ final void setOnMonthSelectedListener(OnMonthSelectedListener listener) { @Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); - int h = MeasureSpec.getSize(heightSpec); - mAdapter.setItemHeight(h / 4); + int height = MeasureSpec.getSize(heightSpec); + int width = MeasureSpec.getSize(widthSpec); + mAdapter.setYearViewSize(width / 3, height / 4); } interface OnMonthSelectedListener { diff --git a/calendarview/src/main/java/com/haibin/calendarview/YearView.java b/calendarview/src/main/java/com/haibin/calendarview/YearView.java index 422f8017..33889ac2 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/YearView.java +++ b/calendarview/src/main/java/com/haibin/calendarview/YearView.java @@ -21,25 +21,145 @@ import android.graphics.Paint; import android.graphics.Rect; import android.support.annotation.Nullable; +import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; +import java.util.List; + /** - * 年份视图 - * Created by haibin on 2017/3/6. + * 年视图 + * Created by huanghaibin on 2018/10/9. */ +@SuppressWarnings("unused") +public abstract class YearView extends View { + + CalendarViewDelegate mDelegate; + + /** + * 当前月份日期的笔 + */ + protected Paint mCurMonthTextPaint = new Paint(); + + /** + * 其它月份日期颜色 + */ + protected Paint mOtherMonthTextPaint = new Paint(); + + /** + * 当前月份农历文本颜色 + */ + protected Paint mCurMonthLunarTextPaint = new Paint(); + + /** + * 当前月份农历文本颜色 + */ + protected Paint mSelectedLunarTextPaint = new Paint(); + + /** + * 其它月份农历文本颜色 + */ + protected Paint mOtherMonthLunarTextPaint = new Paint(); + + /** + * 其它月份农历文本颜色 + */ + protected Paint mSchemeLunarTextPaint = new Paint(); + + /** + * 标记的日期背景颜色画笔 + */ + protected Paint mSchemePaint = new Paint(); + + /** + * 被选择的日期背景色 + */ + protected Paint mSelectedPaint = new Paint(); + + /** + * 标记的文本画笔 + */ + protected Paint mSchemeTextPaint = new Paint(); + + /** + * 选中的文本画笔 + */ + protected Paint mSelectTextPaint = new Paint(); + + /** + * 当前日期文本颜色画笔 + */ + protected Paint mCurDayTextPaint = new Paint(); + + /** + * 当前日期文本颜色画笔 + */ + protected Paint mCurDayLunarTextPaint = new Paint(); + + /** + * 月份画笔 + */ + protected Paint mMonthTextPaint = new Paint(); + + /** + * 周栏画笔 + */ + protected Paint mWeekTextPaint = new Paint(); + + /** + * 日历项 + */ + List mItems; + + /** + * 每一项的高度 + */ + protected int mItemHeight; + + /** + * 每一项的宽度 + */ + protected int mItemWidth; -public class YearView extends View { + /** + * Text的基线 + */ + protected float mTextBaseLine; - private int mDiff;//第一天偏离周日多少天 - private int mCount;//总数 - private int mLastCount;//最后一行的天数 - private int mLine;//多少行 - private Paint mPaint = new Paint(); - private Paint mSchemePaint = new Paint(); - private Calendar mCalendar; - private int mMinHeight;//最小高度 - private CalendarViewDelegate mDelegate; + /** + * Text的基线 + */ + protected float mMonthTextBaseLine; + + /** + * Text的基线 + */ + protected float mWeekTextBaseLine; + + /** + * 当前日历卡年份 + */ + protected int mYear; + + /** + * 当前日历卡月份 + */ + protected int mMonth; + + /** + * 下个月偏移的数量 + */ + protected int mNextDiff; + + /** + * 周起始 + */ + protected int mWeekStart; + + /** + * 日历的行数 + */ + protected int mLineCount; public YearView(Context context) { this(context, null); @@ -47,140 +167,365 @@ public YearView(Context context) { public YearView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); - mPaint.setAntiAlias(true); - mPaint.setTextAlign(Paint.Align.CENTER); + initPaint(); + } + + + /** + * 初始化配置 + */ + private void initPaint() { + mCurMonthTextPaint.setAntiAlias(true); + mCurMonthTextPaint.setTextAlign(Paint.Align.CENTER); + mCurMonthTextPaint.setColor(0xFF111111); + mCurMonthTextPaint.setFakeBoldText(true); + + mOtherMonthTextPaint.setAntiAlias(true); + mOtherMonthTextPaint.setTextAlign(Paint.Align.CENTER); + mOtherMonthTextPaint.setColor(0xFFe1e1e1); + mOtherMonthTextPaint.setFakeBoldText(true); + + mCurMonthLunarTextPaint.setAntiAlias(true); + mCurMonthLunarTextPaint.setTextAlign(Paint.Align.CENTER); + + mSelectedLunarTextPaint.setAntiAlias(true); + mSelectedLunarTextPaint.setTextAlign(Paint.Align.CENTER); + + mOtherMonthLunarTextPaint.setAntiAlias(true); + mOtherMonthLunarTextPaint.setTextAlign(Paint.Align.CENTER); + + mMonthTextPaint.setAntiAlias(true); + mMonthTextPaint.setFakeBoldText(true); + + mWeekTextPaint.setAntiAlias(true); + mWeekTextPaint.setFakeBoldText(true); + mWeekTextPaint.setTextAlign(Paint.Align.CENTER); + + mSchemeLunarTextPaint.setAntiAlias(true); + mSchemeLunarTextPaint.setTextAlign(Paint.Align.CENTER); + + mSchemeTextPaint.setAntiAlias(true); + mSchemeTextPaint.setStyle(Paint.Style.FILL); + mSchemeTextPaint.setTextAlign(Paint.Align.CENTER); + mSchemeTextPaint.setColor(0xffed5353); + mSchemeTextPaint.setFakeBoldText(true); + + mSelectTextPaint.setAntiAlias(true); + mSelectTextPaint.setStyle(Paint.Style.FILL); + mSelectTextPaint.setTextAlign(Paint.Align.CENTER); + mSelectTextPaint.setColor(0xffed5353); + mSelectTextPaint.setFakeBoldText(true); + mSchemePaint.setAntiAlias(true); - mSchemePaint.setTextAlign(Paint.Align.CENTER); - measureLine(); + mSchemePaint.setStyle(Paint.Style.FILL); + mSchemePaint.setStrokeWidth(2); + mSchemePaint.setColor(0xffefefef); + + mCurDayTextPaint.setAntiAlias(true); + mCurDayTextPaint.setTextAlign(Paint.Align.CENTER); + mCurDayTextPaint.setColor(Color.RED); + mCurDayTextPaint.setFakeBoldText(true); + + mCurDayLunarTextPaint.setAntiAlias(true); + mCurDayLunarTextPaint.setTextAlign(Paint.Align.CENTER); + mCurDayLunarTextPaint.setColor(Color.RED); + mCurDayLunarTextPaint.setFakeBoldText(true); + + mSelectedPaint.setAntiAlias(true); + mSelectedPaint.setStyle(Paint.Style.FILL); + mSelectedPaint.setStrokeWidth(2); } /** - * 初始化 + * 设置 * * @param delegate delegate */ - void setup(CalendarViewDelegate delegate) { + final void setup(CalendarViewDelegate delegate) { this.mDelegate = delegate; - mSchemePaint.setColor(delegate.getYearViewSchemeTextColor()); - mSchemePaint.setTextSize(delegate.getYearViewDayTextSize()); - mPaint.setTextSize(delegate.getYearViewDayTextSize()); - mPaint.setColor(delegate.getYearViewDayTextColor()); + this.mCurMonthTextPaint.setTextSize(delegate.getYearViewDayTextSize()); + this.mSchemeTextPaint.setTextSize(delegate.getYearViewDayTextSize()); + this.mOtherMonthTextPaint.setTextSize(delegate.getYearViewDayTextSize()); + this.mCurDayTextPaint.setTextSize(delegate.getYearViewDayTextSize()); + this.mSelectTextPaint.setTextSize(delegate.getYearViewDayTextSize()); + + this.mSchemeTextPaint.setColor(delegate.getYearViewSchemeTextColor()); + this.mCurMonthTextPaint.setColor(delegate.getYearViewDayTextColor()); + this.mOtherMonthTextPaint.setColor(delegate.getYearViewDayTextColor()); + this.mCurDayTextPaint.setColor(delegate.getYearViewDayTextColor()); + this.mSelectTextPaint.setColor(delegate.getYearViewDayTextColor()); + this.mMonthTextPaint.setTextSize(delegate.getYearViewMonthTextSize()); + this.mMonthTextPaint.setColor(delegate.getYearViewMonthTextColor()); + this.mWeekTextPaint.setColor(delegate.getYearViewWeekTextColor()); + this.mWeekTextPaint.setTextSize(delegate.getYearViewWeekTextSize()); + } + + /** + * 初始化年视图 + * @param year year + * @param month month + */ + final void init(int year, int month) { + mYear = year; + mMonth = month; + mNextDiff = CalendarUtil.getMonthEndDiff(mYear, mMonth, mDelegate.getWeekStart()); + int preDiff = CalendarUtil.getMonthViewStartDiff(mYear, mMonth, mDelegate.getWeekStart()); + + mItems = CalendarUtil.initCalendarForMonthView(mYear, mMonth, mDelegate.getCurrentDay(), mDelegate.getWeekStart()); + + mLineCount = 6; + addSchemesFromMap(); + + } + + /** + * 测量大小 + * + * @param width width + * @param height height + */ + final void measureSize(int width, int height) { + Rect rect = new Rect(); - mPaint.getTextBounds("1", 0, 1, rect); + mCurMonthTextPaint.getTextBounds("1", 0, 1, rect); int textHeight = rect.height(); - mMinHeight = 12 * textHeight; + int mMinHeight = 12 * textHeight + getMonthViewTop(); + + int h = height >= mMinHeight ? height : mMinHeight; + + getLayoutParams().width = width; + getLayoutParams().height = h; + mItemHeight = (h - getMonthViewTop()) / 6; + + Paint.FontMetrics metrics = mCurMonthTextPaint.getFontMetrics(); + mTextBaseLine = mItemHeight / 2 - metrics.descent + (metrics.bottom - metrics.top) / 2; + + Paint.FontMetrics monthMetrics = mMonthTextPaint.getFontMetrics(); + mMonthTextBaseLine = mDelegate.getYearViewMonthHeight() / 2 - monthMetrics.descent + + (monthMetrics.bottom - monthMetrics.top) / 2; + + Paint.FontMetrics weekMetrics = mWeekTextPaint.getFontMetrics(); + mWeekTextBaseLine = mDelegate.getYearViewWeekHeight() / 2 - weekMetrics.descent + + (weekMetrics.bottom - weekMetrics.top) / 2; + + invalidate(); } + /** + * 添加事件标记,来自Map + */ + private void addSchemesFromMap() { + if (mDelegate.mSchemeDatesMap == null || mDelegate.mSchemeDatesMap.size() == 0) { + return; + } + for (Calendar a : mItems) { + if (mDelegate.mSchemeDatesMap.containsKey(a.toString())) { + Calendar d = mDelegate.mSchemeDatesMap.get(a.toString()); + a.setScheme(TextUtils.isEmpty(d.getScheme()) ? mDelegate.getSchemeText() : d.getScheme()); + a.setSchemeColor(d.getSchemeColor()); + a.setSchemes(d.getSchemes()); + } else { + a.setScheme(""); + a.setSchemeColor(0); + a.setSchemes(null); + } + } + } @Override protected void onDraw(Canvas canvas) { - int width = getWidth(); - int height = getHeight(); - int pLeft = getPaddingLeft(); - int w = (width - getPaddingLeft() - getPaddingRight()) / 7; - int h = (height - getPaddingTop() - getPaddingBottom()) / 6; + mItemWidth = (getWidth() - 2 * mDelegate.getYearViewPadding()) / 7; + onPreviewHook(); + onDrawMonth(canvas); + onDrawWeek(canvas); + onDrawMonthView(canvas); + } + + /** + * 绘制 + * + * @param canvas canvas + */ + private void onDrawMonth(Canvas canvas) { + onDrawMonth(canvas, + mYear, mMonth, + mDelegate.getYearViewPadding(), + mDelegate.getYearViewMonthMarginTop(), + getWidth() - 2 * mDelegate.getYearViewPadding(), + mDelegate.getYearViewMonthHeight() + + mDelegate.getYearViewMonthMarginTop()); + } + + private int getMonthViewTop() { + return mDelegate.getYearViewMonthMarginTop() + + mDelegate.getYearViewMonthHeight() + + mDelegate.getYearViewMonthMarginBottom() + + mDelegate.getYearViewWeekHeight(); + } + + /** + * 绘制 + * + * @param canvas canvas + */ + private void onDrawWeek(Canvas canvas) { + if (mDelegate.getYearViewWeekHeight() <= 0) { + return; + } + int week = mDelegate.getWeekStart(); + if(week > 0){ + week-=1; + } + int width = (getWidth() - 2 * mDelegate.getYearViewPadding()) / 7; + for (int i = 0; i < 7; i++) { + onDrawWeek(canvas, + week, + mDelegate.getYearViewPadding() + i * width, + mDelegate.getYearViewMonthHeight() + + mDelegate.getYearViewMonthMarginTop() + + mDelegate.getYearViewMonthMarginBottom(), + width, + mDelegate.getYearViewWeekHeight()); + week += 1; + if (week >= 7) { + week = 0; + } + + } + } + + /** + * 绘制月份数据 + * + * @param canvas canvas + */ + private void onDrawMonthView(Canvas canvas) { + + int count = mLineCount * 7; int d = 0; - for (int i = 0; i < mLine; i++) { - if (i == 0) {//第一行 - for (int j = 0; j < (7 - mDiff); j++) { - ++d; - canvas.drawText(String.valueOf(j + 1), mDiff * w + j * w + pLeft + w / 2, h, isScheme(d) ? mSchemePaint : mPaint); - } - } else if (i == mLine - 1 && mLastCount != 0) { - int first = mCount - mLastCount + 1; - for (int j = 0; j < mLastCount; j++) { - ++d; - canvas.drawText(String.valueOf(first), j * w + pLeft + w / 2, (i + 1) * h, isScheme(d) ? mSchemePaint : mPaint); - ++first; + for (int i = 0; i < mLineCount; i++) { + for (int j = 0; j < 7; j++) { + Calendar calendar = mItems.get(d); + if (d > mItems.size() - mNextDiff) { + return; } - } else { - int first = i * 7 - mDiff + 1; - for (int j = 0; j < 7; j++) { + if (!calendar.isCurrentMonth()) { ++d; - canvas.drawText(String.valueOf(first), j * w + pLeft + w / 2, (i + 1) * h, isScheme(d) ? mSchemePaint : mPaint); - ++first; + continue; } + draw(canvas, calendar, i, j, d); + ++d; } } } + /** - * 计算行数 + * 开始绘制 + * + * @param canvas canvas + * @param calendar 对应日历 + * @param i i + * @param j j + * @param d d */ - private void measureLine() { - int offset = mCount - (7 - mDiff); - mLine = 1 + (offset % 7 == 0 ? 0 : 1) + offset / 7; - mLastCount = offset % 7; + private void draw(Canvas canvas, Calendar calendar, int i, int j, int d) { + int x = j * mItemWidth + mDelegate.getYearViewPadding(); + int y = i * mItemHeight + getMonthViewTop(); + + boolean isSelected = calendar.equals(mDelegate.mSelectedCalendar); + boolean hasScheme = calendar.hasScheme(); + + if (hasScheme) { + //标记的日子 + boolean isDrawSelected = false;//是否继续绘制选中的onDrawScheme + if (isSelected) { + isDrawSelected = onDrawSelected(canvas, calendar, x, y, true); + } + if (isDrawSelected || !isSelected) { + //将画笔设置为标记颜色 + mSchemePaint.setColor(calendar.getSchemeColor() != 0 ? calendar.getSchemeColor() : mDelegate.getSchemeThemeColor()); + onDrawScheme(canvas, calendar, x, y); + } + } else { + if (isSelected) { + onDrawSelected(canvas, calendar, x, y, false); + } + } + onDrawText(canvas, calendar, x, y, hasScheme, isSelected); } /** - * 初始化月份卡 - * - * @param mDiff 偏离天数 - * @param mCount 当月总天数 - * @param mYear 哪一年 - * @param mMonth 哪一月 - */ - void init(int mDiff, int mCount, int mYear, int mMonth) { - this.mDiff = mDiff; - this.mCount = mCount; - mCalendar = new Calendar(); - mCalendar.setYear(mYear); - mCalendar.setMonth(mMonth); - measureLine(); - invalidate(); + * 开始绘制前的钩子,这里做一些初始化的操作,每次绘制只调用一次,性能高效 + * 没有需要可忽略不实现 + * 例如: + * 1、需要绘制圆形标记事件背景,可以在这里计算半径 + * 2、绘制矩形选中效果,也可以在这里计算矩形宽和高 + */ + protected void onPreviewHook() { + // TODO: 2017/11/16 } + /** + * 绘制月份 + * + * @param canvas canvas + * @param year year + * @param month month + * @param x x + * @param y y + * @param width width + * @param height height + */ + protected abstract void onDrawMonth(Canvas canvas, int year, int month, int x, int y, int width, int height); + /** - * 设置标记颜色 + * 绘制年视图的周栏 * - * @param schemeColor schemeColor + * @param canvas canvas + * @param week week + * @param x x + * @param y y + * @param width width + * @param height height */ - void setSchemeColor(int schemeColor) { - if (schemeColor != 0) - mSchemePaint.setColor(schemeColor); - if (schemeColor == 0xff30393E) - mSchemePaint.setColor(Color.RED); - } + protected abstract void onDrawWeek(Canvas canvas, int week, int x, int y, int width, int height); + /** - * 设置字体 + * 绘制选中的日期 * - * @param textSize textSize - * @param textColor textColor + * @param canvas canvas + * @param calendar 日历日历calendar + * @param x 日历Card x起点坐标 + * @param y 日历Card y起点坐标 + * @param hasScheme hasScheme 非标记的日期 + * @return 是否绘制onDrawScheme,true or false */ - void setTextStyle(int textSize, int textColor) { - mSchemePaint.setTextSize(textSize); - mPaint.setTextSize(textSize); - mPaint.setColor(textColor); - } + protected abstract boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme); /** - * 测量高度 + * 绘制标记的日期,这里可以是背景色,标记色什么的 * - * @param height height + * @param canvas canvas + * @param calendar 日历calendar + * @param x 日历Card x起点坐标 + * @param y 日历Card y起点坐标 */ - void measureHeight(int height) { - if (height <= mMinHeight) { - getLayoutParams().height = mMinHeight; - } else { - getLayoutParams().height = height; - } - } + protected abstract void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y); + /** - * 是否包含事件 + * 绘制日历文本 * - * @param day day - * @return return + * @param canvas canvas + * @param calendar 日历calendar + * @param x 日历Card x起点坐标 + * @param y 日历Card y起点坐标 + * @param hasScheme 是否是标记的日期 + * @param isSelected 是否选中 */ - private boolean isScheme(int day) { - if (mDelegate.mSchemeDatesMap == null || mDelegate.mSchemeDatesMap.size() == 0) { - return false; - } - mCalendar.setDay(day); - return mDelegate.mSchemeDatesMap.containsKey(mCalendar.toString()); - } + protected abstract void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected); } diff --git a/calendarview/src/main/java/com/haibin/calendarview/YearAdapter.java b/calendarview/src/main/java/com/haibin/calendarview/YearViewAdapter.java similarity index 53% rename from calendarview/src/main/java/com/haibin/calendarview/YearAdapter.java rename to calendarview/src/main/java/com/haibin/calendarview/YearViewAdapter.java index 523a6c70..535cd3a2 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/YearAdapter.java +++ b/calendarview/src/main/java/com/haibin/calendarview/YearViewAdapter.java @@ -17,57 +17,60 @@ import android.content.Context; import android.support.v7.widget.RecyclerView; -import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; -final class YearAdapter extends BaseRecyclerAdapter { +import java.lang.reflect.Constructor; + +final class YearViewAdapter extends BaseRecyclerAdapter { private CalendarViewDelegate mDelegate; - private int mItemHeight; - private int mTextHeight; + private int mItemWidth, mItemHeight; - YearAdapter(Context context) { + YearViewAdapter(Context context) { super(context); - mTextHeight = CalendarUtil.dipToPx(context, 52); } - void setup(CalendarViewDelegate delegate) { + final void setup(CalendarViewDelegate delegate) { this.mDelegate = delegate; } - void setItemHeight(int itemHeight) { - this.mItemHeight = itemHeight; + + final void setYearViewSize(int width, int height) { + this.mItemWidth = width; + this.mItemHeight = height; } @Override RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) { - return new YearViewHolder(mInflater.inflate(R.layout.cv_item_list_year, parent, false), mDelegate); + YearView yearView; + try { + Constructor constructor = mDelegate.getYearViewClass().getConstructor(Context.class); + yearView = (YearView) constructor.newInstance(mContext); + RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, + RecyclerView.LayoutParams.MATCH_PARENT); + yearView.setLayoutParams(params); + } catch (Exception e) { + e.printStackTrace(); + yearView = null; + } + return new YearViewHolder(yearView, mDelegate); } @Override void onBindViewHolder(RecyclerView.ViewHolder holder, Month item, int position) { YearViewHolder h = (YearViewHolder) holder; YearView view = h.mYearView; - view.setSchemeColor(mDelegate.getYearViewSchemeTextColor()); - view.setTextStyle(mDelegate.getYearViewDayTextSize(), - mDelegate.getYearViewDayTextColor()); - view.init(item.getDiff(), item.getCount(), item.getYear(), item.getMonth()); - view.measureHeight(mItemHeight - mTextHeight); - h.mTextMonth.setText(mContext.getResources().getStringArray(R.array.month_string_array)[item.getMonth() - 1]); - h.mTextMonth.setTextSize(TypedValue.COMPLEX_UNIT_PX, mDelegate.getYearViewMonthTextSize()); - h.mTextMonth.setTextColor(mDelegate.getYearViewMonthTextColor()); + view.init(item.getYear(), item.getMonth()); + view.measureSize(mItemWidth, mItemHeight); } private static class YearViewHolder extends RecyclerView.ViewHolder { YearView mYearView; - TextView mTextMonth; YearViewHolder(View itemView, CalendarViewDelegate delegate) { super(itemView); - mYearView = (YearView) itemView.findViewById(R.id.selectView); + mYearView = (YearView) itemView; mYearView.setup(delegate); - mTextMonth = (TextView) itemView.findViewById(R.id.tv_month); } } } diff --git a/calendarview/src/main/java/com/haibin/calendarview/YearSelectLayout.java b/calendarview/src/main/java/com/haibin/calendarview/YearViewSelectLayout.java similarity index 93% rename from calendarview/src/main/java/com/haibin/calendarview/YearSelectLayout.java rename to calendarview/src/main/java/com/haibin/calendarview/YearViewSelectLayout.java index 8ab33665..ad0b4d56 100644 --- a/calendarview/src/main/java/com/haibin/calendarview/YearSelectLayout.java +++ b/calendarview/src/main/java/com/haibin/calendarview/YearViewSelectLayout.java @@ -30,17 +30,17 @@ * 年份+月份选择布局 * ViewPager + RecyclerView */ -public final class YearSelectLayout extends ViewPager { +public final class YearViewSelectLayout extends ViewPager { private int mYearCount; private boolean isUpdateYearView; private CalendarViewDelegate mDelegate; private YearRecyclerView.OnMonthSelectedListener mListener; - public YearSelectLayout(Context context) { + public YearViewSelectLayout(Context context) { this(context, null); } - public YearSelectLayout(Context context, AttributeSet attrs) { + public YearViewSelectLayout(Context context, AttributeSet attrs) { super(context, attrs); } @@ -104,7 +104,7 @@ void scrollToYear(int year, boolean smoothScroll) { /** * 更新日期范围 */ - void updateRange() { + final void updateRange() { isUpdateYearView = true; notifyDataSetChanged(); isUpdateYearView = false; @@ -113,7 +113,7 @@ void updateRange() { /** * 更新界面 */ - void update() { + final void update() { for (int i = 0; i < getChildCount(); i++) { YearRecyclerView view = (YearRecyclerView) getChildAt(i); view.getAdapter().notifyDataSetChanged(); @@ -124,7 +124,7 @@ void update() { /** * 更新周起始 */ - void updateWeekStart() { + final void updateWeekStart() { for (int i = 0; i < getChildCount(); i++) { YearRecyclerView view = (YearRecyclerView) getChildAt(i); view.updateWeekStart(); @@ -132,7 +132,7 @@ void updateWeekStart() { } } - public void setOnMonthSelectedListener(YearRecyclerView.OnMonthSelectedListener listener) { + final void setOnMonthSelectedListener(YearRecyclerView.OnMonthSelectedListener listener) { this.mListener = listener; } diff --git a/calendarview/src/main/res/layout/cv_item_list_year.xml b/calendarview/src/main/res/layout/cv_item_list_year.xml deleted file mode 100644 index e4092209..00000000 --- a/calendarview/src/main/res/layout/cv_item_list_year.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/calendarview/src/main/res/layout/cv_layout_calendar_view.xml b/calendarview/src/main/res/layout/cv_layout_calendar_view.xml index c540a40e..5ff98046 100644 --- a/calendarview/src/main/res/layout/cv_layout_calendar_view.xml +++ b/calendarview/src/main/res/layout/cv_layout_calendar_view.xml @@ -36,7 +36,7 @@ - 六 + + + + + + + + + + 1月 2月 diff --git a/calendarview/src/main/res/values/attrs.xml b/calendarview/src/main/res/values/attrs.xml index 33bd8a84..bfd51f1a 100644 --- a/calendarview/src/main/res/values/attrs.xml +++ b/calendarview/src/main/res/values/attrs.xml @@ -11,8 +11,8 @@ - + @@ -44,6 +44,13 @@ + + + + + + + diff --git a/calendarview/src/main/res/values/strings.xml b/calendarview/src/main/res/values/strings.xml index 180a21e9..200a663e 100644 --- a/calendarview/src/main/res/values/strings.xml +++ b/calendarview/src/main/res/values/strings.xml @@ -130,6 +130,15 @@ Sat + + S + M + T + W + T + F + S + JAN diff --git a/gradle.properties b/gradle.properties index 03026226..21c24c30 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,8 +18,8 @@ org.gradle.jvmargs=-Xmx1536m POM_BINTRAY_NAME=com.haibin:calendarview POM_DESCRIPTION=A very good CalendarView for Android. POM_ARTIFACT_ID=calendarview -VERSION_NAME=3.4.7 -VERSION_CODE=3.4.7 +VERSION_NAME=3.4.8 +VERSION_CODE=3.4.8 POM_BINTRAY_PRPO=maven POM_PACKAGING=aar POM_NAME=calendarview