package com.lashou.calendar;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class Grid extends CalendarParent implements Serializable
{
//修改该
boolean isBefore = false;
Date estimateDate = new Date();
SimpleDateFormat sdf;
java.util.Calendar calendarDate;
private Date selectDate = null;
//修改完
//修改完
private String[] days = new String[42];//这个里面包含了每个月的42个数字
// true表示有记录,false表示没记录
private boolean[] recordDays = new boolean[42];
private String[] monthNames = new String[12];
private TextView tvMsg1;
// private TextView tvMsg2;
// private TextView tvMsg3;
private int dayColor;
private int innerGridColor;
private int prevNextMonthDayColor;
private int currentDayColor;
private int todayColor;
private int todayBackgroundColor;
private int sundaySaturdayPrevNextMonthDayColor;
private float daySize;
private float dayTopOffset;
private float currentDaySize;
private float cellX = -1, cellY = -1;
// 从0开始
private int currentRow, currentCol;
private boolean redrawForKeyDown = false;
//当前的年和月
public int currentYear, currentMonth;
// 上月或者下个月选中的天数
public int currentDay = -1, currentDay1 = -1, currentDayIndex = -1;
private java.util.Calendar calendar = java.util.Calendar.getInstance();
public Date getSelectDate(){
if(!isBefore){
return null;
}
return selectDate;
}
public void setCurrentRow(int currentRow)
{
if (currentRow < 0)
{
currentMonth--;
if (currentMonth == -1)
{
currentMonth = 11;
currentYear--;
}
currentDay = getMonthDays(currentYear, currentMonth) + currentDay
- 7;
currentDay1 = currentDay;
cellX = -1;
cellX = -1;
view.invalidate();
return;
}
else if (currentRow > 5)
{
int n = 0;
for (int i = 35; i < days.length; i++)
{
if (!days[i].startsWith("*"))
n++;
else
break;
}
currentDay = 7 - n + currentCol + 1;
currentDay1 = currentDay;
currentMonth++;
if (currentMonth == 12)
{
currentMonth = 0;
currentYear++;
}
cellX = -1;
cellX = -1;
view.invalidate();
return;
}
this.currentRow = currentRow;
redrawForKeyDown = true;
view.invalidate();
}
private void getRecordDays()
{
int beginIndex = 8;
int endIndex = 7;
int beginDayIndex = 0;
if (currentMonth > 9)
{
beginIndex = 9;
endIndex = 8;
}
for (int i = 0; i < recordDays.length; i++)
recordDays[i] = false;
for (int i = 0; i < days.length; i++)
{
if (!days[i].startsWith("*"))
{
beginDayIndex = i;
break;
}
}
if (days[0].startsWith("*"))
{
int prevYear = currentYear, prevMonth = currentMonth - 1;
if (prevMonth == -1)
{
prevMonth = 11;
prevYear--;
}
int minDay = Integer.parseInt(days[0].substring(1));
}
if (days[days.length - 1].startsWith("*"))
{
int nextYear = currentYear, nextMonth = currentMonth + 1;
if (nextMonth == 12)
{
nextMonth = 0;
nextYear++;
}
int maxDay = Integer.parseInt(days[days.length - 1].substring(1));
}
}
public void setCurrentCol(int currentCol)
{
if (currentCol < 0)
{
if (currentRow == 0)
{
currentMonth--;
if (currentMonth == -1)
{
currentMonth = 11;
currentYear--;
}
currentDay = getMonthDays(currentYear, currentMonth);
currentDay1 = currentDay;
cellX = -1;
cellX = -1;
view.invalidate();
return;
}
else
{
currentCol = 6;
setCurrentRow(--currentRow);
}
}
else if (currentCol > 6)
{
currentCol = 0;
setCurrentRow(++currentRow);
}
this.currentCol = currentCol;
redrawForKeyDown = true;
view.invalidate();
}
public int getCurrentRow()
{
return currentRow;
}
public int getCurrentCol()
{
return currentCol;
}
public void setCellX(float cellX)
{
this.cellX = cellX;
}
public void setCellY(float cellY)
{
this.cellY = cellY;
}
private int getMonthDays(int year, int month)
{
month++;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
return 31;
}
case 4:
case 6:
case 9:
case 11:
{
return 30;
}
case 2:
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
return 29;
else
return 28;
}
}
return 0;
}
private void calculateDays()
{
calendar.set(currentYear, currentMonth, 1);
int week = calendar.get(calendar.DAY_OF_WEEK);
int monthDays = 0;
int prevMonthDays = 0;
monthDays = getMonthDays(currentYear, currentMonth);
if (currentMonth == 0)
prevMonthDays = getMonthDays(currentYear - 1, 11);
else
prevMonthDays = getMonthDays(currentYear, currentMonth - 1);
for (int i = week, day = prevMonthDays; i > 1; i--, day--)
{
days[i - 2] = "*" + String.valueOf(day);
}
for (int day = 1, i = week - 1; day <= monthDays; day++, i++)
{
days[i] = String.valueOf(day);
if (day == currentDay)
{
currentDayIndex = i;
}
}
for (int i = week + monthDays - 1, day = 1; i < days.length; i++, day++)
{
days[i] = "*" + String.valueOf(day);
}
}
public Grid(Activity activity, View view)
{
super(activity, view);
tvMsg1 = (TextView) activity.findViewById(R.id.data);
// tvMsg2 = (TextView) activity.findViewById(R.id.month);
dayColor = activity.getResources().getColor(R.color.day_color);//白色
todayColor = activity.getResources().getColor(R.color.today_color);
todayBackgroundColor = activity.getResources().getColor(
R.color.today_background_color);
innerGridColor = activity.getResources().getColor(
R.color.inner_grid_color);
prevNextMonthDayColor = activity.getResources().getColor(
R.color.prev_next_month_day_color);
currentDayColor = activity.getResources().getColor(
R.color.current_day_color);//星期六、日文字颜色
sundaySaturdayPrevNextMonthDayColor = activity.getResources().getColor(
R.color.sunday_saturday_prev_next_month_day_color);
daySize = activity.getResources().getDimension(R.dimen.day_size);
dayTopOffset = activity.getResources().getDimension(
R.dimen.day_top_offset);
currentDaySize = activity.getResources().getDimension(
R.dimen.current_day_size);
// monthNames = activity.getResources().getStringArray(R.array.month_name);
paint.setColor(activity.getResources().getColor(R.color.border_color));
currentYear = calendar.get(calendar.YEAR);
currentMonth = calendar.get(calendar.MONTH);
}
private boolean isCurrentDay(int dayIndex, int currentDayIndex,
Rect cellRect)
{
boolean result = false;
if (redrawForKeyDown == true)
{
result = dayIndex == (7 * ((currentRow > 0) ? currentRow : 0) + currentCol);
if (result)
redrawForKeyDown = false;
}
else if (cellX != -1 && cellY != -1)
{
if (cellX >= cellRect.left && cellX <= cellRect.right
&& cellY >= cellRect.top && cellY <= cellRect.bottom)
{
result = true;
}
else
{
result = false;
}
}
else
{
result = (dayIndex == currentDayIndex);
}
if (result)
{
if (currentRow > 0 && currentRow < 6)
{
currentDay1 = currentDay;
}
currentDayIndex = -1;
cellX = -1;
cellY = -1;
}
return result;
}
// 更新当前
- 1
- 2
前往页