Skip to content

Commit

Permalink
完善
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Jul 10, 2019
1 parent f94558a commit 4dd7ef9
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 71 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion 15
targetSdkVersion 27
versionCode 2
versionName "4.1.1"
versionName "4.1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.necer.listener.OnCalendarChangedListener;
import com.necer.listener.OnCalendarMultipleChangedListener;
import com.necer.ncalendar.R;
import com.necer.ncalendar.painter.LigaturePainter;
import com.necer.painter.InnerPainter;
import com.necer.utils.CalendarUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
monthCalendar = findViewById(R.id.monthCalendar);
monthCalendar.setSelectedMode(selectedModel);


monthCalendar.setOnCalendarChangedListener(new OnCalendarChangedListener() {
@Override
public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, LocalDate localDate) {
Expand Down
32 changes: 29 additions & 3 deletions app/src/main/java/com/necer/ncalendar/painter/LigaturePainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@ public class LigaturePainter implements CalendarPainter {

protected Paint mTextPaint;
protected Paint mBgPaint;
private int mCircleRadius;
private float mCircleRadius;
private Context mContext;

public LigaturePainter(Context context) {
mContext = context;
mTextPaint = getPaint();
mBgPaint = getPaint();


mCircleRadius = CalendarUtil.dp2px(context, 20);
mBgPaint.setColor(Color.parseColor("#ff7575"));
mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mCircleRadius = (int) CalendarUtil.dp2px(context, 20);
}

private Paint getPaint() {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setTextAlign(Paint.Align.CENTER);
return paint;
}
Expand Down Expand Up @@ -80,21 +81,46 @@ private void drawSelectBg(Canvas canvas, RectF rectF, LocalDate localDate, boole
if (selectedDateList.contains(lastLocalDate) && selectedDateList.contains(nextLocalDate) && CalendarUtil.isEqualsMonth(lastLocalDate, nextLocalDate)) {
//画全整个矩形
RectF rectF1 = new RectF(rectF.left, rectF.centerY() - mCircleRadius, rectF.right, rectF.centerY() + mCircleRadius);
mBgPaint.setAntiAlias(false);
mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawRect(rectF1, mBgPaint);

} else if (selectedDateList.contains(lastLocalDate) && (!selectedDateList.contains(nextLocalDate) || !CalendarUtil.isEqualsMonth(nextLocalDate, localDate)) && CalendarUtil.isEqualsMonth(lastLocalDate, localDate)) {
//左矩形 右圆
RectF rectF1 = new RectF(rectF.left, rectF.centerY() - mCircleRadius, rectF.centerX(), rectF.centerY() + mCircleRadius);
mBgPaint.setAntiAlias(false);
mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawRect(rectF1, mBgPaint);

mBgPaint.setAntiAlias(false);
mBgPaint.setStyle(Paint.Style.FILL);
RectF rectF2 = new RectF(rectF.centerX() - mCircleRadius, rectF.centerY() - mCircleRadius, rectF.centerX() + mCircleRadius, rectF.centerY() + mCircleRadius);
canvas.drawArc(rectF2, -90, 180, false, mBgPaint);//右半圆

mBgPaint.setAntiAlias(true);
mBgPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(rectF2, -90, 180, false, mBgPaint);//右半圆弧

} else if ((!selectedDateList.contains(lastLocalDate) || !CalendarUtil.isEqualsMonth(lastLocalDate, localDate)) && selectedDateList.contains(nextLocalDate) && CalendarUtil.isEqualsMonth(nextLocalDate, localDate)) {
//右矩形 左圆
RectF rectF1 = new RectF(rectF.centerX(), rectF.centerY() - mCircleRadius, rectF.right, rectF.centerY() + mCircleRadius);
mBgPaint.setAntiAlias(false);
mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawRect(rectF1, mBgPaint);

mBgPaint.setAntiAlias(false);
mBgPaint.setStyle(Paint.Style.FILL);
RectF rectF2 = new RectF(rectF.centerX() - mCircleRadius, rectF.centerY() - mCircleRadius, rectF.centerX() + mCircleRadius, rectF.centerY() + mCircleRadius);
canvas.drawArc(rectF2, 90, 180, false, mBgPaint);//右半圆

mBgPaint.setAntiAlias(true);
mBgPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(rectF2, 90, 180, false, mBgPaint);//右半圆弧

} else {
//圆形
mBgPaint.setAntiAlias(true);
mBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawCircle(rectF.centerX(), rectF.centerY(), mCircleRadius, mBgPaint);
}
}
Expand Down
4 changes: 2 additions & 2 deletions ncalendar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "4.1.1"
versionName "4.1.2"
}
}

Expand All @@ -23,7 +23,7 @@ publish {
repoName = 'maven'//bintray仓库
groupId = 'com.necer.ncalendar'//jcenter上的路径
artifactId = 'ncalendar'//项目名称
publishVersion = '4.1.1'//版本号
publishVersion = '4.1.2'//版本号
desc = 'NCalendar'
website = 'https://github.com/yannecer/NCalendar'
}
53 changes: 20 additions & 33 deletions ncalendar/src/main/java/com/necer/calendar/BaseCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public abstract class BaseCalendar extends ViewPager implements ICalendar {
private Context mContext;
private Attrs mAttrs;


private SelectedModel mSelectedModel;//选中模式

private boolean mIsJumpClick;//是否是点击上月、下月或跳转,这个只在默认选中时有用
Expand All @@ -59,23 +58,12 @@ public abstract class BaseCalendar extends ViewPager implements ICalendar {
public BaseCalendar(@NonNull Context context, @Nullable AttributeSet attributeSet) {
super(context, attributeSet);
this.mAttrs = AttrsUtil.getAttrs(context, attributeSet);
this.mCalendarPainter = new InnerPainter(this);
init(context);
}


public BaseCalendar(Context context, Attrs attrs, CalendarPainter calendarPainter) {
super(context);
this.mAttrs = attrs;
this.mCalendarPainter = calendarPainter;
init(context);
}

private void init(Context context) {
this.mContext = context;
mSelectedModel = SelectedModel.SINGLE_SELECTED;
mAllSelectDateList = new ArrayList<>();
mInitializeDate = new LocalDate();
mStartDate = new LocalDate("1901-01-01");
mEndDate = new LocalDate("2099-12-31");
setBackgroundColor(mAttrs.bgCalendarColor);
addOnPageChangeListener(new SimpleOnPageChangeListener() {
@Override
Expand All @@ -84,23 +72,15 @@ public void onPageSelected(final int position) {
}
});

if (mSelectedModel == SelectedModel.SINGLE_SELECTED) {
mAllSelectDateList.clear();
mAllSelectDateList.add(mInitializeDate);
}
initAdapter();
}


private void initAdapter() {

String startDateString = mAttrs.startDateString;
String endDateString = mAttrs.endDateString;
try {
mStartDate = new LocalDate(startDateString);
mEndDate = new LocalDate(endDateString);
} catch (Exception e) {
throw new RuntimeException("startDate、endDate需要 yyyy-MM-dd 格式的日期");
if (mSelectedModel == SelectedModel.SINGLE_SELECTED) {
mAllSelectDateList.clear();
mAllSelectDateList.add(mInitializeDate);
}

if (mStartDate.isAfter(mEndDate)) {
Expand All @@ -115,9 +95,9 @@ private void initAdapter() {
throw new RuntimeException("endDate必须在2099-12-31之前");
}

/* if (startDate.isAfter(initializeDate) || endDate.isBefore(initializeDate)) {
throw new RuntimeException("日期区间需要包含今天");
}*/
if (mStartDate.isAfter(mInitializeDate) || mEndDate.isBefore(mInitializeDate)) {
throw new RuntimeException("日期区间必须包含初始化日期");
}

BaseCalendarAdapter calendarAdapter = getCalendarAdapter(mContext, mStartDate, mEndDate, mInitializeDate, mAttrs.firstDayOfWeek);
int currItem = calendarAdapter.getCurrItem();
Expand All @@ -127,8 +107,12 @@ private void initAdapter() {

@Override
public void setDateInterval(String startFormatDate, String endFormatDate) {
mAttrs.startDateString = startFormatDate;
mAttrs.endDateString = endFormatDate;
try {
mStartDate = new LocalDate(startFormatDate);
mEndDate = new LocalDate(endFormatDate);
} catch (Exception e) {
throw new RuntimeException("startDate、endDate需要 yyyy-MM-dd 格式的日期");
}
initAdapter();
}

Expand All @@ -144,9 +128,9 @@ public void setInitializeDate(String formatInitializeDate) {

@Override
public void setDateInterval(String startFormatDate, String endFormatDate, String formatInitializeDate) {
mAttrs.startDateString = startFormatDate;
mAttrs.endDateString = endFormatDate;
try {
mStartDate = new LocalDate(startFormatDate);
mEndDate = new LocalDate(endFormatDate);
mInitializeDate = new LocalDate(formatInitializeDate);
} catch (Exception e) {
throw new RuntimeException("setInitializeDate的参数需要 yyyy-MM-dd 格式的日期");
Expand Down Expand Up @@ -398,6 +382,9 @@ public void setCalendarPainter(CalendarPainter calendarPainter) {

@Override
public CalendarPainter getCalendarPainter() {
if (mCalendarPainter == null) {
mCalendarPainter = new InnerPainter(this);
}
return mCalendarPainter;
}

Expand Down Expand Up @@ -506,7 +493,7 @@ public void setSelectedMode(SelectedModel selectedMode) {
this.mSelectedModel = selectedMode;
mAllSelectDateList.clear();
if (mSelectedModel == SelectedModel.SINGLE_SELECTED) {
mAllSelectDateList.add(new LocalDate());
mAllSelectDateList.add(mInitializeDate);
}
}

Expand Down
4 changes: 0 additions & 4 deletions ncalendar/src/main/java/com/necer/calendar/MonthCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ protected BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate star
return new MonthCalendarAdapter(context, startDate, endDate, initializeDate, firstDayOfWeek);
}

public MonthCalendar(Context context, Attrs attrs, CalendarPainter calendarPainter) {
super(context, attrs, calendarPainter);
}

@Override
protected int getTwoDateCount(LocalDate startDate, LocalDate endDate, int type) {
return CalendarUtil.getIntervalMonths(startDate, endDate);
Expand Down
11 changes: 5 additions & 6 deletions ncalendar/src/main/java/com/necer/calendar/NCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public abstract class NCalendar extends FrameLayout implements IICalendar, Neste

private boolean isWeekHold;//是否需要周状态定

private CalendarPainter calendarPainter;
private boolean isInflateFinish;//是否加载完成,

protected ValueAnimator monthValueAnimator;//月日历动画
Expand Down Expand Up @@ -88,9 +87,10 @@ public NCalendar(@NonNull Context context, @Nullable AttributeSet attrs, int def
weekHeight = monthHeight / 5;
isWeekHold = this.attrs.isWeekHold;

calendarPainter = new InnerPainter(this);
weekCalendar = new WeekCalendar(context, this.attrs, calendarPainter);
monthCalendar = new MonthCalendar(context, this.attrs, calendarPainter);
monthCalendar = new MonthCalendar(context, attrs);
weekCalendar = new WeekCalendar(context, attrs);

setCalendarPainter(new InnerPainter(this));

monthCalendar.setOnMWDateChangeListener(onMWDateChangeListener);
weekCalendar.setOnMWDateChangeListener(onMWDateChangeListener);
Expand Down Expand Up @@ -418,7 +418,7 @@ public void toMonth() {

@Override
public CalendarPainter getCalendarPainter() {
return calendarPainter;
return monthCalendar.getCalendarPainter();
}

@Override
Expand Down Expand Up @@ -493,7 +493,6 @@ public void setOnClickDisableDateListener(OnClickDisableDateListener onClickDisa

@Override
public void setCalendarPainter(CalendarPainter calendarPainter) {
this.calendarPainter = calendarPainter;
monthCalendar.setCalendarPainter(calendarPainter);
weekCalendar.setCalendarPainter(calendarPainter);
}
Expand Down
4 changes: 0 additions & 4 deletions ncalendar/src/main/java/com/necer/calendar/WeekCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ public WeekCalendar(@NonNull Context context, @Nullable AttributeSet attributeSe
super(context, attributeSet);
}

public WeekCalendar(Context context, Attrs attrs, CalendarPainter calendarPainter) {
super(context, attrs, calendarPainter);
}

@Override
protected BaseCalendarAdapter getCalendarAdapter(Context context, LocalDate startDate, LocalDate endDate, LocalDate initializeDate, int firstDayOfWeek) {
return new WeekCalendarAdapter(context, startDate, endDate, initializeDate, firstDayOfWeek);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public enum CalendarState implements Serializable {
this.value = value;
}

public int getValue() {
return value;
}

public static CalendarState valueOf(int value) {
switch (value) {
case 100:
Expand Down
5 changes: 0 additions & 5 deletions ncalendar/src/main/java/com/necer/utils/Attrs.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

public class Attrs {

//日历默认视图
public static final int WEEK = 100;//周视图
public static final int MONTH = 101;//月视图

//指示圆点的位置
public static final int UP = 200;//再公历日期上面
Expand Down Expand Up @@ -62,8 +59,6 @@ public class Attrs {
public int holidayLocation;
public int workdayColor;

public String startDateString;
public String endDateString;
public int alphaColor;//不在同一月的颜色透明度
public int disabledAlphaColor;//不可用的日期颜色透明度
public String disabledString;//点击不可用的日期提示语
Expand Down
10 changes: 2 additions & 8 deletions ncalendar/src/main/java/com/necer/utils/AttrsUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.util.AttributeSet;

import com.necer.R;
import com.necer.enumeration.CalendarState;

/**
* Created by necer on 2018/11/28.
Expand Down Expand Up @@ -49,20 +50,13 @@ public static Attrs getAttrs(Context context, AttributeSet attributeSet) {
attrs.bgCalendarColor = ta.getColor(R.styleable.NCalendar_bgCalendarColor, context.getResources().getColor(R.color.white));
attrs.firstDayOfWeek = ta.getInt(R.styleable.NCalendar_firstDayOfWeek, Attrs.SUNDAY);
attrs.pointLocation = ta.getInt(R.styleable.NCalendar_pointLocation, Attrs.UP);
attrs.defaultCalendar = ta.getInt(R.styleable.NCalendar_defaultCalendar, Attrs.MONTH);
attrs.defaultCalendar = ta.getInt(R.styleable.NCalendar_defaultCalendar, CalendarState.MONTH.getValue());
attrs.holidayLocation = ta.getInt(R.styleable.NCalendar_holidayLocation, Attrs.TOP_RIGHT);

attrs.alphaColor = ta.getInt(R.styleable.NCalendar_alphaColor, 90);
attrs.disabledAlphaColor = ta.getInt(R.styleable.NCalendar_disabledAlphaColor, 50);

String startString = ta.getString(R.styleable.NCalendar_startDate);
String endString = ta.getString(R.styleable.NCalendar_endDate);
attrs.disabledString = ta.getString(R.styleable.NCalendar_disabledString);


attrs.startDateString = TextUtils.isEmpty(startString) ? "1901-01-01" : startString;
attrs.endDateString = TextUtils.isEmpty(endString) ? "2099-12-31" : endString;

ta.recycle();

return attrs;
Expand Down
6 changes: 3 additions & 3 deletions ncalendar/src/main/java/com/necer/view/CalendarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ public CalendarView(Context context, ViewGroup container, LocalDate initialDate,
@Override
protected void onDraw(Canvas canvas) {

CalendarPainter calendarPainter = mCalendar.getCalendarPainter();

for (int i = 0; i < mLineNum; i++) {
for (int j = 0; j < 7; j++) {
RectF rectF = mRectFList.get(i * 7 + j);
//矩形确定位置
if (rectF.isEmpty()) {
float width = getMeasuredWidth();
float height = getMeasuredHeight();
Expand All @@ -77,8 +76,9 @@ protected void onDraw(Canvas canvas) {
}
}

//开始绘制
CalendarPainter calendarPainter = mCalendar.getCalendarPainter();
LocalDate localDate = mDateList.get(i * 7 + j);

//在可用区间内的正常绘制,
if (!(localDate.isBefore(mStartDate) || localDate.isAfter(mEndDate))) {
if (isEqualsMonthOrWeek(localDate, mInitialDate)) { //当月和上下月的颜色不同
Expand Down
2 changes: 0 additions & 2 deletions ncalendar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

<attr name="hollowCircleColor" format="color" />
<attr name="hollowCircleStroke" format="dimension" />
<attr name="startDate" format="string" />
<attr name="endDate" format="string" />

<attr name="disabledString" format="string" />

Expand Down

0 comments on commit 4dd7ef9

Please sign in to comment.