Skip to content

Commit

Permalink
支持选中背景为drawable
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Mar 23, 2020
1 parent 51a85c3 commit 057cf4b
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 67 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
apply plugin: 'com.android.application'

android {

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}


compileSdkVersion 28
defaultConfig {
applicationId "com.necer.ncalendar"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void onBindCurrentMonthOrWeekView(View view, LocalDate localDate, List<Lo
setLunar(view, localDate);

if (selectedDateList.contains(localDate)) {
ll_content.setBackgroundResource(R.drawable.bg_checked);
ll_content.setBackgroundResource(R.drawable.bg_checked_today);
} else {
ll_content.setBackgroundResource(R.drawable.bg_unchecked);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.necer.ncalendar.R;
import com.necer.ncalendar.TestActivity;

import java.time.LocalDate;


public class MainActivity extends AppCompatActivity {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.necer.ncalendar.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_miui10.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
app:selectCircleColor="#398FE9"
app:solarHolidayTextColor="#519EDC"
app:solarTermTextColor="#519EDC"
app:checkedBackground="@drawable/bg_checked_ding"
app:solarTextColor="@color/colorAccent"
app:todaySolarTextColor="#398FE9">

Expand Down
7 changes: 6 additions & 1 deletion ncalendar/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'


android {

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

compileSdkVersion 28
defaultConfig {
minSdkVersion 15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public CalendarAdapter getCalendarAdapter() {
@Override
public CalendarPainter getCalendarPainter() {
if (mCalendarPainter == null) {
mCalendarPainter = new InnerPainter(this);
mCalendarPainter = new InnerPainter(getContext(),this);
}
return mCalendarPainter;
}
Expand Down
2 changes: 1 addition & 1 deletion ncalendar/src/main/java/com/necer/calendar/NCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public NCalendar(@NonNull Context context, @Nullable AttributeSet attrs, int def
monthCalendar.setId(R.id.N_monthCalendar);
weekCalendar.setId(R.id.N_weekCalendar);

setCalendarPainter(new InnerPainter(this));
setCalendarPainter(new InnerPainter(getContext(),this));

monthCalendar.setOnMWDateChangeListener(onMWDateChangeListener);
weekCalendar.setOnMWDateChangeListener(onMWDateChangeListener);
Expand Down
67 changes: 35 additions & 32 deletions ncalendar/src/main/java/com/necer/painter/CalendarPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.graphics.Canvas;
import android.graphics.RectF;

import com.necer.calendar.BaseCalendar;
import com.necer.view.CalendarView;
import com.necer.view.ICalendarView;

import org.joda.time.LocalDate;
Expand All @@ -16,57 +14,62 @@
*/
public interface CalendarPainter {

/**
* 绘制月日历或这日历背景,如数字背景等
*
* @param iCalendarView ICalendarView 日历页面,可判断是月日历或者周日历
* @param canvas
* @param rectF
* @param localDate
* @param totalDistance 滑动的全部距离
* @param currentDistance 当前位置的距离
*/
void onDrawCalendarBackground(ICalendarView iCalendarView, Canvas canvas, RectF rectF, LocalDate localDate, int totalDistance, int currentDistance);

/**
* 绘制今天的日期
*
* @param canvas
* @param rectF
* @param localDate
* @param selectedDateList 全部选中的日期集合
* @param canvas 画布
* @param rectF 当前日期得位置矩形
* @param localDate 当前日期
* @param checkedDateList 全部选中的日期集合
*/
void onDrawToday(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList);
void onDrawToday(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> checkedDateList);

/**
* 绘制当前月或周的日期
*
* @param canvas
* @param rectF
* @param localDate
* @param selectedDateList 全部选中的日期集合
* @param canvas 画布
* @param rectF 当前日期得位置矩形
* @param localDate 当前日期
* @param checkedDateList 全部选中的日期集合
*/
void onDrawCurrentMonthOrWeek(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList);
void onDrawCurrentMonthOrWeek(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> checkedDateList);

/**
* 绘制上一月,下一月的日期,周日历不用实现
*
* @param canvas
* @param rectF
* @param localDate
* @param selectedDateList 全部选中的日期集合
* @param canvas 画布
* @param rectF 当前日期得位置矩形
* @param localDate 当前日期
* @param checkedDateList 全部选中的日期集合
*/
void onDrawLastOrNextMonth(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList);
void onDrawLastOrNextMonth(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> checkedDateList);

/**
* 绘制不可用的日期,和方法setDateInterval(startFormatDate, endFormatDate)对应,
* 如果没有使用setDateInterval设置日期范围 此方法不用实现
*
* @param canvas
* @param rectF
* @param localDate
* @param canvas 画布
* @param rectF 当前日期得位置矩形
* @param localDate 当前日期
*/
void onDrawDisableDate(Canvas canvas, RectF rectF, LocalDate localDate);
default void onDrawDisableDate(Canvas canvas, RectF rectF, LocalDate localDate) {

}

/**
* 绘制月日历或这日历背景,如数字背景等
*
* @param iCalendarView ICalendarView 日历页面,可判断是月日历或者周日历
* @param canvas 画布
* @param rectF 当前日历的背景得位置矩形
* @param localDate 当前日历的中心日期
* @param totalDistance 滑动的全部距离
* @param currentDistance 当前位置的距离
*/
default void onDrawCalendarBackground(ICalendarView iCalendarView, Canvas canvas, RectF rectF, LocalDate localDate, int totalDistance, int currentDistance) {

}


}
40 changes: 29 additions & 11 deletions ncalendar/src/main/java/com/necer/painter/InnerPainter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.necer.painter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import com.necer.R;
import com.necer.calendar.ICalendar;
import com.necer.entity.CalendarDate;
import com.necer.enumeration.CalendarType;
import com.necer.utils.Attrs;
import com.necer.utils.CalendarUtil;
import com.necer.view.CalendarView;
import com.necer.view.ICalendarView;

import org.joda.time.LocalDate;
Expand All @@ -26,13 +29,13 @@
public class InnerPainter implements CalendarPainter {

private Attrs mAttrs;
protected Paint mTextPaint;
protected Paint mCirclePaint;
private Paint mTextPaint;
private Paint mCirclePaint;

private int noAlphaColor = 255;

protected List<LocalDate> mHolidayList;
protected List<LocalDate> mWorkdayList;
private List<LocalDate> mHolidayList;
private List<LocalDate> mWorkdayList;

private List<LocalDate> mPointList;
private Map<LocalDate, String> mReplaceLunarStrMap;
Expand All @@ -41,7 +44,10 @@ public class InnerPainter implements CalendarPainter {

private ICalendar mCalendar;

public InnerPainter(ICalendar calendar) {
private Drawable mCheckedBackground;
private Drawable mTodayCheckedBackground;

public InnerPainter(Context context, ICalendar calendar) {
this.mAttrs = calendar.getAttrs();
this.mCalendar = calendar;
mTextPaint = getPaint();
Expand All @@ -53,6 +59,9 @@ public InnerPainter(ICalendar calendar) {
mReplaceLunarColorMap = new HashMap<>();
mStretchStrMap = new HashMap<>();

mCheckedBackground = context.getResources().getDrawable(mAttrs.checkedBackground);
mTodayCheckedBackground = context.getResources().getDrawable(mAttrs.todayCheckedBackground);

List<String> holidayList = CalendarUtil.getHolidayList();
for (int i = 0; i < holidayList.size(); i++) {
mHolidayList.add(new LocalDate(holidayList.get(i)));
Expand Down Expand Up @@ -115,6 +124,7 @@ public void onDrawCurrentMonthOrWeek(Canvas canvas, RectF rectF, LocalDate local
drawHolidays(canvas, rectF, false, noAlphaColor, localDate);
}
drawStretchText(canvas, rectF, noAlphaColor, localDate);

}

@Override
Expand Down Expand Up @@ -144,13 +154,21 @@ public void onDrawDisableDate(Canvas canvas, RectF rectF, LocalDate localDate) {
}





//选中背景
private void drawSelectBg(Canvas canvas, RectF rectF, int alphaColor, boolean isToday) {
mCirclePaint.setStyle(isToday ? Paint.Style.FILL_AND_STROKE : Paint.Style.STROKE);
mCirclePaint.setStrokeWidth(mAttrs.hollowCircleStroke);
mCirclePaint.setColor(isToday ? mAttrs.selectCircleColor : mAttrs.hollowCircleColor);
mCirclePaint.setAlpha(alphaColor);
canvas.drawCircle(rectF.centerX(), rectF.centerY(), mAttrs.selectCircleRadius, mCirclePaint);
// mCirclePaint.setStyle(isToday ? Paint.Style.FILL_AND_STROKE : Paint.Style.STROKE);
// mCirclePaint.setStrokeWidth(mAttrs.hollowCircleStroke);
//// mCirclePaint.setColor(isToday ? mAttrs.selectCircleColor : mAttrs.hollowCircleColor);
//// mCirclePaint.setAlpha(alphaColor);

Rect rect = new Rect((int) (rectF.centerX() - mCheckedBackground.getIntrinsicWidth() / 2), (int) (rectF.centerY() - mCheckedBackground.getIntrinsicHeight() / 2), (int) (rectF.centerX() + mCheckedBackground.getIntrinsicWidth() / 2), (int) (rectF.centerY() + mCheckedBackground.getIntrinsicHeight() / 2));
mCheckedBackground.setBounds(rect);
mCheckedBackground.draw(canvas);

// canvas.drawCircle(rectF.centerX(), rectF.centerY(), mAttrs.selectCircleRadius, mCirclePaint);
}


Expand Down
7 changes: 6 additions & 1 deletion ncalendar/src/main/java/com/necer/utils/Attrs.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.necer.utils;

import android.graphics.drawable.Drawable;

/**
* Created by necer on 2017/6/13.
*/
Expand Down Expand Up @@ -36,7 +38,6 @@ public class Attrs {
public float solarTextSize;
public float lunarTextSize;
public float lunarDistance;//农历到文字中心的距离
public float selectCircleRadius;
public boolean isShowLunar;


Expand Down Expand Up @@ -84,4 +85,8 @@ public class Attrs {
public boolean isLastNextMonthClickEnable;//月日历上下月是否可点击


public int todayCheckedBackground;//选中当天的checkedBackground
public int checkedBackground;//选中其他日期的checkedBackground


}
Loading

0 comments on commit 057cf4b

Please sign in to comment.