Skip to content

Commit

Permalink
2020/09/30 1、优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Edison0716 committed Sep 30, 2020
1 parent fcd6264 commit 57ae04c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,41 @@
*/
public class BaseCalendarEntity implements Serializable {

// 年
/**
* 年
*/
private int year;
// 月

/**
* 月
*/
private int month;
// 日

/**
* 日
*/
private int day;

// 是否是今天
/**
* 是否是今天
*/
private boolean isToday;

// 绘制时的坐标
/**
* 绘制时的坐标
*/
private int locationX;

private int locationY;

// 是否是可用的
/**
* 是否是可用的
*/
private boolean isAvailable;

// 时间戳
/**
* 时间戳
*/
private long timeStamp;

public BaseCalendarEntity(int year, int month, int day) {
Expand Down
46 changes: 36 additions & 10 deletions library/src/main/java/com/junlong0716/library/BaseCalendarView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.junlong0716.library;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
Expand Down Expand Up @@ -97,14 +98,23 @@ public BaseCalendarView(Context context, @Nullable AttributeSet attrs) {

public BaseCalendarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint(context);
initPaint(context, attrs);
}

/**
* 初始化 画笔
* @param context 上下文
* @param attrs
*/
private void initPaint(Context context) {
private void initPaint(Context context, AttributeSet attrs) {

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseCalendarView);

try {
mItemHeight = (int) typedArray.getDimension(R.styleable.BaseCalendarView_date_height,-1f);
} finally {
typedArray.recycle();
}

mUnselectedDateTextPaint.setTextSize(CalendarUtil.dipToPx(context, DEFAULT_TEXT_SIZE));
mUnselectedDateTextPaint.setAntiAlias(true);
Expand Down Expand Up @@ -180,7 +190,8 @@ public List<? super BaseCalendarEntity> getDate() {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.EXACTLY) {
if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED
|| heightMode == MeasureSpec.EXACTLY) {
int realHeight = getPaddingTop() + getPaddingBottom() + mLineCount * mItemHeight;
setMeasuredDimension(widthMeasureSpec, MeasureSpec.makeMeasureSpec(realHeight, MeasureSpec.EXACTLY));
// 计算一个格子的宽度
Expand All @@ -192,25 +203,40 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
* 计算偏移量
*/
public void calculateOffset() {
if (mItems.isEmpty()){
if (mItems.isEmpty()) {
return;
}
// 这里设置高度与宽度相等
if (mItemHeight <= 0) {
mItemHeight = CalendarUtil.dipToPx(getContext(), 60);
} else {
// EdisonLi TODO 2020/3/10 自定义Item高度
}
mMonthDaysCount = CalendarUtil.getMonthDaysCount(mYear, mMonth);
// 偏移量
mDayOfMonthStartOffset = CalendarUtil.getDayOfMonthStartOffset(mYear, mMonth, mDay, mWeekStart);
mLineCount = CalendarUtil.getMaxLines(mDayOfMonthStartOffset, mMonthDaysCount);
mTotalBlocksInMonth = CalendarUtil.getTotalBlockInMonth(mLineCount);
// 残月
if (mItems.size() != mMonthDaysCount) {
// 一个月的总计时间 减去当前的日期
mMonthDaysCount = mMonthDaysCount - mDay + 1;
// 取整获取多少行
int lineIndex = (mMonthDaysCount + mDayOfMonthStartOffset) / DAYS_COUNT_IN_WEEK;
// 有余数直接加一行
if ((mMonthDaysCount + mDayOfMonthStartOffset) % DAYS_COUNT_IN_WEEK > 0) {
lineIndex += 1;
}
// 一共得行数
mLineCount = lineIndex;
}
}

int lineIndex = (mDay) / 7;
mMonthDaysCount = mMonthDaysCount - mDay + 1;
// 一共得行数
mLineCount = mLineCount - lineIndex;
/**
* 设置日期高度
* @param dateHeight 每一个的日期的高度
*/
public void setDateHeight(int dateHeight){
mItemHeight = CalendarUtil.dipToPx(getContext(), dateHeight);
requestLayout();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* @Author: LiJunlong
* @CreateDate: 2020/3/7 6:56 PM
*/
public abstract class BaseCommonCalendarView<T extends BaseCalendarEntity> extends BaseCalendarView implements ICalendarView<T> {
public abstract class BaseCommonCalendarView<T extends BaseCalendarEntity> extends BaseCalendarView
implements ICalendarView<T> {

public static final String RANGE_CALENDAR_CLASS_NAME = BaseCommonCalendarView.class.getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
*/
public interface ICalendarStrategy<T extends BaseCalendarEntity> {
/**
* 点击策略
* @param clickEntity 点击的日期
*/
void handleClick(T clickEntity);

/**
* 返回选中的那个日期
* @return 选中得数据
*/
List<T> getCheckedDates();


/**
* 重置
*/
void reset();

/**
Expand Down
16 changes: 16 additions & 0 deletions library/src/main/java/com/junlong0716/library/ICalendarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,25 @@
* @CreateDate: 2020/3/7 6:02 PM
*/
public interface ICalendarView<T extends BaseCalendarEntity> {

/**
* 绘制文字
* @param canvas 画板
* @param item 数据
*/
void drawDayText(Canvas canvas, T item);

/**
* 绘制选中得
* @param canvas 画板
* @param item 数据
*/
void drawDaySelected(Canvas canvas, T item);


/**
* 日历策略
* @param calendarDates 画板
*/
void createCalendarStrategy(List<List<T>> calendarDates);
}
7 changes: 7 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="BaseCalendarView">
<attr name="date_height" format="dimension"/>
</declare-styleable>
</resources>

0 comments on commit 57ae04c

Please sign in to comment.