Skip to content

Commit

Permalink
修改背景绘制逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Mar 27, 2020
1 parent c61a41f commit 3953f86
Show file tree
Hide file tree
Showing 32 changed files with 472 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,13 @@ public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, Loc
}



public static class DingAdapter extends CalendarAdapter {
@Override
public View getCalendarItemView(Context context) {
return LayoutInflater.from(context).inflate(R.layout.item_calendar, null);
}

@Override
public View getCalendarBackgroundView(Context context) {
return LayoutInflater.from(context).inflate(R.layout.bg_calendar, null);
}

@Override
public void onBindCalendarBackgroundView(ICalendarView iCalendarView, View calendarBackgroundView, LocalDate localDate, int totalDistance, int currentDistance) {
super.onBindCalendarBackgroundView(iCalendarView, calendarBackgroundView, localDate, totalDistance, currentDistance);
if (iCalendarView.getCalendarType() == CalendarType.MONTH) {
TextView tv_bg = calendarBackgroundView.findViewById(R.id.tv_bg);
if (totalDistance != 0) {
float i = (float) currentDistance / (float) totalDistance;
tv_bg.setAlpha(i);
tv_bg.setText(String.valueOf(localDate.getMonthOfYear()));

Log.e("onBindCalenww", "onBindCalendarBackgroundView:::" + localDate);
}
}
}

@Override
public void onBindToadyView(View view, LocalDate localDate, List<LocalDate> totalCheckedDateList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.necer.ncalendar.activity;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.util.Log;
import android.widget.TextView;

Expand All @@ -14,6 +17,7 @@
import com.necer.listener.OnCalendarChangedListener;
import com.necer.listener.OnCalendarMultipleChangedListener;
import com.necer.ncalendar.R;
import com.necer.painter.CalendarBackground;
import com.necer.painter.InnerPainter;
import com.necer.utils.CalendarUtil;

Expand Down Expand Up @@ -51,6 +55,17 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
InnerPainter innerPainter = (InnerPainter) miui10Calendar.getCalendarPainter();
innerPainter.setPointList(pointList);


// Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
// miui10Calendar.setMonthCalendarBackground(new CalendarBackground() {
// @Override
// public Drawable getBackgroundDrawable(LocalDate localDate, int currentDistance, int totalDistance) {
// return drawable;
// }
// });



Map<String, String> strMap = new HashMap<>();
strMap.put("2019-01-25", "测试");
strMap.put("2019-01-23", "测试1");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.necer.ncalendar.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.Nullable;
Expand All @@ -14,6 +15,7 @@
import com.necer.listener.OnCalendarChangedListener;
import com.necer.listener.OnCalendarMultipleChangedListener;
import com.necer.ncalendar.R;
import com.necer.painter.CalendarBackground;

import org.joda.time.LocalDate;

Expand All @@ -37,6 +39,9 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
monthCalendar.setCheckMode(checkModel);
// monthCalendar.setDefaultSelectFirst(true);




monthCalendar.setOnCalendarChangedListener(new OnCalendarChangedListener() {
@Override
public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, LocalDate localDate, DateChangeBehavior dateChangeBehavior) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ private Paint getPaint() {
return paint;
}

@Override
public void onDrawCalendarBackground(ICalendarView calendarView, Canvas canvas, RectF rectF, LocalDate localDate, int totalDistance, int currentDistance) {

}

@Override
public void onDrawToday(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList) {
drawSelectBg(canvas, rectF, localDate, true, selectedDateList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public StretchPainter(Context context) {
}


@Override
public void onDrawCalendarBackground(ICalendarView calendarView, Canvas canvas, RectF rectF, LocalDate localDate, int totalDistance, int currentDistance) {

}

@Override
public void onDrawToday(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList) {
canvas.drawRect(rectF, mBgPaint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ private Paint getPaint() {
}


@Override
public void onDrawCalendarBackground(ICalendarView calendarView, Canvas canvas, RectF rectF, LocalDate localDate, int totalDistance, int currentDistance) {

}

@Override
public void onDrawToday(Canvas canvas, RectF rectF, LocalDate localDate, List<LocalDate> selectedDateList) {
drawSelectBg(canvas, rectF, selectedDateList.contains(localDate), true);
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/drawable/bg_checked.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<!-- <stroke-->
<!-- android:width="1dp"-->
<!-- android:color="@color/hollowCircleColor" />-->
<stroke
android:width="1dp"
android:color="@color/N_pointColor" />
<size
android:width="10dp"
android:height="10dp" />
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/drawable/bg_today_checked.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<!-- <stroke-->
<!-- android:width="1dp"-->
<!-- android:color="@color/holidayColor" />-->
<stroke
android:width="1dp"
android:color="@color/N_pointColor" />
<size
android:width="10dp"
android:height="10dp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ public class GridCalendarAdapter extends BaseAdapter {


private List<View> viewList;
private CalendarView2 calendarView;
public GridCalendarAdapter(List<View> viewList, CalendarView2 calendarView) {
public GridCalendarAdapter(List<View> viewList) {
this.viewList = viewList;
this.calendarView = calendarView;
}

@Override
Expand All @@ -40,7 +38,6 @@ public View getView(int position, View convertView, ViewGroup parent) {
int realHeight = parent.getMeasuredHeight() - parent.getPaddingBottom() - parent.getPaddingTop();
AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, realHeight / (viewList.size() / 7));
calendarItemView.setLayoutParams(params);
calendarView.bindView(calendarItemView,position);
return calendarItemView;
}
}
45 changes: 33 additions & 12 deletions ncalendar/src/main/java/com/necer/calendar/BaseCalendar.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.necer.calendar;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
Expand All @@ -11,15 +12,18 @@
import com.necer.adapter.BasePagerAdapter;
import com.necer.enumeration.CalendarBuild;
import com.necer.enumeration.DateChangeBehavior;
import com.necer.enumeration.MultipleNumModel;
import com.necer.enumeration.MultipleCountModel;
import com.necer.enumeration.CheckModel;
import com.necer.listener.OnCalendarChangedListener;
import com.necer.listener.OnCalendarMultipleChangedListener;
import com.necer.listener.OnClickDisableDateListener;
import com.necer.listener.OnMWDateChangeListener;
import com.necer.painter.CalendarAdapter;
import com.necer.painter.CalendarBackground;
import com.necer.painter.CalendarPainter;
import com.necer.painter.InnerPainter;
import com.necer.painter.NumBackground;
import com.necer.painter.WhiteBackground;
import com.necer.utils.Attrs;
import com.necer.utils.AttrsUtil;
import com.necer.view.ICalendarView;
Expand Down Expand Up @@ -69,13 +73,14 @@ public abstract class BaseCalendar extends ViewPager implements ICalendar {
protected CalendarPainter mCalendarPainter;
private List<LocalDate> mTotalCheckedDateList;

private MultipleNumModel mMultipleNumModel;//多选数量模式
private int mMultipleNum;//多选个数
private MultipleCountModel mMultipleCountModel;//多选数量模式
private int mMultipleCount;//多选个数

private int mFirstDayOfWeek;//日历的周一开始、周日开始
private boolean mAllMonthSixLine;//月日历是否都是6行

private CalendarBuild mCalendarBuild;
private CalendarBackground mCalendarBackground;

private CalendarAdapter mCalendarAdapter;

Expand All @@ -98,8 +103,13 @@ public BaseCalendar(@NonNull Context context, @Nullable AttributeSet attributeSe
mEndDate = new LocalDate("2099-12-31");

//背景颜色
int calendarBgColor = mAttrs.bgCalendarColor;
setBackgroundColor(calendarBgColor);
if (mAttrs.showNumberBackground) {
mCalendarBackground = new NumBackground(mAttrs.numberBackgroundTextSize, mAttrs.numberBackgroundTextColor, mAttrs.numberBackgroundAlphaColor);
} else if (mAttrs.calendarBackground != null) {
mCalendarBackground = (localDate, currentDistance, totalDistance) -> mAttrs.calendarBackground;
} else {
mCalendarBackground = new WhiteBackground();
}

mFirstDayOfWeek = mAttrs.firstDayOfWeek;
mAllMonthSixLine = mAttrs.allMonthSixLine;
Expand Down Expand Up @@ -246,7 +256,7 @@ public void onClickNextMonthDate(LocalDate localDate) {
}
}

public void jump(LocalDate localDate, boolean isCheck) {
protected void jump(LocalDate localDate, boolean isCheck) {
//判断日期是否合法
if (!isAvailable(localDate)) {
if (getVisibility() == VISIBLE) {
Expand All @@ -262,9 +272,9 @@ public void jump(LocalDate localDate, boolean isCheck) {
if (isCheck) {
if (mCheckModel == CheckModel.MULTIPLE) {
if (!mTotalCheckedDateList.contains(localDate)) {
if (mTotalCheckedDateList.size() == mMultipleNum && mMultipleNumModel == MultipleNumModel.FULL_CLEAR) {
if (mTotalCheckedDateList.size() == mMultipleCount && mMultipleCountModel == MultipleCountModel.FULL_CLEAR) {
mTotalCheckedDateList.clear();
} else if (mTotalCheckedDateList.size() == mMultipleNum && mMultipleNumModel == MultipleNumModel.FULL_REMOVE_FIRST) {
} else if (mTotalCheckedDateList.size() == mMultipleCount && mMultipleCountModel == MultipleCountModel.FULL_REMOVE_FIRST) {
mTotalCheckedDateList.remove(0);
}
mTotalCheckedDateList.add(localDate);
Expand Down Expand Up @@ -406,7 +416,7 @@ public void jumpMonth(int year, int month) {
}

@Override
public List<LocalDate> getAllCheckedDateList() {
public List<LocalDate> getTotalCheckedDateList() {
return mTotalCheckedDateList;
}

Expand Down Expand Up @@ -609,10 +619,10 @@ public void setDefaultCheckedFirstDate(boolean isDefaultCheckedFirstDate) {
}

@Override
public void setMultipleNum(int multipleNum, MultipleNumModel multipleNumModel) {
public void setMultipleCount(int multipleCount, MultipleCountModel multipleCountModel) {
this.mCheckModel = CheckModel.MULTIPLE;
this.mMultipleNumModel = multipleNumModel;
this.mMultipleNum = multipleNum;
this.mMultipleCountModel = multipleCountModel;
this.mMultipleCount = multipleCount;
}


Expand All @@ -629,4 +639,15 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
public void setScrollEnable(boolean scrollEnable) {
this.mScrollEnable = scrollEnable;
}


@Override
public void setCalendarBackground(CalendarBackground calendarBackground) {
this.mCalendarBackground = calendarBackground;
}

@Override
public CalendarBackground getCalendarBackground() {
return mCalendarBackground;
}
}
2 changes: 0 additions & 2 deletions ncalendar/src/main/java/com/necer/calendar/EmuiCalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,4 @@ protected void setWeekVisible(boolean isUp) {
weekCalendar.setVisibility(INVISIBLE);
}
}


}
20 changes: 14 additions & 6 deletions ncalendar/src/main/java/com/necer/calendar/ICalendar.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.necer.calendar;

import com.necer.enumeration.MultipleNumModel;
import com.necer.enumeration.MultipleCountModel;
import com.necer.enumeration.CheckModel;
import com.necer.listener.OnCalendarChangedListener;
import com.necer.listener.OnCalendarMultipleChangedListener;
import com.necer.listener.OnClickDisableDateListener;
import com.necer.painter.CalendarAdapter;
import com.necer.painter.CalendarBackground;
import com.necer.painter.CalendarPainter;
import com.necer.utils.Attrs;

Expand All @@ -30,11 +31,11 @@ public interface ICalendar {
/**
* 多选个数和模式
*
* @param multipleNum 多选个数
* @param multipleNumModel FULL_CLEAR-超过清除所有
* FULL_REMOVE_FIRST-超过清除第一个
* @param multipleCount 多选个数
* @param multipleCountModel FULL_CLEAR-超过清除所有
* FULL_REMOVE_FIRST-超过清除第一个
*/
void setMultipleNum(int multipleNum, MultipleNumModel multipleNumModel);
void setMultipleCount(int multipleCount, MultipleCountModel multipleCountModel);


/**
Expand Down Expand Up @@ -175,7 +176,7 @@ public interface ICalendar {
*
* @return
*/
List<LocalDate> getAllCheckedDateList();
List<LocalDate> getTotalCheckedDateList();

/**
* 获取当前页面选中的日期集合
Expand Down Expand Up @@ -208,8 +209,15 @@ public interface ICalendar {

/**
* 设置日历是否可以左右滑动
*
* @param scrollEnable
*/
void setScrollEnable(boolean scrollEnable);


void setCalendarBackground(CalendarBackground calendarBackground) throws IllegalAccessException;

CalendarBackground getCalendarBackground() throws IllegalAccessException;


}
5 changes: 5 additions & 0 deletions ncalendar/src/main/java/com/necer/calendar/IICalendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.necer.enumeration.CalendarState;
import com.necer.listener.OnCalendarScrollingListener;
import com.necer.listener.OnCalendarStateChangedListener;
import com.necer.painter.CalendarBackground;

/**
* 折叠日历特有的功能接口
Expand Down Expand Up @@ -67,4 +68,8 @@ public interface IICalendar extends ICalendar {
CalendarState getCalendarState();


void setMonthCalendarBackground(CalendarBackground calendarBackground);

void setWeekCalendarBackground(CalendarBackground calendarBackground);

}
Loading

0 comments on commit 3953f86

Please sign in to comment.