Skip to content

Commit 07d329a

Browse files
author
Jens Claes
committed
Provide a new way of coloring the weekview.
Solves #48, solves #49 Provides 4 new background colors: past and future (and optional past and future colors for the weekend, so they stand out). Today is rendered as partially past and partially future (based on the hour). There's also a line drawn at the current hour (on today's view).
1 parent 9f9b25b commit 07d329a

File tree

2 files changed

+92
-20
lines changed

2 files changed

+92
-20
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class WeekView extends View {
4343
@Deprecated
4444
public static final int LENGTH_LONG = 2;
4545
private final Context mContext;
46-
private Calendar mToday;
47-
private Calendar mStartDate;
4846
private Paint mTimeTextPaint;
4947
private float mTimeTextWidth;
5048
private float mTimeTextHeight;
@@ -60,6 +58,11 @@ public class WeekView extends View {
6058
private Paint mHourSeparatorPaint;
6159
private float mHeaderMarginBottom;
6260
private Paint mTodayBackgroundPaint;
61+
private Paint mFutureBackgroundPaint;
62+
private Paint mPastBackgroundPaint;
63+
private Paint mFutureWeekendBackgroundPaint;
64+
private Paint mPastWeekendBackgroundPaint;
65+
private Paint mNowLinePaint;
6366
private Paint mTodayHeaderTextPaint;
6467
private Paint mEventBackgroundPaint;
6568
private float mHeaderColumnWidth;
@@ -84,6 +87,13 @@ public class WeekView extends View {
8487
private int mHeaderRowPadding = 10;
8588
private int mHeaderRowBackgroundColor = Color.WHITE;
8689
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;
8797
private int mHourSeparatorColor = Color.rgb(230, 230, 230);
8898
private int mTodayBackgroundColor = Color.rgb(239, 247, 254);
8999
private int mHourSeparatorHeight = 2;
@@ -238,6 +248,13 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
238248
mHeaderRowPadding = a.getDimensionPixelSize(R.styleable.WeekView_headerRowPadding, mHeaderRowPadding);
239249
mHeaderRowBackgroundColor = a.getColor(R.styleable.WeekView_headerRowBackgroundColor, mHeaderRowBackgroundColor);
240250
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);
241258
mHourSeparatorColor = a.getColor(R.styleable.WeekView_hourSeparatorColor, mHourSeparatorColor);
242259
mTodayBackgroundColor = a.getColor(R.styleable.WeekView_todayBackgroundColor, mTodayBackgroundColor);
243260
mHourSeparatorHeight = a.getDimensionPixelSize(R.styleable.WeekView_hourSeparatorHeight, mHourSeparatorHeight);
@@ -258,12 +275,6 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
258275
}
259276

260277
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-
267278
// Scrolling initialization.
268279
mGestureDetector = new GestureDetectorCompat(mContext, mGestureListener);
269280
mScroller = new OverScroller(mContext);
@@ -296,13 +307,26 @@ private void init() {
296307
// Prepare day background color paint.
297308
mDayBackgroundPaint = new Paint();
298309
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);
299318

300319
// Prepare hour separator color paint.
301320
mHourSeparatorPaint = new Paint();
302321
mHourSeparatorPaint.setStyle(Paint.Style.STROKE);
303322
mHourSeparatorPaint.setStrokeWidth(mHourSeparatorHeight);
304323
mHourSeparatorPaint.setColor(mHourSeparatorColor);
305324

325+
// Prepare the "now" line color paint
326+
mNowLinePaint = new Paint();
327+
mNowLinePaint.setStrokeWidth(mNowLineThickness);
328+
mNowLinePaint.setColor(mNowLineColor);
329+
306330
// Prepare today background color paint.
307331
mTodayBackgroundPaint = new Paint();
308332
mTodayBackgroundPaint.setColor(mTodayBackgroundColor);
@@ -327,7 +351,6 @@ private void init() {
327351
mEventTextPaint.setStyle(Paint.Style.FILL);
328352
mEventTextPaint.setColor(mEventTextColor);
329353
mEventTextPaint.setTextSize(mEventTextSize);
330-
mStartDate = (Calendar) mToday.clone();
331354

332355
// Set default event color.
333356
mDefaultEventColor = Color.parseColor("#9fc6e7");
@@ -378,10 +401,12 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
378401
mWidthPerDay = getWidth() - mHeaderColumnWidth - mColumnGap * (mNumberOfVisibleDays - 1);
379402
mWidthPerDay = mWidthPerDay/mNumberOfVisibleDays;
380403

404+
Calendar today = today();
405+
381406
// If the week view is being drawn for the first time, then consider the first day of week.
382407
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);
385410
mCurrentOrigin.x += (mWidthPerDay + mColumnGap) * difference;
386411
}
387412
mIsFirstDraw = false;
@@ -395,7 +420,7 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
395420
float startPixel = startFromPixel;
396421

