@@ -45,8 +45,6 @@ public class WeekView extends View {
45
45
@ Deprecated
46
46
public static final int LENGTH_LONG = 2 ;
47
47
private final Context mContext ;
48
- private Calendar mToday ;
49
- private Calendar mStartDate ;
50
48
private Paint mTimeTextPaint ;
51
49
private float mTimeTextWidth ;
52
50
private float mTimeTextHeight ;
@@ -62,6 +60,11 @@ public class WeekView extends View {
62
60
private Paint mHourSeparatorPaint ;
63
61
private float mHeaderMarginBottom ;
64
62
private Paint mTodayBackgroundPaint ;
63
+ private Paint mFutureBackgroundPaint ;
64
+ private Paint mPastBackgroundPaint ;
65
+ private Paint mFutureWeekendBackgroundPaint ;
66
+ private Paint mPastWeekendBackgroundPaint ;
67
+ private Paint mNowLinePaint ;
65
68
private Paint mTodayHeaderTextPaint ;
66
69
private Paint mEventBackgroundPaint ;
67
70
private float mHeaderColumnWidth ;
@@ -86,6 +89,13 @@ public class WeekView extends View {
86
89
private int mHeaderRowPadding = 10 ;
87
90
private int mHeaderRowBackgroundColor = Color .WHITE ;
88
91
private int mDayBackgroundColor = Color .rgb (245 , 245 , 245 );
92
+ private int mPastBackgroundColor = Color .rgb (227 , 227 , 227 );
93
+ private int mFutureBackgroundColor = Color .rgb (245 , 245 , 245 );
94
+ private int mPastWeekendBackgroundColor = 0 ;
95
+ private int mFutureWeekendBackgroundColor = 0 ;
96
+ private int mNowLineColor = Color .rgb (102 , 102 , 102 );
97
+ private int mNowLineThickness = 5 ;
98
+ private boolean mUseNewColoring = false ;
89
99
private int mHourSeparatorColor = Color .rgb (230 , 230 , 230 );
90
100
private int mTodayBackgroundColor = Color .rgb (239 , 247 , 254 );
91
101
private int mHourSeparatorHeight = 2 ;
@@ -240,6 +250,13 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
240
250
mHeaderRowPadding = a .getDimensionPixelSize (R .styleable .WeekView_headerRowPadding , mHeaderRowPadding );
241
251
mHeaderRowBackgroundColor = a .getColor (R .styleable .WeekView_headerRowBackgroundColor , mHeaderRowBackgroundColor );
242
252
mDayBackgroundColor = a .getColor (R .styleable .WeekView_dayBackgroundColor , mDayBackgroundColor );
253
+ mFutureBackgroundColor = a .getColor (R .styleable .WeekView_futureBackgroundColor , mFutureBackgroundColor );
254
+ mPastBackgroundColor = a .getColor (R .styleable .WeekView_pastBackgroundColor , mPastBackgroundColor );
255
+ mFutureWeekendBackgroundColor = a .getColor (R .styleable .WeekView_futureWeekendBackgroundColor , mFutureBackgroundColor ); // If not set, use the same color as in the week
256
+ mPastWeekendBackgroundColor = a .getColor (R .styleable .WeekView_pastWeekendBackgroundColor , mPastBackgroundColor );
257
+ mNowLineColor = a .getColor (R .styleable .WeekView_nowLineColor , mNowLineColor );
258
+ mNowLineThickness = a .getDimensionPixelSize (R .styleable .WeekView_nowLineThickness , mNowLineThickness );
259
+ mUseNewColoring = a .getBoolean (R .styleable .WeekView_useNewColoringStyle , mUseNewColoring );
243
260
mHourSeparatorColor = a .getColor (R .styleable .WeekView_hourSeparatorColor , mHourSeparatorColor );
244
261
mTodayBackgroundColor = a .getColor (R .styleable .WeekView_todayBackgroundColor , mTodayBackgroundColor );
245
262
mHourSeparatorHeight = a .getDimensionPixelSize (R .styleable .WeekView_hourSeparatorHeight , mHourSeparatorHeight );
@@ -260,12 +277,6 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
260
277
}
261
278
262
279
private void init () {
263
- // Get the date today.
264
- mToday = Calendar .getInstance ();
265
- mToday .set (Calendar .HOUR_OF_DAY , 0 );
266
- mToday .set (Calendar .MINUTE , 0 );
267
- mToday .set (Calendar .SECOND , 0 );
268
-
269
280
// Scrolling initialization.
270
281
mGestureDetector = new GestureDetectorCompat (mContext , mGestureListener );
271
282
mScroller = new OverScroller (mContext );
@@ -298,13 +309,26 @@ private void init() {
298
309
// Prepare day background color paint.
299
310
mDayBackgroundPaint = new Paint ();
300
311
mDayBackgroundPaint .setColor (mDayBackgroundColor );
312
+ mFutureBackgroundPaint = new Paint ();
313
+ mFutureBackgroundPaint .setColor (mFutureBackgroundColor );
314
+ mPastBackgroundPaint = new Paint ();
315
+ mPastBackgroundPaint .setColor (mPastBackgroundColor );
316
+ mFutureWeekendBackgroundPaint = new Paint ();
317
+ mFutureWeekendBackgroundPaint .setColor (mFutureWeekendBackgroundColor );
318
+ mPastWeekendBackgroundPaint = new Paint ();
319
+ mPastWeekendBackgroundPaint .setColor (mPastWeekendBackgroundColor );
301
320
302
321
// Prepare hour separator color paint.
303
322
mHourSeparatorPaint = new Paint ();
304
323
mHourSeparatorPaint .setStyle (Paint .Style .STROKE );
305
324
mHourSeparatorPaint .setStrokeWidth (mHourSeparatorHeight );
306
325
mHourSeparatorPaint .setColor (mHourSeparatorColor );
307
326
327
+ // Prepare the "now" line color paint
328
+ mNowLinePaint = new Paint ();
329
+ mNowLinePaint .setStrokeWidth (mNowLineThickness );
330
+ mNowLinePaint .setColor (mNowLineColor );
331
+
308
332
// Prepare today background color paint.
309
333
mTodayBackgroundPaint = new Paint ();
310
334
mTodayBackgroundPaint .setColor (mTodayBackgroundColor );
@@ -329,7 +353,6 @@ private void init() {
329
353
mEventTextPaint .setStyle (Paint .Style .FILL );
330
354
mEventTextPaint .setColor (mEventTextColor );
331
355
mEventTextPaint .setTextSize (mEventTextSize );
332
- mStartDate = (Calendar ) mToday .clone ();
333
356
334
357
// Set default event color.
335
358
mDefaultEventColor = Color .parseColor ("#9fc6e7" );
@@ -388,10 +411,12 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
388
411
mWidthPerDay = getWidth () - mHeaderColumnWidth - mColumnGap * (mNumberOfVisibleDays - 1 );
389
412
mWidthPerDay = mWidthPerDay /mNumberOfVisibleDays ;
390
413
414
+ Calendar today = today ();
415
+
391
416
// If the week view is being drawn for the first time, then consider the first day of week.
392
417
if (mIsFirstDraw && mNumberOfVisibleDays >= 7 ) {
393
- if (mToday .get (Calendar .DAY_OF_WEEK ) != mFirstDayOfWeek ) {
394
- int difference = 7 + (mToday .get (Calendar .DAY_OF_WEEK ) - mFirstDayOfWeek );
418
+ if (today .get (Calendar .DAY_OF_WEEK ) != mFirstDayOfWeek ) {
419
+ int difference = 7 + (today .get (Calendar .DAY_OF_WEEK ) - mFirstDayOfWeek );
395
420
mCurrentOrigin .x += (mWidthPerDay + mColumnGap ) * difference ;
396
421
}
397
422
mIsFirstDraw = false ;
@@ -405,7 +430,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
405
430
float startPixel = startFromPixel ;
406
431
407
432
// Prepare to iterate for each day.
408
- Calendar day = (Calendar ) mToday .clone ();
433
+ Calendar day = (Calendar ) today .clone ();
409
434
day .add (Calendar .HOUR , 6 );
410
435
411
436
// Prepare to iterate for each hour to draw the hour lines.
@@ -423,7 +448,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
423
448
424
449
// Iterate through each day.
425
450
Calendar oldFirstVisibleDay = mFirstVisibleDay ;
426
- mFirstVisibleDay = (Calendar ) mToday .clone ();
451
+ mFirstVisibleDay = (Calendar ) today .clone ();
427
452
mFirstVisibleDay .add (Calendar .DATE , leftDaysWithGaps );
428
453
if (!mFirstVisibleDay .equals (oldFirstVisibleDay ) && mScrolledListener != null ){
429
454
mScrolledListener .onFirstVisibleDayChanged (mFirstVisibleDay , oldFirstVisibleDay );
@@ -433,11 +458,11 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
433
458
dayNumber ++) {
434
459
435
460
// Check if the day is today.
436
- day = (Calendar ) mToday .clone ();
461
+ day = (Calendar ) today .clone ();
437
462
mLastVisibleDay = (Calendar ) day .clone ();
438
463
day .add (Calendar .DATE , dayNumber - 1 );
439
464
mLastVisibleDay .add (Calendar .DATE , dayNumber - 2 );
440
- boolean sameDay = isSameDay (day , mToday );
465
+ boolean sameDay = isSameDay (day , today );
441
466
442
467
// Get more events if necessary. We want to store the events 3 months beforehand. Get
443
468
// events only when it is the first iteration of the loop.
@@ -448,8 +473,27 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
448
473
449
474
// Draw background color for each day.
450
475
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel );
451
- if (mWidthPerDay + startPixel - start > 0 )
452
- canvas .drawRect (start , mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom , startPixel + mWidthPerDay , getHeight (), sameDay ? mTodayBackgroundPaint : mDayBackgroundPaint );
476
+ if (mWidthPerDay + startPixel - start > 0 ){
477
+ if (mUseNewColoring ){
478
+ boolean isWeekend = (day .get (Calendar .DAY_OF_WEEK ) == Calendar .SATURDAY || day .get (Calendar .DAY_OF_WEEK ) == Calendar .SUNDAY );
479
+ Paint pastPaint = isWeekend ? mPastWeekendBackgroundPaint : mPastBackgroundPaint ;
480
+ Paint futurePaint = isWeekend ? mFutureWeekendBackgroundPaint : mFutureBackgroundPaint ;
481
+ float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom + mCurrentOrigin .y ;
482
+
483
+ if (sameDay ){
484
+ Calendar now = Calendar .getInstance ();
485
+ float beforeNow = (now .get (Calendar .HOUR_OF_DAY )+now .get (Calendar .MINUTE )/60.0f )*mHourHeight ;
486
+ canvas .drawRect (start , startY , startPixel + mWidthPerDay , startY +beforeNow , pastPaint );
487
+ canvas .drawRect (start , startY +beforeNow , startPixel + mWidthPerDay , getHeight (), futurePaint );
488
+ } else if (day .before (today )) {
489
+ canvas .drawRect (start , startY , startPixel + mWidthPerDay , getHeight (), pastPaint );
490
+ } else {
491
+ canvas .drawRect (start , startY , startPixel + mWidthPerDay , getHeight (), futurePaint );
492
+ }
493
+ }else {
494
+ canvas .drawRect (start , mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom , startPixel + mWidthPerDay , getHeight (), sameDay ? mTodayBackgroundPaint : mDayBackgroundPaint );
495
+ }
496
+ }
453
497
454
498
// Prepare the separator lines for hours.
455
499
int i = 0 ;
@@ -470,6 +514,14 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
470
514
// Draw the events.
471
515
drawEvents (day , startPixel , canvas );
472
516
517
+ //Draw the line at the current time
518
+ if (mUseNewColoring && sameDay ){
519
+ float startY = mHeaderTextHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom + mCurrentOrigin .y ;
520
+ Calendar now = Calendar .getInstance ();
521
+ float beforeNow = (now .get (Calendar .HOUR_OF_DAY )+now .get (Calendar .MINUTE )/60.0f )*mHourHeight ;
522
+ canvas .drawLine (start , startY +beforeNow , startPixel + mWidthPerDay , startY +beforeNow , mNowLinePaint );
523
+ }
524
+
473
525
// In the next iteration, start from the next day.
474
526
startPixel += mWidthPerDay + mColumnGap ;
475
527
}
@@ -481,9 +533,9 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
481
533
startPixel = startFromPixel ;
482
534
for (int dayNumber =leftDaysWithGaps +1 ; dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays + 1 ; dayNumber ++) {
483
535
// Check if the day is today.
484
- day = (Calendar ) mToday .clone ();
536
+ day = (Calendar ) today .clone ();
485
537
day .add (Calendar .DATE , dayNumber - 1 );
486
- boolean sameDay = isSameDay (day , mToday );
538
+ boolean sameDay = isSameDay (day , today );
487
539
488
540
// Draw the day labels.
489
541
String dayLabel = getDateTimeInterpreter ().interpretDate (day );
@@ -511,7 +563,7 @@ private Calendar getTimeFromPoint(float x, float y){
511
563
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel );
512
564
if (mWidthPerDay + startPixel - start > 0
513
565
&& x >start && x <startPixel + mWidthPerDay ){
514
- Calendar day = ( Calendar ) mToday . clone ();
566
+ Calendar day = today ();
515
567
day .add (Calendar .DATE , dayNumber - 1 );
516
568
float pixelsFromZero = y - mCurrentOrigin .y - mHeaderTextHeight
517
569
- mHeaderRowPadding * 2 - mTimeTextHeight /2 - mHeaderMarginBottom ;
@@ -1498,4 +1550,17 @@ private boolean isSameDay(Calendar dayOne, Calendar dayTwo) {
1498
1550
return dayOne .get (Calendar .YEAR ) == dayTwo .get (Calendar .YEAR ) && dayOne .get (Calendar .DAY_OF_YEAR ) == dayTwo .get (Calendar .DAY_OF_YEAR );
1499
1551
}
1500
1552
1553
+ /**
1554
+ * Returns a calendar instance at the start of this day
1555
+ * @return the calendar instance
1556
+ */
1557
+ private Calendar today (){
1558
+ Calendar today = Calendar .getInstance ();
1559
+ today .set (Calendar .HOUR_OF_DAY , 0 );
1560
+ today .set (Calendar .MINUTE , 0 );
1561
+ today .set (Calendar .SECOND , 0 );
1562
+ today .set (Calendar .MILLISECOND , 0 );
1563
+ return today ;
1564
+ }
1565
+
1501
1566
}
0 commit comments