@@ -43,8 +43,6 @@ public class WeekView extends View {
43
43
@ Deprecated
44
44
public static final int LENGTH_LONG = 2 ;
45
45
private final Context mContext ;
46
- private Calendar mToday ;
47
- private Calendar mStartDate ;
48
46
private Paint mTimeTextPaint ;
49
47
private float mTimeTextWidth ;
50
48
private float mTimeTextHeight ;
@@ -60,6 +58,11 @@ public class WeekView extends View {
60
58
private Paint mHourSeparatorPaint ;
61
59
private float mHeaderMarginBottom ;
62
60
private Paint mTodayBackgroundPaint ;
61
+ private Paint mFutureBackgroundPaint ;
62
+ private Paint mPastBackgroundPaint ;
63
+ private Paint mFutureWeekendBackgroundPaint ;
64
+ private Paint mPastWeekendBackgroundPaint ;
65
+ private Paint mNowLinePaint ;
63
66
private Paint mTodayHeaderTextPaint ;
64
67
private Paint mEventBackgroundPaint ;
65
68
private float mHeaderColumnWidth ;
@@ -84,6 +87,13 @@ public class WeekView extends View {
84
87
private int mHeaderRowPadding = 10 ;
85
88
private int mHeaderRowBackgroundColor = Color .WHITE ;
86
89
private int mDayBackgroundColor = Color .rgb (245 , 245 , 245 );
90
+ private int mPastBackgroundColor = Color .rgb (227 , 227 , 227 );
91
+ private int mFutureBackgroundColor = Color .rgb (245 , 245 , 245 );
92
+ private int mPastWeekendBackgroundColor = 0 ;
93
+ private int mFutureWeekendBackgroundColor = 0 ;
94
+ private int mNowLineColor = Color .rgb (102 , 102 , 102 );
95
+ private int mNowLineThickness = 5 ;
96
+ private boolean mUseNewColoring = false ;
87
97
private int mHourSeparatorColor = Color .rgb (230 , 230 , 230 );
88
98
private int mTodayBackgroundColor = Color .rgb (239 , 247 , 254 );
89
99
private int mHourSeparatorHeight = 2 ;
@@ -238,6 +248,13 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
238
248
mHeaderRowPadding = a .getDimensionPixelSize (R .styleable .WeekView_headerRowPadding , mHeaderRowPadding );
239
249
mHeaderRowBackgroundColor = a .getColor (R .styleable .WeekView_headerRowBackgroundColor , mHeaderRowBackgroundColor );
240
250
mDayBackgroundColor = a .getColor (R .styleable .WeekView_dayBackgroundColor , mDayBackgroundColor );
251
+ mFutureBackgroundColor = a .getColor (R .styleable .WeekView_futureBackgroundColor , mFutureBackgroundColor );
252
+ mPastBackgroundColor = a .getColor (R .styleable .WeekView_pastBackgroundColor , mPastBackgroundColor );
253
+ mFutureWeekendBackgroundColor = a .getColor (R .styleable .WeekView_futureWeekendBackgroundColor , mFutureBackgroundColor ); // If not set, use the same color as in the week
254
+ mPastWeekendBackgroundColor = a .getColor (R .styleable .WeekView_pastWeekendBackgroundColor , mPastBackgroundColor );
255
+ mNowLineColor = a .getColor (R .styleable .WeekView_nowLineColor , mNowLineColor );
256
+ mNowLineThickness = a .getDimensionPixelSize (R .styleable .WeekView_nowLineThickness , mNowLineThickness );
257
+ mUseNewColoring = a .getBoolean (R .styleable .WeekView_useNewColoringStyle , mUseNewColoring );
241
258
mHourSeparatorColor = a .getColor (R .styleable .WeekView_hourSeparatorColor , mHourSeparatorColor );
242
259
mTodayBackgroundColor = a .getColor (R .styleable .WeekView_todayBackgroundColor , mTodayBackgroundColor );
243
260
mHourSeparatorHeight = a .getDimensionPixelSize (R .styleable .WeekView_hourSeparatorHeight , mHourSeparatorHeight );
@@ -258,12 +275,6 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
258
275
}
259
276
260
277
private void init () {
261
- // Get the date today.
262
- mToday = Calendar .getInstance ();
263
- mToday .set (Calendar .HOUR_OF_DAY , 0 );
264
- mToday .set (Calendar .MINUTE , 0 );
265
- mToday .set (Calendar .SECOND , 0 );
266
-
267
278
// Scrolling initialization.
268
279
mGestureDetector = new GestureDetectorCompat (mContext , mGestureListener );
269
280
mScroller = new OverScroller (mContext );
@@ -296,13 +307,26 @@ private void init() {
296
307
// Prepare day background color paint.
297
308
mDayBackgroundPaint = new Paint ();
298
309
mDayBackgroundPaint .setColor (mDayBackgroundColor );
310
+ mFutureBackgroundPaint = new Paint ();
311
+ mFutureBackgroundPaint .setColor (mFutureBackgroundColor );
312
+ mPastBackgroundPaint = new Paint ();
313
+ mPastBackgroundPaint .setColor (mPastBackgroundColor );
314
+ mFutureWeekendBackgroundPaint = new Paint ();
315
+ mFutureWeekendBackgroundPaint .setColor (mFutureWeekendBackgroundColor );
316
+ mPastWeekendBackgroundPaint = new Paint ();
317
+ mPastWeekendBackgroundPaint .setColor (mPastWeekendBackgroundColor );
299
318
300
319
// Prepare hour separator color paint.
301
320
mHourSeparatorPaint = new Paint ();
302
321
mHourSeparatorPaint .setStyle (Paint .Style .STROKE );
303
322
mHourSeparatorPaint .setStrokeWidth (mHourSeparatorHeight );
304
323
mHourSeparatorPaint .setColor (mHourSeparatorColor );
305
324
325
+ // Prepare the "now" line color paint
326
+ mNowLinePaint = new Paint ();
327
+ mNowLinePaint .setStrokeWidth (mNowLineThickness );
328
+ mNowLinePaint .setColor (mNowLineColor );
329
+
306
330
// Prepare today background color paint.
307
331
mTodayBackgroundPaint = new Paint ();
308
332
mTodayBackgroundPaint .setColor (mTodayBackgroundColor );
@@ -327,7 +351,6 @@ private void init() {
327
351
mEventTextPaint .setStyle (Paint .Style .FILL );
328
352
mEventTextPaint .setColor (mEventTextColor );
329
353
mEventTextPaint .setTextSize (mEventTextSize );
330
- mStartDate = (Calendar ) mToday .clone ();
331
354
332
355
// Set default event color.
333
356
mDefaultEventColor = Color .parseColor ("#9fc6e7" );
@@ -378,10 +401,12 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
378
401
mWidthPerDay = getWidth () - mHeaderColumnWidth - mColumnGap * (mNumberOfVisibleDays - 1 );
379
402
mWidthPerDay = mWidthPerDay /mNumberOfVisibleDays ;
380
403
404
+ Calendar today = today ();
405
+
381
406
// If the week view is being drawn for the first time, then consider the first day of week.
382
407
if (mIsFirstDraw && mNumberOfVisibleDays >= 7 ) {
383
- if (mToday .get (Calendar .DAY_OF_WEEK ) != mFirstDayOfWeek ) {
384
- int difference = 7 + (mToday .get (Calendar .DAY_OF_WEEK ) - mFirstDayOfWeek );
408
+ if (today .get (Calendar .DAY_OF_WEEK ) != mFirstDayOfWeek ) {
409
+ int difference = 7 + (today .get (Calendar .DAY_OF_WEEK ) - mFirstDayOfWeek );
385
410
mCurrentOrigin .x += (mWidthPerDay + mColumnGap ) * difference ;
386
411
}
387
412
mIsFirstDraw = false ;
@@ -395,7 +420,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
395
420
float startPixel = startFromPixel ;
396
421
397
422
// Prepare to iterate for each day.
398
- Calendar day = (Calendar ) mToday .clone ();
423
+ Calendar day = (Calendar ) today .clone ();
399
424
day .add (Calendar .HOUR , 6 );
400
425
401
426
// Prepare to iterate for each hour to draw the hour lines.
@@ -412,18 +437,18 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
412
437
}
413
438
414
439
// Iterate through each day.
415
- mFirstVisibleDay = (Calendar ) mToday .clone ();
440
+ mFirstVisibleDay = (Calendar ) today .clone ();
416
441
mFirstVisibleDay .add (Calendar .DATE , leftDaysWithGaps );
417
442
for (int dayNumber = leftDaysWithGaps + 1 ;
418
443
dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays + 1 ;
419
444
dayNumber ++) {
420
445
421
446
// Check if the day is today.
422
- day = (Calendar ) mToday .clone ();
447
+ day = (Calendar ) today .clone ();
423
448
mLastVisibleDay = (Calendar ) day .clone ();
424
449
day .add (Calendar .DATE , dayNumber - 1 );
425
450
mLastVisibleDay .add (Calendar .DATE , dayNumber - 2 );
426
- boolean sameDay = isSameDay (day , mToday );
451
+ boolean sameDay = isSameDay (day , today );
427
452
428
453
// Get more events if necessary. We want to store the events 3 months beforehand. Get
429
454
// events only when it is the first iteration of the loop.
@@ -434,8 +459,27 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
434
459
435
460
// Draw background color for each day.
436
461
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel );
437
- if (mWidthPerDay + startPixel - start > 0 )
438
- canvas .drawRect (start , mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom , startPixel + mWidthPerDay , getHeight (), sameDay ? mTodayBackgroundPaint : mDayBackgroundPaint );
462
+ if (mWidthPerDay + startPixel - start > 0 ){
463
+ if (mUseNewColoring ){
464
+ boolean isWeekend = (day .get (Calendar .DAY_OF_WEEK ) == Calendar .SATURDAY || day .get (Calendar .DAY_OF_WEEK ) == Calendar .SUNDAY );
465
+ Paint pastPaint = isWeekend ? mPastWeekendBackgroundPaint : mPastBackgroundPaint ;
466
+ Paint futurePaint = isWeekend ? mFutureWeekendBackgroundPaint : mFutureBackgroundPaint ;
467
+ float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom + mCurrentOrigin .y ;
468
+
469
+ if (sameDay ){
470
+ Calendar now = Calendar .getInstance ();
471
+ float beforeNow = (now .get (Calendar .HOUR_OF_DAY )+now .get (Calendar .MINUTE )/60.0f )*mHourHeight ;
472
+ canvas .drawRect (start , startY , startPixel + mWidthPerDay , startY +beforeNow , pastPaint );
473
+ canvas .drawRect (start , startY +beforeNow , startPixel + mWidthPerDay , getHeight (), futurePaint );
474
+ } else if (day .before (today )) {
475
+ canvas .drawRect (start , startY , startPixel + mWidthPerDay , getHeight (), pastPaint );
476
+ } else {
477
+ canvas .drawRect (start , startY , startPixel + mWidthPerDay , getHeight (), futurePaint );
478
+ }
479
+ }else {
480
+ canvas .drawRect (start , mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom , startPixel + mWidthPerDay , getHeight (), sameDay ? mTodayBackgroundPaint : mDayBackgroundPaint );
481
+ }
482
+ }
439
483
440
484
// Prepare the separator lines for hours.
441
485
int i = 0 ;
@@ -456,6 +500,14 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
456
500
// Draw the events.
457
501
drawEvents (day , startPixel , canvas );
458
502
503
+ //Draw the line at the current time
504
+ if (mUseNewColoring && sameDay ){
505
+ float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom + mCurrentOrigin .y ;
506
+ Calendar now = Calendar .getInstance ();
507
+ float beforeNow = (now .get (Calendar .HOUR_OF_DAY )+now .get (Calendar .MINUTE )/60.0f )*mHourHeight ;
508
+ canvas .drawLine (start , startY +beforeNow , startPixel + mWidthPerDay , startY +beforeNow , mNowLinePaint );
509
+ }
510
+
459
511
// In the next iteration, start from the next day.
460
512
startPixel += mWidthPerDay + mColumnGap ;
461
513
}
@@ -467,9 +519,9 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
467
519
startPixel = startFromPixel ;
468
520
for (int dayNumber =leftDaysWithGaps +1 ; dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays + 1 ; dayNumber ++) {
469
521
// Check if the day is today.
470
- day = (Calendar ) mToday .clone ();
522
+ day = (Calendar ) today .clone ();
471
523
day .add (Calendar .DATE , dayNumber - 1 );
472
- boolean sameDay = isSameDay (day , mToday );
524
+ boolean sameDay = isSameDay (day , today );
473
525
474
526
// Draw the day labels.
475
527
String dayLabel = getDateTimeInterpreter ().interpretDate (day );
@@ -497,7 +549,7 @@ private Calendar getTimeFromPoint(float x, float y){
497
549
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel );
498
550
if (mWidthPerDay + startPixel - start > 0
499
551
&& x >start && x <startPixel + mWidthPerDay ){
500
- Calendar day = ( Calendar ) mToday . clone ();
552
+ Calendar day = today ();
501
553
day .add (Calendar .DATE , dayNumber - 1 );
502
554
float pixelsFromZero = y - mCurrentOrigin .y - mHeaderTextHeight
503
555
- mHeaderRowPadding * 2 - mTimeTextHeight /2 - mHeaderMarginBottom ;
@@ -1449,4 +1501,17 @@ private boolean isSameDay(Calendar dayOne, Calendar dayTwo) {
1449
1501
return dayOne .get (Calendar .YEAR ) == dayTwo .get (Calendar .YEAR ) && dayOne .get (Calendar .DAY_OF_YEAR ) == dayTwo .get (Calendar .DAY_OF_YEAR );
1450
1502
}
1451
1503
1504
+ /**
1505
+ * Returns a calendar instance at the start of this day
1506
+ * @return the calendar instance
1507
+ */
1508
+ private Calendar today (){
1509
+ Calendar today = Calendar .getInstance ();
1510
+ today .set (Calendar .HOUR_OF_DAY , 0 );
1511
+ today .set (Calendar .MINUTE , 0 );
1512
+ today .set (Calendar .SECOND , 0 );
1513
+ today .set (Calendar .MILLISECOND , 0 );
1514
+ return today ;
1515
+ }
1516
+
1452
1517
}
0 commit comments