package com.pwp.activity; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; import android.graphics.Color; import android.graphics.Typeface; import android.text.SpannableString; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.text.style.StyleSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.pwp.calendar.LunarCalendar; import com.pwp.calendar.SpecialCalendar; import com.pwp.dao.ScheduleDAO; import com.pwp.vo.ScheduleDateTag; /** * * @author lilin * @date 2013-2-6 上午8:57:30 * @annotation 日历适配器 */ public class MyCalendarAdp extends BaseAdapter { private ScheduleDAO dao = null; private boolean isLeapyear = false; // 是否为闰年 private int allDaysOfMonth = 0; // 某月的天数 private int dayOfWeek = 0; // 具体某一天是星期几 private int allDaysOfLastMonth = 0; // 上一个月的总天数 private Context context; // 7行7列 private String[] dayNumber = new String[49]; // 一个gridview中的日期存入此数组中 private static String week[] = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; private SpecialCalendar sc = null; private LunarCalendar lc = null; private Resources res = null; private String currentYear = ""; private String currentMonth = ""; @SuppressWarnings("unused") private String currentDay = ""; @SuppressLint("SimpleDateFormat") private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d"); private int currentFlag = -1; // 用于标记当天 private int[] schDateTagFlag = null; // 存储当月所有的日程日期 private String showYear = ""; // 用于在头部显示的年份 private String showMonth = ""; // 用于在头部显示的月份 private String animalsYear = ""; private String leapMonth = ""; // 闰哪一个月 private String cyclical = ""; // 天干地支 // 系统当前时间 private String sysDate = ""; private String sys_year = ""; private String sys_month = ""; private String sys_day = ""; // 日程时间(需要标记的日程日期) // private String sch_year = ""; // private String sch_month = ""; // private String sch_day = ""; public MyCalendarAdp() { Date date = new Date(); sysDate = sdf.format(date); // 当期日期 sys_year = sysDate.split("-")[0]; sys_month = sysDate.split("-")[1]; sys_day = sysDate.split("-")[2]; } public MyCalendarAdp(Context context, Resources rs, int jumpMonth, int jumpYear, int year_c, int month_c, int day_c) { this(); this.context = context; sc = new SpecialCalendar(); lc = new LunarCalendar(); this.res = rs; int stepYear = year_c + jumpYear; int stepMonth = month_c + jumpMonth; if (stepMonth > 0) { // 往下一个月滑动 if (stepMonth % 12 == 0) { stepYear = year_c + stepMonth / 12 - 1; stepMonth = 12; } else { stepYear = year_c + stepMonth / 12; stepMonth = stepMonth % 12; } } else { // 往上一个月滑动 stepYear = year_c - 1 + stepMonth / 12; stepMonth = stepMonth % 12 + 12; if (stepMonth % 12 == 0) { } } currentYear = String.valueOf(stepYear);// 得到当前的年份 currentMonth = String.valueOf(stepMonth); // 得到本月 // (jumpMonth为滑动的次数,每滑动一次就增加一月或减一月) currentDay = String.valueOf(day_c); // 得到当前日期是哪天 getCalendar(Integer.parseInt(currentYear), Integer.parseInt(currentMonth)); } public MyCalendarAdp(Context context, Resources rs, int year, int month, int day) { this(); this.context = context; sc = new SpecialCalendar(); lc = new LunarCalendar(); this.res = rs; currentYear = String.valueOf(year);// 得到跳转到的年份 currentMonth = String.valueOf(month); // 得到跳转到的月份 currentDay = String.valueOf(day); // 得到跳转到的天 getCalendar(Integer.parseInt(currentYear), Integer.parseInt(currentMonth)); } @Override public int getCount() { return dayNumber.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate( R.layout.mycalendar_gridview_item, null); } TextView toptext = (TextView) convertView.findViewById(R.id.toptext); String topStr = dayNumber[position].split("\\.")[0];// 上部显示的信息,日期或者阳历 String bottonStr = dayNumber[position].split("\\.")[1];// 下部显示的信息,空值或者阴历 // 设置显示的字体:为自定义 Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Helvetica.ttf"); toptext.setTypeface(typeface); SpannableString mSpannableString = new SpannableString(topStr + "\n" + bottonStr);// 分为上下 mSpannableString.setSpan( new StyleSpan(android.graphics.Typeface.NORMAL), 0, topStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置字体大小(相对值,单位:像素) 参数表示为默认字体大小的多少倍 // 0.5f表示默认字体大小的一半 // 2.0f表示默认字体大小的两倍 // 设置上部字体的大小 mSpannableString.setSpan(// new RelativeSizeSpan(1.5f),// 0, // topStr.length(),// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE// );// // 设置下部字体的大小 if (bottonStr != null || bottonStr != "") { mSpannableString.setSpan(new RelativeSizeSpan(0.5f), topStr.length() + 1, dayNumber[position].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } toptext.setTextColor(Color.WHITE);// 设置其他月份的字体为白色,就不可见了 // 设置周显示的字体颜色和大小 if (position < 7) { toptext.setTextColor(Color.BLACK); toptext.setTextSize(10); // 设置粗体 // TextPaint tp = toptext .getPaint(); // tp.setFakeBoldText(true); } // 设置非当月,周六,日显示的颜色 // if (position >= 7) { // if (position % 7 == 0 || position % 7 == 6) { // textView.setTextColor(res.getColor(R.color.weekcolor)); // } // } // 当前月信息显示 if (position < allDaysOfMonth + dayOfWeek + 7 && position >= dayOfWeek + 7) { toptext.setTextColor(Color.BLACK); toptext.setBackgroundResource(R.drawable.mycalendar_gridview_bg); // 设置周六,日显示的颜色 if (position % 7 == 0 || position % 7 == 6) { toptext.setTextColor(res.getColor(R.color.weekcolor)); } toptext.setTextSize(18); // 设置下部信息,前景色 mSpannableString.setSpan(new ForegroundColorSpan(Color.BLACK), topStr.length() + 1, dayNumber[position].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); if (isFestivial(bottonStr)) { mSpannableString.setSpan(new ForegroundColorSpan(Color.RED), topStr.length() + 1, dayNumber[position].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } // 设置有日程的显示 if (schDateTagFlag != null && schDateTagFlag.length > 0) { for (int i = 0; i < schDateTagFlag.length; i++) { if (schDateTagFlag[i] == position) { // textView.setBackgroundResource(R.drawable.mark); toptext.setTextColor(Color.BLUE); } } } // 设置当天的背景 if (currentFlag == position) { toptext.setBackgroundResource(R.drawable.mark); } toptext.setText(mSpannableString); return convertView; } // 是否是节假日 private boolean isFestivial(String str) { if (str.contains("一") || str.contains("二") || str.contains("三") || str.contains("四") || str.contains("五") || str.contains("六") || str.contains("七") || str.contains("八") || str.contains("九") || str.contains("十")) { return false; } else { return true; } } // 得到某年的某月的天数且这月的第一天是星期几 public void getCalendar(int year, int month) { isLeapyear = sc.isLeapYear(year); // 是否为闰年 allDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month); // 某月的总天数 dayOfWeek = sc.getWeekdayOfMonth(year, month); // 某月第一天为星期几 allDaysOfLastMonth = sc.getDaysOfMonth(isLeapyear, month - 1); // 上一个月的总天数 // Log.d("andli", "DAY===" + isLeapyear + " ====== " + allDaysOfMonth // + " ============ " + dayOfWeek + " ========= " // + allDaysOfLastMonth); getweek(year, month); } // 将一个月中的每一天的值添加入数组dayNuber中 private void getweek(int year, int month) { int j = 1; int flag = 0; String lunarDay = ""; // 得到当前月的所有日程日期(这些日期需要标记) dao = new ScheduleDAO(context); ArrayList<ScheduleDateTag> dateTagList = dao.getTagDate(year, month); if (dateTagList != null && dateTagList.size() > 0) { schDateTagFlag = new int[dateTagList.size()]; } for (int i = 0; i < dayNumber.length; i++) { // 周一 if (i < 7) { dayNumber[i] = week[i] + "." + " "; } else if (i < dayOfWeek + 7) { // 前一个月 int temp = allDaysOfLastMonth - dayOfWeek + 1 - 7; lunarDay = lc.getLunarDate(year, month - 1, temp + i, false); dayNumber[i] = (temp + i) + "." + lunarDay; } else if (i < allDaysOfMonth + dayOfWeek + 7) { // 本月 String day = String.valueOf(i - dayOfWeek + 1 - 7); // 得到的日期 lunarDay = lc.getLunarDate(year, month, i - dayOfWeek + 1 - 7, false); dayNumber[i] = i - dayOfWeek + 1 - 7 + "." + lunarDay; // 对于当前月才去标记当前日期 if (sys_year.equals(String.valueOf(year)) && sys_month.equals(String.valueOf(month)) && sys_day.equals(day)) { // 笔记当前日期 currentFlag = i; } // 标记日程日期 if (dateTagList != null && dateTagList.size() > 0) { for (int m = 0; m < dateTagList.size(); m++) { ScheduleDateTag dateTag = dateTagList.get(m); int matchYear = dateTag.getYear(); int matchMonth = dateTag.getMonth(); int matchDay = dateTag.getDay(); if (matchYear == year && matchMonth == month && matchDay == Integer.parseInt(day)) { schDateTagFlag[flag] = i; flag++; } } } setShowYear(String.valueOf(year)); setShowMonth(String.valueOf(month)); setAnimalsYear(lc.animalsYear(year)); setLeapMonth(lc.leapMonth == 0 ? "" : String .valueOf(lc.leapMonth)); setCyclical(lc.cyclical(year)); } else { // 下一个月 lunarDay = lc.getLunarDate(year, month + 1, j, false); dayNumber[i] = j + "." + lunarDay; j++; } } // String abc = ""; // for (int i = 0; i < dayNumber.length; i++) { // abc = abc + dayNumber[i] + ":"; // } // Log.i("andli", abc); } public void matchScheduleDate(int year, int month, int day) { } /** * 点击每一个item时返回item中的日期 * * @param position * @return */ public String getDateByClickItem(int position) { return dayNumber[position]; } /** * 在点击gridView时,得到这个月中第一天的位置 * * @return */ public int getStartPositon() { return dayOfWeek + 7; } /** * 在点击gridView时,得到这个月中最后一天的位置 * * @return */ public int getEndPosition() { return (dayOfWeek + allDaysOfMonth + 7) - 1; } public String getShowYear() { return showYear; } public void setShowYear(String showYear) { this.showYear = showYear; } public String getShowMonth() { return showMonth; } public void setShowMonth(String showMonth) { this.showMonth = showMonth; } public String getAnimalsYear() { return animalsYear; } public void setAnimalsYear(String animalsYear) { this.animalsYear = animalsYear; } public String getLeapMonth() { return leapMonth; } public void setLeapMonth(String leapMonth) { this.leapMonth = leapMonth; } public String getCyclical() { return cyclical; } public void setCyclical(String cyclical) { this.cyclical = cyclical; } }