Skip to content

Commit

Permalink
Fix drawing of time column and header row
Browse files Browse the repository at this point in the history
  • Loading branch information
thellmund committed Sep 8, 2018
1 parent 2d0f448 commit 3054049
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
60 changes: 29 additions & 31 deletions library/src/main/java/com/alamkanak/weekview/WeekView.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Typeface;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.text.Layout;
Expand Down Expand Up @@ -492,11 +490,7 @@ private void initTextTimeWidth() {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

// Draw the header row.
drawHeaderRowAndEvents(canvas);

// Draw the time column and all the axes/separators.
drawTimeColumnAndAxes(canvas);
}

Expand Down Expand Up @@ -530,25 +524,33 @@ private void calculateHeaderHeight() {
}

private void drawTimeColumnAndAxes(Canvas canvas) {
int bottom = getHeight();
float top = mHeaderHeight + mHeaderRowPadding * 2;

// Draw the background color for the header column.
canvas.drawRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), mHeaderColumnBackgroundPaint);
canvas.drawRect(0, top, mHeaderColumnWidth, bottom, mHeaderColumnBackgroundPaint);

// Clip to paint in left column only.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight());
} else {
canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), Region.Op.REPLACE);
}
canvas.restore();
canvas.save();

canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight());

for (int i = 0; i < 24; i++) {
float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;

// Draw the text if its y position is not outside of the visible area. The pivot point of the text is the point at the bottom-right corner.
// Draw the text if its y position is not outside of the visible area. The pivot point
// of the text is the point at the bottom-right corner.
String time = getDateTimeInterpreter().interpretTime(i);
if (time == null)
throw new IllegalStateException("A DateTimeInterpreter must not return null time");
if (top < getHeight()) canvas.drawText(time, mTimeTextWidth + mHeaderColumnPadding, top + mTimeTextHeight, mTimeTextPaint);
if (top < getHeight()) {
float x = mTimeTextWidth + mHeaderColumnPadding;
float y = top + mTimeTextHeight;
canvas.drawText(time, x, y, mTimeTextPaint);
}
}

canvas.restore();
}

private void drawHeaderRowAndEvents(Canvas canvas) {
Expand Down Expand Up @@ -631,12 +633,10 @@ else if (mNewHourHeight > mMaxHourHeight)
}
}

canvas.save();

// Clip to paint events only.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
canvas.clipRect(mHeaderColumnWidth, mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2, getWidth(), getHeight());
} else {
canvas.clipRect(mHeaderColumnWidth, mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2, getWidth(), getHeight(), Region.Op.REPLACE);
}
canvas.clipRect(mHeaderColumnWidth, mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2, getWidth(), getHeight());

// Iterate through each day.
Calendar oldFirstVisibleDay = mFirstVisibleDay;
Expand Down Expand Up @@ -723,21 +723,19 @@ else if (day.before(today)) {
startPixel += mWidthPerDay + mColumnGap;
}

canvas.restore();
canvas.save();

// Hide everything in the first cell (top left corner).
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
canvas.clipRect(0, 0, mTimeTextWidth + mHeaderColumnPadding * 2, mHeaderHeight + mHeaderRowPadding * 2);
} else {
canvas.clipRect(0, 0, mTimeTextWidth + mHeaderColumnPadding * 2, mHeaderHeight + mHeaderRowPadding * 2, Region.Op.REPLACE);
}
canvas.clipRect(0, 0, mTimeTextWidth + mHeaderColumnPadding * 2, mHeaderHeight + mHeaderRowPadding * 2);

canvas.drawRect(0, 0, mTimeTextWidth + mHeaderColumnPadding * 2, mHeaderHeight + mHeaderRowPadding * 2, mHeaderBackgroundPaint);

canvas.restore();
canvas.save();

// Clip to paint header row only.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
canvas.clipRect(mHeaderColumnWidth, 0, getWidth(), mHeaderHeight + mHeaderRowPadding * 2);
} else {
canvas.clipRect(mHeaderColumnWidth, 0, getWidth(), mHeaderHeight + mHeaderRowPadding * 2, Region.Op.REPLACE);
}
canvas.clipRect(mHeaderColumnWidth, 0, getWidth(), mHeaderHeight + mHeaderRowPadding * 2);

// Draw the header background.
canvas.drawRect(0, 0, getWidth(), mHeaderHeight + mHeaderRowPadding * 2, mHeaderBackgroundPaint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,39 @@ public boolean onOptionsItemSelected(MenuItem item) {
*/
private void setupDateTimeInterpreter(final boolean shortDate) {
mWeekView.setDateTimeInterpreter(new DateTimeInterpreter() {

SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEE", Locale.getDefault());
SimpleDateFormat format = new SimpleDateFormat(" M/d", Locale.getDefault());

@Override
public String interpretDate(Calendar date) {
SimpleDateFormat weekdayNameFormat = new SimpleDateFormat("EEE", Locale.getDefault());
String weekday = weekdayNameFormat.format(date.getTime());
SimpleDateFormat format = new SimpleDateFormat(" M/d", Locale.getDefault());

// All android api level do not have a standard way of getting the first letter of
// the week day name. Hence we get the first char programmatically.
// Details: http://stackoverflow.com/questions/16959502/get-one-letter-abbreviation-of-week-day-of-a-date-in-java#answer-16959657
if (shortDate)
if (shortDate) {
weekday = String.valueOf(weekday.charAt(0));
return weekday.toUpperCase() + format.format(date.getTime());
}

String result = weekday.toUpperCase() + format.format(date.getTime());
return result;
}

@Override
public String interpretTime(int hour) {
return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM");
String result = hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM");
return result;
}
});
}

protected String getEventTitle(Calendar time) {
return String.format("Event of %02d:%02d %s/%d", time.get(Calendar.HOUR_OF_DAY), time.get(Calendar.MINUTE), time.get(Calendar.MONTH)+1, time.get(Calendar.DAY_OF_MONTH));
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
int month = time.get(Calendar.MONTH) + 1;
int dayOfMonth = time.get(Calendar.DAY_OF_MONTH);
return String.format(Locale.getDefault(), "Event of %02d:%02d %s/%d", hour, minute, month, dayOfMonth);
}

@Override
Expand Down

0 comments on commit 3054049

Please sign in to comment.