@@ -144,6 +144,9 @@ private enum Direction {
144
144
private boolean mVerticalFlingEnabled = true ;
145
145
private int mAllDayEventHeight = 100 ;
146
146
private int mScrollDuration = 250 ;
147
+ private int mStartTime = 0 ;
148
+ private int mEndTime = 24 ;
149
+ private boolean autoLimitTime = false ;
147
150
148
151
// Listeners.
149
152
private EventClickListener mEventClickListener ;
@@ -353,6 +356,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
353
356
mVerticalFlingEnabled = a .getBoolean (R .styleable .WeekView_verticalFlingEnabled , mVerticalFlingEnabled );
354
357
mAllDayEventHeight = a .getDimensionPixelSize (R .styleable .WeekView_allDayEventHeight , mAllDayEventHeight );
355
358
mScrollDuration = a .getInt (R .styleable .WeekView_scrollDuration , mScrollDuration );
359
+ autoLimitTime = a .getBoolean (R .styleable .WeekView_autoLimitTime , autoLimitTime );
356
360
} finally {
357
361
a .recycle ();
358
362
}
@@ -534,7 +538,7 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
534
538
canvas .clipRect (0 , mHeaderHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), Region .Op .REPLACE );
535
539
536
540
for (int i = 0 ; i < 24 ; i ++) {
537
- float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * i + mHeaderMarginBottom ;
541
+ float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * ( i - mStartTime ) + mHeaderMarginBottom ;
538
542
539
543
// 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.
540
544
String time = getDateTimeInterpreter ().interpretTime (i );
@@ -592,8 +596,8 @@ else if (mNewHourHeight > mMaxHourHeight)
592
596
}
593
597
594
598
// If the new mCurrentOrigin.y is invalid, make it valid.
595
- if (mCurrentOrigin .y < getHeight () - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight /2 )
596
- mCurrentOrigin .y = getHeight () - mHourHeight * 24 - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight /2 ;
599
+ if (mCurrentOrigin .y < getHeight () - mHourHeight * ( mEndTime - mStartTime ) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight /2 )
600
+ mCurrentOrigin .y = getHeight () - mHourHeight * ( mEndTime - mStartTime ) - mHeaderHeight - mHeaderRowPadding * 2 - mHeaderMarginBottom - mTimeTextHeight /2 ;
597
601
598
602
// Don't put an "else if" because it will trigger a glitch when completely zoomed out and
599
603
// scrolling vertically.
@@ -684,7 +688,7 @@ else if (day.before(today)) {
684
688
// Prepare the separator lines for hours.
685
689
int i = 0 ;
686
690
for (int hourNumber = 0 ; hourNumber < 24 ; hourNumber ++) {
687
- float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * hourNumber + mTimeTextHeight /2 + mHeaderMarginBottom ;
691
+ float top = mHeaderHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * ( hourNumber - mStartTime ) + mTimeTextHeight /2 + mHeaderMarginBottom ;
688
692
if (top > mHeaderHeight + mHeaderRowPadding * 2 + mTimeTextHeight /2 + mHeaderMarginBottom - mHourSeparatorHeight && top < getHeight () && startPixel + mWidthPerDay - start > 0 ){
689
693
hourLines [i * 4 ] = start ;
690
694
hourLines [i * 4 + 1 ] = top ;
@@ -697,6 +701,12 @@ else if (day.before(today)) {
697
701
// Draw the lines for hours.
698
702
canvas .drawLines (hourLines , mHourSeparatorPaint );
699
703
704
+ // Limit time events
705
+ // Only calculate on visible days
706
+ if (dayNumber <= leftDaysWithGaps + mNumberOfVisibleDays && autoLimitTime && mNumberOfVisibleDays == 1 ) {
707
+ limitEventTime (day );
708
+ }
709
+
700
710
// Draw the events.
701
711
drawEvents (day , startPixel , canvas );
702
712
@@ -771,6 +781,39 @@ private Calendar getTimeFromPoint(float x, float y){
771
781
return null ;
772
782
}
773
783
784
+ /**
785
+ * limit current time of event by update mStartTime & mEndTime
786
+ * find smallest of start time & latest of end time
787
+ * */
788
+ private void limitEventTime (Calendar date ){
789
+ if (mEventRects != null && mEventRects .size () > 0 ) {
790
+ Calendar startTime = null ;
791
+ Calendar endTime = null ;
792
+
793
+ for (EventRect eventRect : mEventRects ) {
794
+ if (isSameDay (eventRect .event .getStartTime (), date ) && !eventRect .event .isAllDay ()) {
795
+
796
+ if (startTime ==null || startTime .after (eventRect .event .getStartTime ())){
797
+ startTime = eventRect .event .getStartTime ();
798
+ }
799
+
800
+ if (endTime ==null || endTime .before (eventRect .event .getEndTime ())){
801
+ endTime = eventRect .event .getEndTime ();
802
+ }
803
+ }
804
+ }
805
+
806
+ if (startTime !=null && endTime !=null && startTime .before (endTime )) {
807
+ mStartTime = Math .max (0 ,startTime .get (Calendar .HOUR_OF_DAY ));
808
+ mEndTime = Math .min (24 ,endTime .get (Calendar .HOUR_OF_DAY ));
809
+ return ;
810
+ }
811
+ }
812
+
813
+ mStartTime = 0 ;
814
+ mEndTime = 24 ;
815
+ }
816
+
774
817
/**
775
818
* Draw all the events of a particular day.
776
819
* @param date The day.
@@ -782,12 +825,13 @@ private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) {
782
825
for (int i = 0 ; i < mEventRects .size (); i ++) {
783
826
if (isSameDay (mEventRects .get (i ).event .getStartTime (), date ) && !mEventRects .get (i ).event .isAllDay ()){
784
827
828
+ int marginTop = mHourHeight * mStartTime ;
785
829
// Calculate top.
786
- float top = mHourHeight * 24 * mEventRects .get (i ).top / 1440 + mCurrentOrigin .y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight /2 + mEventMarginVertical ;
830
+ float top = mHourHeight * 24 * mEventRects .get (i ).top / 1440 + mCurrentOrigin .y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight /2 + mEventMarginVertical - marginTop ;
787
831
788
832
// Calculate bottom.
789
833
float bottom = mEventRects .get (i ).bottom ;
790
- bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin .y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight /2 - mEventMarginVertical ;
834
+ bottom = mHourHeight * 24 * bottom / 1440 + mCurrentOrigin .y + mHeaderHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom + mTimeTextHeight /2 - mEventMarginVertical - marginTop ;
791
835
792
836
// Calculate left and right.
793
837
float left = startFromPixel + mEventRects .get (i ).left * mWidthPerDay ;
@@ -1154,7 +1198,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) {
1154
1198
}
1155
1199
}
1156
1200
1157
-
1158
1201
// Calculate left and right position for all the events.
1159
1202
// Get the maxRowCount by looking in all columns.
1160
1203
int maxRowCount = 0 ;
@@ -1184,7 +1227,6 @@ else if (!isEventsCollide(eventRect.event, column.get(column.size()-1).event)) {
1184
1227
}
1185
1228
}
1186
1229
1187
-
1188
1230
/**
1189
1231
* Checks if two events overlap.
1190
1232
* @param event1 The first event.
@@ -1676,6 +1718,30 @@ public void setShowDistinctWeekendColor(boolean showDistinctWeekendColor) {
1676
1718
invalidate ();
1677
1719
}
1678
1720
1721
+ /**
1722
+ * auto calculate limit time on events in day.
1723
+ * @see #limitEventTime(Calendar)
1724
+ * */
1725
+ public void setAutoLimitTime (boolean isAuto ){
1726
+ this .autoLimitTime = isAuto ;
1727
+ invalidate ();
1728
+ }
1729
+
1730
+ /**
1731
+ * set fix visible time.
1732
+ * @param startHour limit time display on top (between 0~24)
1733
+ * @param endHour limit time display at bottom (between 0~24 and > startHour)
1734
+ * */
1735
+ public void setLimitTime (int startHour , int endHour ){
1736
+ if (endHour <= startHour || startHour < 0 || endHour > 24 ){
1737
+ throw new IllegalArgumentException ("endHour must larger startHour" );
1738
+ }
1739
+ this .mStartTime = startHour ;
1740
+ this .mEndTime = endHour ;
1741
+ this .autoLimitTime = false ;
1742
+ invalidate ();
1743
+ }
1744
+
1679
1745
/**
1680
1746
* Whether past and future days should have two different background colors. The past and
1681
1747
* future day colors are defined by the attributes `futureBackgroundColor` and
0 commit comments