diff --git a/library/src/main/java/com/alamkanak/weekview/WeekView.java b/library/src/main/java/com/alamkanak/weekview/WeekView.java
index da3fa71a0..7ec9b0e39 100755
--- a/library/src/main/java/com/alamkanak/weekview/WeekView.java
+++ b/library/src/main/java/com/alamkanak/weekview/WeekView.java
@@ -144,6 +144,9 @@ private enum Direction {
private boolean mVerticalFlingEnabled = true;
private int mAllDayEventHeight = 100;
private int mScrollDuration = 250;
+ private int mStartTime = 0;
+ private int mEndTime = 24;
+ private boolean autoLimitTime = false;
// Listeners.
private EventClickListener mEventClickListener;
@@ -353,6 +356,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
mVerticalFlingEnabled = a.getBoolean(R.styleable.WeekView_verticalFlingEnabled, mVerticalFlingEnabled);
mAllDayEventHeight = a.getDimensionPixelSize(R.styleable.WeekView_allDayEventHeight, mAllDayEventHeight);
mScrollDuration = a.getInt(R.styleable.WeekView_scrollDuration, mScrollDuration);
+ autoLimitTime = a.getBoolean(R.styleable.WeekView_autoLimitTime, autoLimitTime);
} finally {
a.recycle();
}
@@ -533,8 +537,8 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
// Clip to paint in left column only.
canvas.clipRect(0, mHeaderHeight + mHeaderRowPadding * 2, mHeaderColumnWidth, getHeight(), Region.Op.REPLACE);
- for (int i = 0; i < 24; i++) {
- float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * i + mHeaderMarginBottom;
+ for (int i = mStartTime; i < mEndTime; i++) {
+ float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (i-mStartTime) + 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.
String time = getDateTimeInterpreter().interpretTime(i);
@@ -592,8 +596,8 @@ else if (mNewHourHeight > mMaxHourHeight)
}
// If the new mCurrentOrigin.y is invalid, make it valid.
- if (mCurrentOrigin.y < getHeight() - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2)
- mCurrentOrigin.y = getHeight() - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2;
+ if (mCurrentOrigin.y < getHeight() - mHourHeight * (mEndTime-mStartTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2)
+ mCurrentOrigin.y = getHeight() - mHourHeight * (mEndTime-mStartTime) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight/2;
// Don't put an "else if" because it will trigger a glitch when completely zoomed out and
// scrolling vertically.
@@ -609,7 +613,7 @@ else if (mNewHourHeight > mMaxHourHeight)
// Prepare to iterate for each day.
Calendar day = (Calendar) today.clone();
- day.add(Calendar.HOUR, 6);
+ day.add(Calendar.HOUR_OF_DAY, 6);
// Prepare to iterate for each hour to draw the hour lines.
int lineCount = (int) ((getHeight() - mHeaderHeight - mHeaderRowPadding * 2 -
@@ -683,8 +687,8 @@ else if (day.before(today)) {
// Prepare the separator lines for hours.
int i = 0;
- for (int hourNumber = 0; hourNumber < 24; hourNumber++) {
- float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * hourNumber + mTimeTextHeight/2 + mHeaderMarginBottom;
+ for (int hourNumber = mStartTime; hourNumber < mEndTime; hourNumber++) {
+ float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin.y + mHourHeight * (hourNumber-mStartTime) + mTimeTextHeight/2 + mHeaderMarginBottom;
if (top > mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom - mHourSeparatorHeight && top < getHeight() && startPixel + mWidthPerDay - start > 0){
hourLines[i * 4] = start;
hourLines[i * 4 + 1] = top;
@@ -697,6 +701,12 @@ else if (day.before(today)) {
// Draw the lines for hours.
canvas.drawLines(hourLines, mHourSeparatorPaint);
+ // Limit time events
+ // Only calculate on visible days
+ if(dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays && autoLimitTime && mNumberOfVisibleDays == 1) {
+ limitEventTime(day);
+ }
+
// Draw the events.
drawEvents(day, startPixel, canvas);
@@ -704,8 +714,9 @@ else if (day.before(today)) {
if (mShowNowLine && sameDay){
float startY = mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight/2 + mHeaderMarginBottom + mCurrentOrigin.y;
Calendar now = Calendar.getInstance();
- float beforeNow = (now.get(Calendar.HOUR_OF_DAY) + now.get(Calendar.MINUTE)/60.0f) * mHourHeight;
- canvas.drawLine(start, startY + beforeNow, startPixel + mWidthPerDay, startY + beforeNow, mNowLinePaint);
+ float beforeNow = (now.get(Calendar.HOUR_OF_DAY) - mStartTime + now.get(Calendar.MINUTE)/60.0f) * mHourHeight;
+ float top = startY + beforeNow;
+ canvas.drawLine(start, top, startPixel + mWidthPerDay, top, mNowLinePaint);
}
// In the next iteration, start from the next day.
@@ -762,7 +773,7 @@ private Calendar getTimeFromPoint(float x, float y){
- mHeaderRowPadding * 2 - mTimeTextHeight/2 - mHeaderMarginBottom;
int hour = (int)(pixelsFromZero / mHourHeight);
int minute = (int) (60 * (pixelsFromZero - hour * mHourHeight) / mHourHeight);
- day.add(Calendar.HOUR, hour);
+ day.add(Calendar.HOUR_OF_DAY, hour + mStartTime);
day.set(Calendar.MINUTE, minute);
return day;
}
@@ -771,6 +782,39 @@ private Calendar getTimeFromPoint(float x, float y){
return null;
}
+ /**
+ * limit current time of event by update mStartTime & mEndTime
+ * find smallest of start time & latest of end time
+ * */
+ private void limitEventTime(Calendar date){
+ if (mEventRects != null && mEventRects.size() > 0) {
+ Calendar startTime = null;
+ Calendar endTime = null;
+
+ for (EventRect eventRect: mEventRects) {
+ if (isSameDay(eventRect.event.getStartTime(), date) && !eventRect.event.isAllDay()) {
+
+ if(startTime==null || startTime.after(eventRect.event.getStartTime())){
+ startTime = eventRect.event.getStartTime();
+ }
+
+ if(endTime==null || endTime.before(eventRect.event.getEndTime())){
+ endTime = eventRect.event.getEndTime();
+ }
+ }
+ }
+
+ if(startTime!=null && endTime !=null && startTime.before(endTime)) {
+ mStartTime = Math.max(0,startTime.get(Calendar.HOUR_OF_DAY));
+ mEndTime = Math.min(24,endTime.get(Calendar.HOUR_OF_DAY)+1);
+ return;
+ }
+ }
+
+ mStartTime = 0;
+ mEndTime = 24;
+ }
+
/**
* Draw all the events of a particular day.
* @param date The day.
@@ -782,12 +826,13 @@ private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) {
for (int i = 0; i < mEventRects.size(); i++) {
if (isSameDay(mEventRects.get(i).event.getStartTime(), date) && !mEventRects.get(i).event.isAllDay()){
+ int marginTop = mHourHeight * mStartTime;
// Calculate top.
- float top = mHourHeight * 24 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical;
+ float top = mHourHeight * 24 * mEventRects.get(i).top / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 + mEventMarginVertical-marginTop;
// Calculate bottom.
float bottom = mEventRects.get(i).bottom;
- bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical;
+ bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin.y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight/2 - mEventMarginVertical - marginTop;
// Calculate left and right.
float left = startFromPixel + mEventRects.get(i).left * mWidthPerDay;
@@ -1154,7 +1199,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) {
}
}
-
// Calculate left and right position for all the events.
// Get the maxRowCount by looking in all columns.
int maxRowCount = 0;
@@ -1184,7 +1228,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) {
}
}
-
/**
* Checks if two events overlap.
* @param event1 The first event.
@@ -1676,6 +1719,30 @@ public void setShowDistinctWeekendColor(boolean showDistinctWeekendColor) {
invalidate();
}
+ /**
+ * auto calculate limit time on events in day.
+ * @see #limitEventTime(Calendar)
+ * */
+ public void setAutoLimitTime(boolean isAuto){
+ this.autoLimitTime = isAuto;
+ invalidate();
+ }
+
+ /**
+ * set fix visible time.
+ * @param startHour limit time display on top (between 0~24)
+ * @param endHour limit time display at bottom (between 0~24 and > startHour)
+ * */
+ public void setLimitTime(int startHour, int endHour){
+ if(endHour <= startHour || startHour < 0 || endHour > 24){
+ throw new IllegalArgumentException("endHour must larger startHour");
+ }
+ this.mStartTime = startHour;
+ this.mEndTime = endHour;
+ this.autoLimitTime = false;
+ invalidate();
+ }
+
/**
* Whether past and future days should have two different background colors. The past and
* future day colors are defined by the attributes `futureBackgroundColor` and
diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml
index a37a3717b..e1b62e05e 100644
--- a/library/src/main/res/values/attrs.xml
+++ b/library/src/main/res/values/attrs.xml
@@ -51,5 +51,6 @@
+