1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
| class MonthView extends View {
public static final String VIEW_PARAMS_HEIGHT = "height"; public static final String VIEW_PARAMS_MONTH = "month"; public static final String VIEW_PARAMS_YEAR = "year"; public static final String VIEW_PARAMS_SELECTED_BEGIN_DAY = "selected_begin_day";
public static final String VIEW_PARAMS_SELECTED_BEGIN_MONTH = "selected_begin_month";
public static final String VIEW_PARAMS_SELECTED_BEGIN_YEAR = "selected_begin_year";
public static final String VIEW_PARAMS_WEEK_START = "week_start";
protected int DEFAULT_HEIGHT = DensityUtil.dip2px(getContext(),16); protected static final int DEFAULT_NUM_ROWS = 6; protected static int DAY_SELECTED_CIRCLE_SIZE; protected int DAY_SEPARATOR_WIDTH = DensityUtil.dip2px(getContext(),33); protected static int MINI_DAY_NUMBER_TEXT_SIZE; protected int MIN_HEIGHT = DensityUtil.dip2px(getContext(),3); protected static int MONTH_LINE_SIZE; protected static int MONTH_HEADER_SIZE; protected static int MONTH_TITLE_TEXT_SIZE;
protected int mPadding = dip2px(getContext(),10);
private String mMonthTitleTypeface; protected Paint flagTextPaint; protected Paint flagPreBgPaint; protected Paint flagNormalBgPaint; protected Paint mMonthLinePaint; protected Paint mMonthNumPaint; protected Paint mMonthTitlePaint; protected Paint todayCiclePaint; protected Paint mSelectedCirclePaint;
protected int mMonthTitleColor; protected int mMonthLineColor; protected int mNormalDayColor; protected int mPreviousDayColor; protected int mSelectedBgColor; protected int mSelectedTextColor; protected int todayTextColor; protected int flagPreBgColor; protected int flagNormalBgColor; protected int flagTextColor; protected String flag; private final StringBuilder mStringBuilder;
protected boolean mHasToday = false; protected boolean mIsPrev = false; protected int mSelectedBeginDay = -1; protected int mSelectedBeginMonth = -1; protected int mSelectedBeginYear = -1; private List<String> dates; protected int mToday = -1; protected int mWeekStart = 1; protected int mNumDays = 7; protected int mNumCells = mNumDays; private int mDayOfWeekStart = 0; protected int mMonth; protected Boolean mDrawRect; protected int mRowHeight = DEFAULT_HEIGHT; protected int mWidth; protected int mYear; final Time today;
private final Calendar mCalendar;
private int mNumRows = DEFAULT_NUM_ROWS;
private OnDayClickListener mOnDayClickListener;
public MonthView(Context context, TypedArray typedArray) { super(context);
Resources resources = context.getResources(); mCalendar = Calendar.getInstance(); today = new Time(Time.getCurrentTimezone()); today.setToNow(); mMonthTitleTypeface = resources.getString(R.string.sans_serif); mMonthTitleColor = typedArray.getColor(R.styleable.MonthCalendarView_monthTitleColor, ContextCompat.getColor(context,R.color.normal_day)); mMonthLineColor = typedArray.getColor(R.styleable.MonthCalendarView_monthLineColor, ContextCompat.getColor(context,R.color.normal_day)); mNormalDayColor = typedArray.getColor(R.styleable.MonthCalendarView_normalDayTextColor_month, ContextCompat.getColor(context,R.color.normal_day)); mPreviousDayColor = typedArray.getColor(R.styleable.MonthCalendarView_previousDayTextColor_month, ContextCompat.getColor(context,R.color.normal_day)); mSelectedBgColor = typedArray.getColor(R.styleable.MonthCalendarView_selectedBgColor_month, ContextCompat.getColor(context,R.color.selected_day_background)); mSelectedTextColor = typedArray.getColor(R.styleable.MonthCalendarView_selectedTextColor_month, ContextCompat.getColor(context,R.color.white)); todayTextColor = typedArray.getColor(R.styleable.MonthCalendarView_todayTextColor_month,ContextCompat.getColor(context,R.color.default_blue)); flagTextColor= typedArray.getColor(R.styleable.MonthCalendarView_flagTextColor_month,ContextCompat.getColor(context,R.color.white)); flagPreBgColor=typedArray.getColor(R.styleable.MonthCalendarView_flagPreBgColor_month,ContextCompat.getColor(context,R.color.white)); flagNormalBgColor=typedArray.getColor(R.styleable.MonthCalendarView_flagNormalBgColor_month,ContextCompat.getColor(context,R.color.default_orange)); mDrawRect = typedArray.getBoolean(R.styleable.MonthCalendarView_isRoundRect_month, false); int preMonthNum = typedArray.getInteger(R.styleable.MonthCalendarView_preMonthNum_month,2); DateUtil.setPreMonthNum(preMonthNum); int nextMonthNum = typedArray.getInteger(R.styleable.MonthCalendarView_nextMonthNum_month,2); DateUtil.setNextMonthNum(nextMonthNum);
flag = typedArray.getString(R.styleable.MonthCalendarView_flagTextStr_month); if(TextUtils.isEmpty(flag)){ flag="行"; } mStringBuilder = new StringBuilder(50);
MINI_DAY_NUMBER_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthCalendarView_dayTextSize_month, resources.getDimensionPixelSize(R.dimen.text_size_day)); MONTH_TITLE_TEXT_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthCalendarView_monthTitleTextSize, resources.getDimensionPixelSize(R.dimen.text_size_month)); MONTH_LINE_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthCalendarView_monthLineTextSize, resources.getDimensionPixelSize(R.dimen.text_size_day_name)); MONTH_HEADER_SIZE = typedArray.getDimensionPixelOffset(R.styleable.MonthCalendarView_headerMonthHeight, resources.getDimensionPixelOffset(R.dimen.header_month_height)); DAY_SELECTED_CIRCLE_SIZE = typedArray.getDimensionPixelSize(R.styleable.MonthCalendarView_selectedDayBgRadius, resources.getDimensionPixelOffset(R.dimen.selected_day_radius));
mRowHeight = ((typedArray.getDimensionPixelSize(R.styleable.MonthCalendarView_calendarHeight, resources.getDimensionPixelOffset(R.dimen.calendar_height)) - MONTH_HEADER_SIZE) / 6);
initView();
}
public void setFlagDates(List<String> flagDates){ this.dates=flagDates; }
private int calculateNumRows() { int offset = findDayOffset(); int dividend = (offset + mNumCells) / mNumDays; int remainder = (offset + mNumCells) % mNumDays; return (dividend + (remainder > 0 ? 1 : 0)); }
private void drawMonthTitle(Canvas canvas) { StringBuilder stringBuilder = new StringBuilder(getMonthAndYearString().toLowerCase()); stringBuilder.setCharAt(0, Character.toUpperCase(stringBuilder.charAt(0))); Rect rect=new Rect(); mMonthTitlePaint.getTextBounds(String.valueOf(stringBuilder),0,stringBuilder.length(),rect); int x = mWidth / mNumDays -mPadding; int y = (MONTH_HEADER_SIZE - MONTH_LINE_SIZE) / 2 ; canvas.drawLine(0, y-rect.height()-MIN_HEIGHT, mWidth, y-rect.height()-MIN_HEIGHT, mMonthLinePaint); canvas.drawText(stringBuilder.toString(), x, y, mMonthTitlePaint); canvas.drawLine(0, y+MIN_HEIGHT, mWidth, y+MIN_HEIGHT, mMonthLinePaint); }
private int findDayOffset() { return (mDayOfWeekStart < mWeekStart ? (mDayOfWeekStart + mNumDays) : mDayOfWeekStart) - mWeekStart; }
private String getMonthAndYearString() { int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_MONTH_DAY; mStringBuilder.setLength(0); long millis = mCalendar.getTimeInMillis(); return DateUtils.formatDateRange(getContext(), millis, millis, flags); }
private void onDayClick(CalendarDay calendarDay) { if (mOnDayClickListener != null) { mOnDayClickListener.onDayClick(this, calendarDay); } }
private boolean sameDay(int monthDay, Time time) { return (mYear == time.year) && (mMonth == time.month) && (monthDay == time.monthDay); }
private boolean prevDay(int monthDay, Time time) { return ((mYear < time.year)) || (mYear == time.year && mMonth < time.month) || (mMonth == time.month && monthDay < time.monthDay); }
protected void drawMonthNums(Canvas canvas) {
int y = (mRowHeight + MINI_DAY_NUMBER_TEXT_SIZE) / 2 - DAY_SEPARATOR_WIDTH + MONTH_HEADER_SIZE; int paddingDay = (mWidth - 2 * mPadding) / (2 * mNumDays); int dayOffset = findDayOffset(); int day = 1;
while (day <= mNumCells) { int x = paddingDay * (1 + dayOffset * 2) + mPadding;
String dayStr= String.format("%d", day); Rect dayRect=new Rect(); mMonthTitlePaint.getTextBounds(dayStr,0,dayStr.length(),dayRect); Rect flagRect=new Rect(); mMonthTitlePaint.getTextBounds(flag,0,flag.length(),flagRect);
if (prevDay(day, today)) { mMonthNumPaint.setColor(mPreviousDayColor); if(isFlag(mYear,mMonth,day)){ canvas.drawRect(x-flagRect.width()/2-MIN_HEIGHT/2,y+DEFAULT_HEIGHT-MIN_HEIGHT/2,x+flagRect.width()/2+MIN_HEIGHT/2,y+flagRect.height()+DEFAULT_HEIGHT+MIN_HEIGHT/2,flagPreBgPaint); canvas.drawText(flag, x-flagRect.width()/2-MIN_HEIGHT/2, y+flagRect.height()+DEFAULT_HEIGHT, flagTextPaint); } }else{ if (mHasToday && (mToday == day)) { mMonthNumPaint.setColor(getResources().getColor(R.color.default_blue)); mMonthNumPaint.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); if (mDrawRect) { RectF rectF = new RectF(x - DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) - DAY_SELECTED_CIRCLE_SIZE, x + DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) + DAY_SELECTED_CIRCLE_SIZE); canvas.drawRoundRect(rectF, 10.0f, 10.0f, todayCiclePaint); }else{ canvas.drawCircle(x, y - MINI_DAY_NUMBER_TEXT_SIZE / 3, DAY_SELECTED_CIRCLE_SIZE, todayCiclePaint); } } else { mMonthNumPaint.setColor(mNormalDayColor); mMonthNumPaint.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL)); } if(isFlag(mYear,mMonth,day)){ canvas.drawRect(x-flagRect.width()/2-MIN_HEIGHT/2,y+DEFAULT_HEIGHT-MIN_HEIGHT/2,x+flagRect.width()/2+MIN_HEIGHT/2,y+flagRect.height()+DEFAULT_HEIGHT+MIN_HEIGHT/2,flagNormalBgPaint); canvas.drawText(flag, x-flagRect.width()/2-MIN_HEIGHT/2, y+flagRect.height()+DEFAULT_HEIGHT, flagTextPaint); }
} if ((mMonth == mSelectedBeginMonth && mSelectedBeginDay == day && mSelectedBeginYear == mYear) ) { mMonthNumPaint.setColor(mSelectedTextColor); if (mDrawRect) { RectF rectF = new RectF(x - DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) - DAY_SELECTED_CIRCLE_SIZE, x + DAY_SELECTED_CIRCLE_SIZE, (y - MINI_DAY_NUMBER_TEXT_SIZE / 3) + DAY_SELECTED_CIRCLE_SIZE); canvas.drawRoundRect(rectF, 10.0f, 10.0f, mSelectedCirclePaint); } else{ canvas.drawCircle(x, y - MINI_DAY_NUMBER_TEXT_SIZE / 3, DAY_SELECTED_CIRCLE_SIZE, mSelectedCirclePaint); } }
canvas.drawText(dayStr, x, y, mMonthNumPaint); dayOffset++; if (dayOffset == mNumDays) { dayOffset = 0; y += mRowHeight; } day++; } }
private boolean isFlag(int year,int month,int day){ if(dates!=null){ Calendar c = Calendar.getInstance(); c.set(year, month, day); Date curDay=c.getTime(); Iterator<String> it=dates.iterator(); while(it.hasNext()){ String date=it.next(); String curDayStr=OtherUtils.formatDate(curDay,"yyyy-MM-dd"); if(date.equals(curDayStr)){ return true; } } return false; }else{ return false; }
}
public CalendarDay getDayFromLocation(float x, float y) { int padding = mPadding; if ((x < padding) || (x > mWidth - mPadding)) { return null; }
int yDay = (int) (y - MONTH_HEADER_SIZE + DAY_SEPARATOR_WIDTH) / mRowHeight; int day = 1 + ((int) ((x - padding) * mNumDays / (mWidth - padding - mPadding)) - findDayOffset()) + yDay * mNumDays;
if (mMonth > 11 || mMonth < 0 || CalendarUtils.getDaysInMonth(mMonth, mYear) < day || day < 1) return null;
return new CalendarDay(mYear, mMonth, day); }
protected void initView() { mMonthTitlePaint = new Paint(); mMonthTitlePaint.setAntiAlias(true); mMonthTitlePaint.setTextSize(MONTH_TITLE_TEXT_SIZE); mMonthTitlePaint.setTypeface(Typeface.create(mMonthTitleTypeface, Typeface.BOLD)); mMonthTitlePaint.setColor(mMonthTitleColor); mMonthTitlePaint.setTextAlign(Align.CENTER); mMonthTitlePaint.setStyle(Style.FILL);
mSelectedCirclePaint = new Paint(); mSelectedCirclePaint.setFakeBoldText(true); mSelectedCirclePaint.setAntiAlias(true); mSelectedCirclePaint.setColor(mSelectedBgColor); mSelectedCirclePaint.setTextAlign(Align.CENTER); mSelectedCirclePaint.setStyle(Style.FILL);
mMonthLinePaint = new Paint(); mMonthLinePaint.setAntiAlias(true); mMonthLinePaint.setTextSize(MONTH_LINE_SIZE); mMonthLinePaint.setColor(mMonthLineColor); mMonthLinePaint.setStyle(Style.FILL); mMonthLinePaint.setTextAlign(Align.CENTER); mMonthNumPaint = new Paint(); mMonthNumPaint.setAntiAlias(true); mMonthNumPaint.setTextSize(MINI_DAY_NUMBER_TEXT_SIZE); mMonthNumPaint.setStyle(Style.FILL); mMonthNumPaint.setTextAlign(Align.CENTER); mMonthNumPaint.setFakeBoldText(false);
flagTextPaint=new Paint(); flagTextPaint.setAntiAlias(true); flagTextPaint.setColor(flagTextColor); flagTextPaint.setTextSize(MONTH_LINE_SIZE); flagNormalBgPaint=new Paint(); flagNormalBgPaint.setAntiAlias(true); flagNormalBgPaint.setColor(flagNormalBgColor);
flagPreBgPaint=new Paint(); flagPreBgPaint.setAntiAlias(true); flagPreBgPaint.setColor(flagPreBgColor);
todayCiclePaint=new Paint(); todayCiclePaint.setAntiAlias(true); todayCiclePaint.setStrokeWidth(4); todayCiclePaint.setColor(todayTextColor); todayCiclePaint.setStyle(Style.STROKE); }
protected void onDraw(Canvas canvas) { drawMonthTitle(canvas);
drawMonthNums(canvas); }
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mRowHeight * mNumRows + MONTH_HEADER_SIZE); }
protected void onSizeChanged(int w, int h, int oldw, int oldh) { mWidth = w; }
public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { CalendarDay calendarDay = getDayFromLocation(event.getX(), event.getY()); if (calendarDay != null) { onDayClick(calendarDay); } } return true; }
public void reuse() { mNumRows = DEFAULT_NUM_ROWS; requestLayout(); }
public void setMonthParams(HashMap<String, Integer> params) { if (!params.containsKey(VIEW_PARAMS_MONTH) && !params.containsKey(VIEW_PARAMS_YEAR)) { throw new InvalidParameterException("You must specify month and year for this view"); } setTag(params);
if (params.containsKey(VIEW_PARAMS_HEIGHT)) { mRowHeight = params.get(VIEW_PARAMS_HEIGHT); if (mRowHeight < MIN_HEIGHT) { mRowHeight = MIN_HEIGHT; } } if (params.containsKey(VIEW_PARAMS_SELECTED_BEGIN_DAY)) { mSelectedBeginDay = params.get(VIEW_PARAMS_SELECTED_BEGIN_DAY); }
if (params.containsKey(VIEW_PARAMS_SELECTED_BEGIN_MONTH)) { mSelectedBeginMonth = params.get(VIEW_PARAMS_SELECTED_BEGIN_MONTH); }
if (params.containsKey(VIEW_PARAMS_SELECTED_BEGIN_YEAR)) { mSelectedBeginYear = params.get(VIEW_PARAMS_SELECTED_BEGIN_YEAR); }
mMonth = params.get(VIEW_PARAMS_MONTH); mYear = params.get(VIEW_PARAMS_YEAR);
mHasToday = false; mToday = -1;
mCalendar.set(Calendar.MONTH, mMonth); mCalendar.set(Calendar.YEAR, mYear); mCalendar.set(Calendar.DAY_OF_MONTH, 1); mDayOfWeekStart = mCalendar.get(Calendar.DAY_OF_WEEK);
if (params.containsKey(VIEW_PARAMS_WEEK_START)) { mWeekStart = params.get(VIEW_PARAMS_WEEK_START); } else { mWeekStart = mCalendar.getFirstDayOfWeek(); }
mNumCells = CalendarUtils.getDaysInMonth(mMonth, mYear); for (int i = 0; i < mNumCells; i++) { final int day = i + 1; if (sameDay(day, today)) { mHasToday = true; mToday = day; }
mIsPrev = prevDay(day, today); }
mNumRows = calculateNumRows(); }
public void setOnDayClickListener(OnDayClickListener onDayClickListener) { mOnDayClickListener = onDayClickListener; }
public interface OnDayClickListener { void onDayClick(MonthView monthView, CalendarDay calendarDay); }
public static int dip2px(Context context, float dpValue) { int scale = (int) context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); }
}
|