397422
// Prepare to iterate for each day.
398-
Calendar day = (Calendar) mToday.clone();
423+
Calendar day = (Calendar) today.clone();
399424
day.add(Calendar.HOUR, 6);
400425

401426
// Prepare to iterate for each hour to draw the hour lines.
@@ -412,18 +437,18 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
412437
}
413438

414439
// Iterate through each day.
415-
mFirstVisibleDay = (Calendar) mToday.clone();
440+
mFirstVisibleDay = (Calendar) today.clone();
416441
mFirstVisibleDay.add(Calendar.DATE, leftDaysWithGaps);
417442
for (int dayNumber = leftDaysWithGaps + 1;
418443
dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays + 1;
419444
dayNumber++) {
420445

421446
// Check if the day is today.
422-
day = (Calendar) mToday.clone();
447+
day = (Calendar) today.clone();
423448
mLastVisibleDay = (Calendar) day.clone();
424449
day.add(Calendar.DATE, dayNumber - 1);
425450
mLastVisibleDay.add(Calendar.DATE, dayNumber - 2);
426-
boolean sameDay = isSameDay(day, mToday);
451+
boolean sameDay = isSameDay(day, today);
427452

428453
// Get more events if necessary. We want to store the events 3 months beforehand. Get
429454
// events only when it is the first iteration of the loop.
@@ -434,8 +459,27 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
434459

435460
// Draw background color for each day.
436461
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+
}
439483

440484
// Prepare the separator lines for hours.
441485
int i = 0;
@@ -456,6 +500,14 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
456500
// Draw the events.
457501
drawEvents(day, startPixel, canvas);
458502

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+
459511
// In the next iteration, start from the next day.
460512
startPixel += mWidthPerDay + mColumnGap;
461513
}
@@ -467,9 +519,9 @@ private void drawHeaderRowAndEvents(Canvas canvas) {
467519
startPixel = startFromPixel;
468520
for (int dayNumber=leftDaysWithGaps+1; dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays + 1; dayNumber++) {
469521
// Check if the day is today.
470-
day = (Calendar) mToday.clone();
522+
day = (Calendar) today.clone();
471523
day.add(Calendar.DATE, dayNumber - 1);
472-
boolean sameDay = isSameDay(day, mToday);
524+
boolean sameDay = isSameDay(day, today);
473525

474526
// Draw the day labels.
475527
String dayLabel = getDateTimeInterpreter().interpretDate(day);
@@ -497,7 +549,7 @@ private Calendar getTimeFromPoint(float x, float y){
497549
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel);
498550
if (mWidthPerDay + startPixel - start> 0
499551
&& x>start && x<startPixel + mWidthPerDay){
500-
Calendar day = (Calendar) mToday.clone();
552+
Calendar day = today();
501553
day.add(Calendar.DATE, dayNumber - 1);
502554
float pixelsFromZero = y - mCurrentOrigin.y - mHeaderTextHeight
503555
- mHeaderRowPadding * 2 - mTimeTextHeight/2 - mHeaderMarginBottom;
@@ -1449,4 +1501,17 @@ private boolean isSameDay(Calendar dayOne, Calendar dayTwo) {
14491501
return dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR) && dayOne.get(Calendar.DAY_OF_YEAR) == dayTwo.get(Calendar.DAY_OF_YEAR);
14501502
}
14511503

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+
14521517
}

library/src/main/res/values/attrs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
<attr name="noOfVisibleDays" format="integer"/>
2121
<attr name="headerRowBackgroundColor" format="color"/>
2222
<attr name="dayBackgroundColor" format="color"/>
23+
<attr name="futureBackgroundColor" format="color"/>
24+
<attr name="pastBackgroundColor" format="color"/>
25+
<attr name="futureWeekendBackgroundColor" format="color"/>
26+
<attr name="pastWeekendBackgroundColor" format="color"/>
27+
<attr name="nowLineColor" format="color"/>
28+
<attr name="nowLineThickness" format="dimension"/>
29+
<attr name="useNewColoringStyle" format="boolean"/>
2330
<attr name="hourSeparatorColor" format="color"/>
2431
<attr name="todayBackgroundColor" format="color"/>
2532
<attr name="todayHeaderTextColor" format="color"/>

0 commit comments

Comments
 (0)