19
19
import android .text .format .DateFormat ;
20
20
import android .text .style .StyleSpan ;
21
21
import android .util .AttributeSet ;
22
+ import android .util .Log ;
22
23
import android .util .TypedValue ;
23
24
import android .view .GestureDetector ;
24
25
import android .view .HapticFeedbackConstants ;
@@ -128,6 +129,7 @@ public class WeekView extends View {
128
129
private ScaleGestureDetector mScaleDetector ;
129
130
private boolean mIsZooming ;
130
131
private int mEventCornerRadius = 0 ;
132
+ private boolean showHalfHours = false ;
131
133
132
134
// Listeners.
133
135
private EventClickListener mEventClickListener ;
@@ -291,6 +293,7 @@ public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
291
293
mEventMarginVertical = a .getDimensionPixelSize (R .styleable .WeekView_eventMarginVertical , mEventMarginVertical );
292
294
mXScrollingSpeed = a .getFloat (R .styleable .WeekView_xScrollingSpeed , mXScrollingSpeed );
293
295
mEventCornerRadius = a .getDimensionPixelSize (R .styleable .WeekView_eventCornerRadius , mEventCornerRadius );
296
+ showHalfHours = a .getBoolean (R .styleable .WeekView_showHalfHours , showHalfHours );
294
297
} finally {
295
298
a .recycle ();
296
299
}
@@ -310,7 +313,9 @@ private void init() {
310
313
mTimeTextPaint .setTextSize (mTextSize );
311
314
mTimeTextPaint .setColor (mHeaderColumnTextColor );
312
315
Rect rect = new Rect ();
313
- mTimeTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
316
+ final String exampleTime = showHalfHours ? "00:00 PM" : "00 PM" ;
317
+ mTimeTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
318
+ mTimeTextWidth = mTimeTextPaint .measureText (exampleTime );
314
319
mTimeTextHeight = rect .height ();
315
320
mHeaderMarginBottom = mTimeTextHeight / 2 ;
316
321
initTextTimeWidth ();
@@ -320,7 +325,7 @@ private void init() {
320
325
mHeaderTextPaint .setColor (mHeaderColumnTextColor );
321
326
mHeaderTextPaint .setTextAlign (Paint .Align .CENTER );
322
327
mHeaderTextPaint .setTextSize (mTextSize );
323
- mHeaderTextPaint .getTextBounds ("00 PM" , 0 , "00 PM" .length (), rect );
328
+ mHeaderTextPaint .getTextBounds (exampleTime , 0 , exampleTime .length (), rect );
324
329
mHeaderTextHeight = rect .height ();
325
330
mHeaderTextPaint .setTypeface (Typeface .DEFAULT_BOLD );
326
331
@@ -410,7 +415,7 @@ private void initTextTimeWidth() {
410
415
mTimeTextWidth = 0 ;
411
416
for (int i = 0 ; i < 24 ; i ++) {
412
417
// measure time string and get max width
413
- String time = getDateTimeInterpreter ().interpretTime (i );
418
+ String time = getDateTimeInterpreter ().interpretTime (i , 0 );
414
419
if (time == null )
415
420
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
416
421
mTimeTextWidth = Math .max (mTimeTextWidth , mTimeTextPaint .measureText (time ));
@@ -438,11 +443,35 @@ private void drawTimeColumnAndAxes(Canvas canvas) {
438
443
// Draw the background color for the header column.
439
444
canvas .drawRect (0 , mHeaderTextHeight + mHeaderRowPadding * 2 , mHeaderColumnWidth , getHeight (), mHeaderColumnBackgroundPaint );
440
445
441
- for (int i = 0 ; i < 24 ; i ++) {
442
- float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + mHourHeight * i + mHeaderMarginBottom ;
446
+ int numPeriodsInDay = showHalfHours ? 48 : 24 ;
447
+ for (int i = 0 ; i < numPeriodsInDay ; i ++) {
448
+ // If we are showing half hours (eg. 5:30am), space the times out by half the hour height
449
+ // and need to provide 30 minutes on each odd period, otherwise, minutes is always 0.
450
+ int timeSpacing ;
451
+ int minutes ;
452
+ int hour ;
453
+ if (showHalfHours ) {
454
+ timeSpacing = mHourHeight / 2 ;
455
+ hour = i / 2 ;
456
+ if (i % 2 == 0 ) {
457
+ minutes = 0 ;
458
+ }
459
+ else {
460
+ minutes = 30 ;
461
+ }
462
+ } else {
463
+ timeSpacing = mHourHeight ;
464
+ hour = i ;
465
+ minutes = 0 ;
466
+ }
467
+
468
+ // Calculate the top of the rectangle where the time text will go
469
+ float top = mHeaderTextHeight + mHeaderRowPadding * 2 + mCurrentOrigin .y + timeSpacing * i + mHeaderMarginBottom ;
470
+
471
+ // Get the time to be displayed, as a String.
472
+ String time = getDateTimeInterpreter ().interpretTime (hour , minutes );
443
473
444
474
// 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.
445
- String time = getDateTimeInterpreter ().interpretTime (i );
446
475
if (time == null )
447
476
throw new IllegalStateException ("A DateTimeInterpreter must not return null time" );
448
477
if (top < getHeight ()) canvas .drawText (time , mTimeTextWidth + mHeaderColumnPadding , top + mTimeTextHeight , mTimeTextPaint );
@@ -1130,13 +1159,18 @@ public String interpretDate(Calendar date) {
1130
1159
}
1131
1160
1132
1161
@ Override
1133
- public String interpretTime (int hour ) {
1162
+ public String interpretTime (int hour , int minutes ) {
1134
1163
Calendar calendar = Calendar .getInstance ();
1135
1164
calendar .set (Calendar .HOUR_OF_DAY , hour );
1136
- calendar .set (Calendar .MINUTE , 0 );
1165
+ calendar .set (Calendar .MINUTE , minutes );
1137
1166
1138
1167
try {
1139
- SimpleDateFormat sdf = DateFormat .is24HourFormat (getContext ()) ? new SimpleDateFormat ("HH:mm" , Locale .getDefault ()) : new SimpleDateFormat ("hh a" , Locale .getDefault ());
1168
+ SimpleDateFormat sdf ;
1169
+ if (showHalfHours ) {
1170
+ sdf = new SimpleDateFormat ("HH:mm a" , Locale .getDefault ());
1171
+ } else {
1172
+ sdf = DateFormat .is24HourFormat (getContext ()) ? new SimpleDateFormat ("HH:mm" , Locale .getDefault ()) : new SimpleDateFormat ("hh a" , Locale .getDefault ());
1173
+ }
1140
1174
return sdf .format (calendar .getTime ());
1141
1175
} catch (Exception e ) {
1142
1176
e .printStackTrace ();
0 commit comments