|
|
@@ -1,468 +0,0 @@
|
|
|
-package com.xzjmyk.pm.activity.view;
|
|
|
-
|
|
|
-import android.content.Context;
|
|
|
-import android.graphics.Canvas;
|
|
|
-import android.graphics.Color;
|
|
|
-import android.graphics.Paint;
|
|
|
-import android.graphics.Path;
|
|
|
-import android.util.AttributeSet;
|
|
|
-import android.view.MotionEvent;
|
|
|
-import android.view.View;
|
|
|
-
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.Date;
|
|
|
-
|
|
|
-/**
|
|
|
- * 日历控件 功能:获得点选的日期区间
|
|
|
- * update by:Bitliker 2016/07/11
|
|
|
- */
|
|
|
-public class CalendarView extends View implements View.OnTouchListener {
|
|
|
- private Date curDate; // 当前日历显示的月
|
|
|
- private Date today; // 今天的日期文字显示红色
|
|
|
- private Date downDate; // 手指按下状态时临时日期
|
|
|
- private int downIndex; // 按下的格子索引
|
|
|
- private Calendar calendar;
|
|
|
- private Surface surface;
|
|
|
- private int[] date = new int[42]; // 日历显示数字
|
|
|
- private int curStartIndex, curEndIndex; // 当前显示的日历起始的索引
|
|
|
- private boolean isSelectMore = false;
|
|
|
- private int[] taskDay;
|
|
|
- //给控件设置监听事件
|
|
|
- private OnItemClickListener onItemClickListener;
|
|
|
-
|
|
|
- public CalendarView(Context context) {
|
|
|
- super(context);
|
|
|
- init();
|
|
|
- }
|
|
|
-
|
|
|
- public CalendarView(Context context, AttributeSet attrs) {
|
|
|
- super(context, attrs);
|
|
|
- init();
|
|
|
- }
|
|
|
-
|
|
|
- public int getDownIndex() {
|
|
|
- return downIndex;
|
|
|
- }
|
|
|
-
|
|
|
- private void init() {
|
|
|
- //获取当前日期对象
|
|
|
- downDate = curDate = today = new Date();
|
|
|
- calendar = Calendar.getInstance();
|
|
|
- calendar.setTime(curDate);
|
|
|
- downIndex = calendar.get(Calendar.DAY_OF_MONTH)+3;
|
|
|
- surface = new Surface();
|
|
|
- surface.density = getResources().getDisplayMetrics().density;
|
|
|
- setBackgroundColor(surface.bgColor);
|
|
|
- setOnTouchListener(this);
|
|
|
- }
|
|
|
-
|
|
|
- public void setTaskDay(int[] taskDay) {
|
|
|
- this.taskDay = taskDay;
|
|
|
- invalidate();
|
|
|
- }
|
|
|
-
|
|
|
- public int[] getTaskDay() {
|
|
|
- return taskDay;
|
|
|
- }
|
|
|
-
|
|
|
- //计算视图大小
|
|
|
- @Override
|
|
|
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
- surface.width = getResources().getDisplayMetrics().widthPixels;
|
|
|
- surface.height = (getResources().getDisplayMetrics().heightPixels * 1 / 3);
|
|
|
- widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.width, View.MeasureSpec.EXACTLY);
|
|
|
- heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(surface.height, View.MeasureSpec.EXACTLY);
|
|
|
- setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
|
|
|
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void onLayout(boolean changed, int left, int top, int right,
|
|
|
- int bottom) {
|
|
|
- if (changed) {
|
|
|
- surface.init();
|
|
|
- }
|
|
|
- super.onLayout(changed, left, top, right, bottom);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- protected void onDraw(Canvas canvas) {
|
|
|
- float weekTextY = surface.monthHeight + surface.weekHeight * 3 / 4f;
|
|
|
- for (int i = 0; i < surface.weekText.length; i++) {
|
|
|
- float weekTextX = i
|
|
|
- * surface.cellWidth
|
|
|
- + (surface.cellWidth - surface.weekPaint
|
|
|
- .measureText(surface.weekText[i])) / 2f;
|
|
|
- canvas.drawText(surface.weekText[i], weekTextX, weekTextY,
|
|
|
- surface.weekPaint);
|
|
|
- }
|
|
|
-
|
|
|
- // 计算日期
|
|
|
- calculateDate();
|
|
|
- // 按下状态,选择状态背景色
|
|
|
- drawDownOrSelectedBg(canvas);
|
|
|
- // write date number
|
|
|
- // today index
|
|
|
- int todayIndex = -1;
|
|
|
- calendar.setTime(curDate);
|
|
|
- String curYearAndMonth = calendar.get(Calendar.YEAR) + ""
|
|
|
- + calendar.get(Calendar.MONTH);
|
|
|
- calendar.setTime(today);
|
|
|
- String todayYearAndMonth = calendar.get(Calendar.YEAR) + ""
|
|
|
- + calendar.get(Calendar.MONTH);
|
|
|
- if (curYearAndMonth.equals(todayYearAndMonth)) {
|
|
|
- int todayNumber = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
- todayIndex = curStartIndex + todayNumber - 1;
|
|
|
- }
|
|
|
- //绘制日期
|
|
|
- for (int i = curStartIndex; i < 42; i++) {
|
|
|
- if (!isLastMonth(i) && !isNextMonth(i)) {
|
|
|
- int color = surface.textColor;
|
|
|
- if (todayIndex != -1 && i == todayIndex) {
|
|
|
- color = surface.todayNumberColor;
|
|
|
- drawCellText(canvas, i, "今", color);
|
|
|
- if (downDate == null)
|
|
|
- drawCellBg(canvas, i, surface.cellDownColor);
|
|
|
- } else {
|
|
|
- drawCellText(canvas, i, date[i] + "", color);
|
|
|
- }
|
|
|
- if (taskDay != null && taskDay.length > 0)
|
|
|
- for (int k : taskDay) {
|
|
|
- if (date[i] == k) {
|
|
|
- drawTask(canvas, i);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- super.onDraw(canvas);
|
|
|
- }
|
|
|
-
|
|
|
- private void calculateDate() {
|
|
|
- calendar.setTime(curDate);
|
|
|
- calendar.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
- int dayInWeek = calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
- int monthStart = dayInWeek;
|
|
|
- if (monthStart == 1) {
|
|
|
- monthStart = 8;
|
|
|
- }
|
|
|
- monthStart -= 1; //以日为开头-1,以星期一为开头-2
|
|
|
- curStartIndex = monthStart;
|
|
|
- date[monthStart] = 1;
|
|
|
- // last month
|
|
|
- if (monthStart > 0) {
|
|
|
- calendar.set(Calendar.DAY_OF_MONTH, 0);
|
|
|
- int dayInmonth = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
- for (int i = monthStart - 1; i >= 0; i--) {
|
|
|
- date[i] = dayInmonth;
|
|
|
- dayInmonth--;
|
|
|
- }
|
|
|
- calendar.set(Calendar.DAY_OF_MONTH, date[0]);
|
|
|
- }
|
|
|
- // this month
|
|
|
- calendar.setTime(curDate);
|
|
|
- calendar.add(Calendar.MONTH, 1);
|
|
|
- calendar.set(Calendar.DAY_OF_MONTH, 0);
|
|
|
- // calendar.get(Calendar.DAY_OF_MONTH));
|
|
|
- int monthDay = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
- for (int i = 1; i < monthDay; i++) {
|
|
|
- date[monthStart + i] = i + 1;
|
|
|
- }
|
|
|
- curEndIndex = monthStart + monthDay;
|
|
|
- // next month
|
|
|
- for (int i = monthStart + monthDay; i < 42; i++) {
|
|
|
- date[i] = i - (monthStart + monthDay) + 1;
|
|
|
- }
|
|
|
- if (curEndIndex < 42) {
|
|
|
- // 显示了下一月的
|
|
|
- calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
- }
|
|
|
- calendar.set(Calendar.DAY_OF_MONTH, date[41]);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 绘画有任务日期星号
|
|
|
- *
|
|
|
- * @param canvas
|
|
|
- * @param index
|
|
|
- */
|
|
|
- private void drawTask(Canvas canvas, int index) {
|
|
|
- int x = getXByIndex(index);
|
|
|
- int y = getYByIndex(index);
|
|
|
- float cellY = surface.monthHeight + surface.weekHeight + (y - 1)
|
|
|
- * surface.cellHeight + //当前日期框最上方
|
|
|
- surface.cellHeight * 9 / 16f;//当前框的中心Y
|
|
|
- float cellX = (surface.cellWidth * (x - 1)) //当前日期框左边框
|
|
|
- + surface.cellWidth / 2;//当前中心X
|
|
|
- float radius = Math.min(surface.cellHeight, surface.cellWidth) * (3 / 8f);//获取最短的长度的
|
|
|
-// 绘制圆,参数一是中心点的x轴,参数二是中心点的y轴,参数三是半径,参数四是paint对象;
|
|
|
- canvas.drawCircle(cellX, cellY, radius, surface.circlePaint);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param canvas
|
|
|
- * @param index
|
|
|
- * @param text
|
|
|
- */
|
|
|
- private void drawCellText(Canvas canvas, int index, String text, int color) {
|
|
|
- int x = getXByIndex(index);
|
|
|
- int y = getYByIndex(index);
|
|
|
- if (downDate != null && downIndex == index)
|
|
|
- surface.datePaint.setColor(surface.bgColor);
|
|
|
- else
|
|
|
- surface.datePaint.setColor(color);
|
|
|
- float cellY = surface.monthHeight + surface.weekHeight + (y - 1)
|
|
|
- * surface.cellHeight + //当前日期框最上方
|
|
|
- surface.cellHeight * 3 / 4f;
|
|
|
- float cellX = (surface.cellWidth * (x - 1)) //当前日期框左边框
|
|
|
- + (surface.cellWidth - surface.datePaint.measureText(text))
|
|
|
- / 2f;
|
|
|
- canvas.drawText(text, cellX, cellY, surface.datePaint);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 画被点击的背景
|
|
|
- *
|
|
|
- * @param canvas
|
|
|
- * @param index
|
|
|
- * @param color
|
|
|
- */
|
|
|
- private void drawCellBg(Canvas canvas, int index, int color) {
|
|
|
- int x = getXByIndex(index);
|
|
|
- int y = getYByIndex(index);
|
|
|
- float cellY = surface.monthHeight + surface.weekHeight + (y - 1)
|
|
|
- * surface.cellHeight + //当前日期框最上方
|
|
|
- surface.cellHeight * 9 / 16f;//当前框的中心Y
|
|
|
- float cellX = (surface.cellWidth * (x - 1)) //当前日期框左边框
|
|
|
- + surface.cellWidth / 2;//当前中心X
|
|
|
- float radius = Math.min(surface.cellHeight, surface.cellWidth) * (3 / 8f);//获取最短的长度的
|
|
|
-// 绘制圆,参数一是中心点的x轴,参数二是中心点的y轴,参数三是半径,参数四是paint对象;
|
|
|
- canvas.drawCircle(cellX, cellY, radius, surface.cellBgPaint);
|
|
|
- }
|
|
|
-
|
|
|
- private void drawDownOrSelectedBg(Canvas canvas) {
|
|
|
- if (downDate != null) {
|
|
|
- drawCellBg(canvas, downIndex, surface.cellDownColor);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private boolean isLastMonth(int i) {
|
|
|
- if (i < curStartIndex) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- private boolean isNextMonth(int i) {
|
|
|
- if (i >= curEndIndex) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- private int getXByIndex(int i) {
|
|
|
- return i % 7 + 1; // 1 2 3 4 5 6 7
|
|
|
- }
|
|
|
-
|
|
|
- private int getYByIndex(int i) {
|
|
|
- return i / 7 + 1; // 1 2 3 4 5 6
|
|
|
- }
|
|
|
-
|
|
|
- // 获得当前应该显示的年月
|
|
|
- public String getYearAndmonth() {
|
|
|
- calendar.setTime(curDate);
|
|
|
- int year = calendar.get(Calendar.YEAR);
|
|
|
- int month = calendar.get(Calendar.MONTH) + 1;
|
|
|
- return year + "-" + month;
|
|
|
- }
|
|
|
-
|
|
|
- //上一月
|
|
|
- public String clickLeftMonth() {
|
|
|
- calendar.setTime(curDate);
|
|
|
- calendar.add(Calendar.MONTH, -1);
|
|
|
- curDate = calendar.getTime();
|
|
|
- invalidate();
|
|
|
- return getYearAndmonth();
|
|
|
- }
|
|
|
-
|
|
|
- //下一月
|
|
|
- public String clickRightMonth() {
|
|
|
- calendar.setTime(curDate);
|
|
|
- calendar.add(Calendar.MONTH, 1);
|
|
|
- curDate = calendar.getTime();
|
|
|
- invalidate();
|
|
|
- return getYearAndmonth();
|
|
|
- }
|
|
|
-
|
|
|
- //设置日历时间
|
|
|
- public void setCalendarData(Date date) {
|
|
|
- calendar.setTime(date);
|
|
|
- invalidate();
|
|
|
- }
|
|
|
-
|
|
|
- //获取日历时间
|
|
|
- public void getCalendatData() {
|
|
|
- calendar.getTime();
|
|
|
- }
|
|
|
-
|
|
|
- //设置是否多选
|
|
|
- public boolean isSelectMore() {
|
|
|
- return isSelectMore;
|
|
|
- }
|
|
|
-
|
|
|
- public void setSelectMore(boolean isSelectMore) {
|
|
|
- this.isSelectMore = isSelectMore;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private void setSelectedDateByCoor(float x, float y) {
|
|
|
- if (y > surface.monthHeight + surface.weekHeight) {
|
|
|
- int m = (int) (Math.floor(x / surface.cellWidth) + 1);
|
|
|
- int n = (int) (Math.floor((y - (surface.monthHeight + surface.weekHeight))
|
|
|
- / Float.valueOf(surface.cellHeight)) + 1);
|
|
|
- int index = (n - 1) * 7 + m - 1;
|
|
|
- if (index < curStartIndex || curEndIndex <= index) return;
|
|
|
- downIndex = index;
|
|
|
- calendar.setTime(curDate);
|
|
|
- if (isLastMonth(downIndex)) {
|
|
|
- calendar.add(Calendar.MONTH, -1);
|
|
|
- } else if (isNextMonth(downIndex)) {
|
|
|
- calendar.add(Calendar.MONTH, 1);
|
|
|
- }
|
|
|
- calendar.set(Calendar.DAY_OF_MONTH, date[downIndex]);
|
|
|
- downDate = calendar.getTime();
|
|
|
- }
|
|
|
- //判断点击的在当月日期里面
|
|
|
-// invalidate();
|
|
|
- }
|
|
|
-
|
|
|
- float x;
|
|
|
- float y;
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean onTouch(View v, MotionEvent event) {
|
|
|
- switch (event.getAction()) {
|
|
|
- case MotionEvent.ACTION_DOWN:
|
|
|
- x = event.getX();
|
|
|
- y = event.getY();
|
|
|
- break;
|
|
|
- case MotionEvent.ACTION_UP:
|
|
|
- if (Math.abs(event.getX() - x) > 10 || Math.abs(event.getY() - y) > 10)
|
|
|
- return true;
|
|
|
- setSelectedDateByCoor(event.getX(), event.getY());
|
|
|
- if (downDate != null) {
|
|
|
- //响应监听事件
|
|
|
- onItemClickListener.OnItemClick(date[downIndex]);
|
|
|
- }
|
|
|
- invalidate();
|
|
|
- break;
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- //给控件设置监听事件
|
|
|
- public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
|
|
|
- this.onItemClickListener = onItemClickListener;
|
|
|
- }
|
|
|
-
|
|
|
- //监听接口
|
|
|
- public interface OnItemClickListener {
|
|
|
- void OnItemClick(int date);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 1. 布局尺寸 2. 文字颜色,大小 3. 当前日期的颜色,选择的日期颜色
|
|
|
- */
|
|
|
- private class Surface {
|
|
|
- public float density;
|
|
|
- public int width; // 整个控件的宽度
|
|
|
- public int height; // 整个控件的高度
|
|
|
- public float monthHeight; // 显示月的高度
|
|
|
- public float weekHeight; // 显示星期的高度
|
|
|
- public float cellWidth; // 日期方框宽度
|
|
|
- public float cellHeight; // 日期方框高度
|
|
|
- public float borderWidth;
|
|
|
- public int bgColor = Color.parseColor("#EBE9E9");
|
|
|
- private int textColor = Color.BLACK;
|
|
|
- private int btnColor = Color.parseColor("#666666");
|
|
|
- private int borderColor = Color.parseColor("#CCCCCC");
|
|
|
- public int todayNumberColor = Color.RED;
|
|
|
- public int cellDownColor = Color.parseColor("#CCFFFF");
|
|
|
- public int cellSelectedColor = Color.parseColor("#ea493d");
|
|
|
- public Paint borderPaint;
|
|
|
- public Paint monthPaint;
|
|
|
- public Paint weekPaint;
|
|
|
- public Paint datePaint;
|
|
|
- public Paint circlePaint;
|
|
|
- public Paint monthChangeBtnPaint;
|
|
|
- public Paint cellBgPaint;
|
|
|
- public Path boxPath; // 边框路径
|
|
|
- public String[] weekText = {"日", "一", "二", "三", "四", "五", "六"};
|
|
|
-
|
|
|
- public void init() {
|
|
|
- float temp = height / 7f;
|
|
|
- monthHeight = 0;//(float) ((temp + temp * 0.3f) * 0.6);
|
|
|
- weekHeight = (float) ((temp + temp * 0.3f) * 0.7);
|
|
|
- cellHeight = (height - monthHeight - weekHeight) / 5f;
|
|
|
- weekHeight = cellHeight = (height - 10) / 6f;
|
|
|
- weekHeight += 10;
|
|
|
-// weekHeight = (float) (cellHeight * 1.2);
|
|
|
- cellWidth = width / 7f;
|
|
|
- borderPaint = new Paint();
|
|
|
- borderPaint.setColor(borderColor);
|
|
|
- borderPaint.setStyle(Paint.Style.STROKE);
|
|
|
- borderWidth = (float) (0.5 * density);
|
|
|
- borderWidth = borderWidth < 1 ? 1 : borderWidth;
|
|
|
- borderPaint.setStrokeWidth(borderWidth);
|
|
|
- monthPaint = new Paint();
|
|
|
- monthPaint.setColor(textColor);
|
|
|
- monthPaint.setAntiAlias(true);
|
|
|
- float textSize = cellHeight * 0.4f;
|
|
|
- monthPaint.setTextSize(textSize);
|
|
|
- //monthPaint.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
- weekPaint = new Paint();
|
|
|
- weekPaint.setColor(textColor);
|
|
|
- weekPaint.setAntiAlias(true);
|
|
|
- float weekTextSize = weekHeight * 0.5f;
|
|
|
- weekPaint.setTextSize(weekTextSize);
|
|
|
- //weekPaint.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
- datePaint = new Paint();
|
|
|
- datePaint.setColor(textColor);
|
|
|
- datePaint.setAntiAlias(true);
|
|
|
- circlePaint = new Paint();
|
|
|
- circlePaint.setColor(Color.RED);
|
|
|
- circlePaint.setAntiAlias(true);//抗锯齿 不然会不好看
|
|
|
- circlePaint.setFilterBitmap(true);
|
|
|
- circlePaint.setStyle(Paint.Style.STROKE);//空心
|
|
|
- circlePaint.setStrokeWidth(3);
|
|
|
- float cellTextSize = cellHeight * 0.5f;
|
|
|
- datePaint.setTextSize(cellTextSize);
|
|
|
- //datePaint.setTypeface(Typeface.DEFAULT_BOLD);
|
|
|
- boxPath = new Path();
|
|
|
- boxPath.rLineTo(width, 0);
|
|
|
- boxPath.moveTo(0, monthHeight + weekHeight);
|
|
|
- boxPath.rLineTo(width, 0);
|
|
|
- for (int i = 1; i < 6; i++) {
|
|
|
- boxPath.moveTo(0, monthHeight + weekHeight + i * cellHeight);
|
|
|
- boxPath.rLineTo(width, 0);
|
|
|
- boxPath.moveTo(i * cellWidth, monthHeight);
|
|
|
- boxPath.rLineTo(0, height - monthHeight);
|
|
|
- }
|
|
|
- boxPath.moveTo(6 * cellWidth, monthHeight);
|
|
|
- boxPath.rLineTo(0, height - monthHeight);
|
|
|
- monthChangeBtnPaint = new Paint();
|
|
|
- monthChangeBtnPaint.setAntiAlias(true);
|
|
|
- monthChangeBtnPaint.setStyle(Paint.Style.FILL_AND_STROKE);
|
|
|
- monthChangeBtnPaint.setColor(btnColor);
|
|
|
- cellBgPaint = new Paint();
|
|
|
- cellBgPaint.setAntiAlias(true);
|
|
|
- cellBgPaint.setStyle(Paint.Style.FILL);
|
|
|
- cellBgPaint.setColor(cellSelectedColor);
|
|
|
- datePaint.setFakeBoldText(false);
|
|
|
- weekPaint.setFakeBoldText(false);
|
|
|
- borderPaint.setFakeBoldText(false);
|
|
|
- cellBgPaint.setFakeBoldText(false);
|
|
|
- monthPaint.setFakeBoldText(false);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|