@@ -95,6 +95,25 @@ public static IndicatorStyle fromValue(int value) {
95
95
}
96
96
}
97
97
98
+ public enum LinePosition {
99
+ Bottom (0 ), Top (1 );
100
+
101
+ public final int value ;
102
+
103
+ private LinePosition (int value ) {
104
+ this .value = value ;
105
+ }
106
+
107
+ public static LinePosition fromValue (int value ) {
108
+ for (LinePosition position : LinePosition .values ()) {
109
+ if (position .value == value ) {
110
+ return position ;
111
+ }
112
+ }
113
+ return null ;
114
+ }
115
+ }
116
+
98
117
private ViewPager mViewPager ;
99
118
private ViewPager .OnPageChangeListener mListener ;
100
119
private int mCurrentPage = -1 ;
@@ -108,6 +127,7 @@ public static IndicatorStyle fromValue(int value) {
108
127
private final Rect mBounds = new Rect ();
109
128
private final Paint mPaintFooterLine = new Paint ();
110
129
private IndicatorStyle mFooterIndicatorStyle ;
130
+ private LinePosition mLinePosition ;
111
131
private final Paint mPaintFooterIndicator = new Paint ();
112
132
private float mFooterIndicatorHeight ;
113
133
private float mFooterIndicatorUnderlinePadding ;
@@ -148,6 +168,7 @@ public TitlePageIndicator(Context context, AttributeSet attrs, int defStyle) {
148
168
final float defaultFooterIndicatorHeight = res .getDimension (R .dimen .default_title_indicator_footer_indicator_height );
149
169
final float defaultFooterIndicatorUnderlinePadding = res .getDimension (R .dimen .default_title_indicator_footer_indicator_underline_padding );
150
170
final float defaultFooterPadding = res .getDimension (R .dimen .default_title_indicator_footer_padding );
171
+ final int defaultLinePosition = res .getInteger (R .integer .default_title_indicator_line_position );
151
172
final int defaultSelectedColor = res .getColor (R .color .default_title_indicator_selected_color );
152
173
final boolean defaultSelectedBold = res .getBoolean (R .bool .default_title_indicator_selected_bold );
153
174
final int defaultTextColor = res .getColor (R .color .default_title_indicator_text_color );
@@ -165,6 +186,7 @@ public TitlePageIndicator(Context context, AttributeSet attrs, int defStyle) {
165
186
mFooterIndicatorHeight = a .getDimension (R .styleable .TitlePageIndicator_footerIndicatorHeight , defaultFooterIndicatorHeight );
166
187
mFooterIndicatorUnderlinePadding = a .getDimension (R .styleable .TitlePageIndicator_footerIndicatorUnderlinePadding , defaultFooterIndicatorUnderlinePadding );
167
188
mFooterPadding = a .getDimension (R .styleable .TitlePageIndicator_footerPadding , defaultFooterPadding );
189
+ mLinePosition = LinePosition .fromValue (a .getInteger (R .styleable .TitlePageIndicator_linePosition , defaultLinePosition ));
168
190
mTopPadding = a .getDimension (R .styleable .TitlePageIndicator_topPadding , defaultTopPadding );
169
191
mTitlePadding = a .getDimension (R .styleable .TitlePageIndicator_titlePadding , defaultTitlePadding );
170
192
mClipPadding = a .getDimension (R .styleable .TitlePageIndicator_clipPadding , defaultClipPadding );
@@ -241,6 +263,15 @@ public void setFooterIndicatorStyle(IndicatorStyle indicatorStyle) {
241
263
invalidate ();
242
264
}
243
265
266
+ public LinePosition getLinePosition () {
267
+ return mLinePosition ;
268
+ }
269
+
270
+ public void setLinePosition (LinePosition linePosition ) {
271
+ mLinePosition = linePosition ;
272
+ invalidate ();
273
+ }
274
+
244
275
public int getSelectedColor () {
245
276
return mColorSelected ;
246
277
}
@@ -332,7 +363,9 @@ protected void onDraw(Canvas canvas) {
332
363
}
333
364
334
365
// mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
335
- if (mCurrentPage == -1 && mViewPager != null ) mCurrentPage = mViewPager .getCurrentItem ();
366
+ if (mCurrentPage == -1 && mViewPager != null ) {
367
+ mCurrentPage = mViewPager .getCurrentItem ();
368
+ }
336
369
337
370
//Calculate views bounds
338
371
ArrayList <Rect > bounds = calculateAllBounds (mPaintText );
@@ -349,7 +382,7 @@ protected void onDraw(Canvas canvas) {
349
382
final int left = getLeft ();
350
383
final float leftClip = left + mClipPadding ;
351
384
final int width = getWidth ();
352
- final int height = getHeight ();
385
+ int height = getHeight ();
353
386
final int right = left + width ;
354
387
final float rightClip = right - mClipPadding ;
355
388
@@ -457,19 +490,29 @@ protected void onDraw(Canvas canvas) {
457
490
}
458
491
}
459
492
493
+ //If we want the line on the top change height to zero and invert the line height to trick the drawing code
494
+ float footerLineHeight = mFooterLineHeight ;
495
+ float footerIndicatorLineHeight = mFooterIndicatorHeight ;
496
+ if (mLinePosition == LinePosition .Top ) {
497
+ height = 0 ;
498
+ footerLineHeight = -footerLineHeight ;
499
+ footerIndicatorLineHeight = -footerIndicatorLineHeight ;
500
+ }
501
+
460
502
//Draw the footer line
461
503
mPath .reset ();
462
- mPath .moveTo (0 , height - mFooterLineHeight / 2f );
463
- mPath .lineTo (width , height - mFooterLineHeight / 2f );
504
+ mPath .moveTo (0 , height - footerLineHeight / 2f );
505
+ mPath .lineTo (width , height - footerLineHeight / 2f );
464
506
mPath .close ();
465
507
canvas .drawPath (mPath , mPaintFooterLine );
466
508
509
+ float heightMinusLine = height - footerLineHeight ;
467
510
switch (mFooterIndicatorStyle ) {
468
511
case Triangle :
469
512
mPath .reset ();
470
- mPath .moveTo (halfWidth , height - mFooterLineHeight - mFooterIndicatorHeight );
471
- mPath .lineTo (halfWidth + mFooterIndicatorHeight , height - mFooterLineHeight );
472
- mPath .lineTo (halfWidth - mFooterIndicatorHeight , height - mFooterLineHeight );
513
+ mPath .moveTo (halfWidth , heightMinusLine - footerIndicatorLineHeight );
514
+ mPath .lineTo (halfWidth + footerIndicatorLineHeight , heightMinusLine );
515
+ mPath .lineTo (halfWidth - footerIndicatorLineHeight , heightMinusLine );
473
516
mPath .close ();
474
517
canvas .drawPath (mPath , mPaintFooterIndicator );
475
518
break ;
@@ -480,11 +523,15 @@ protected void onDraw(Canvas canvas) {
480
523
}
481
524
482
525
Rect underlineBounds = bounds .get (page );
526
+ final float rightPlusPadding = underlineBounds .right + mFooterIndicatorUnderlinePadding ;
527
+ final float leftMinusPadding = underlineBounds .left - mFooterIndicatorUnderlinePadding ;
528
+ final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight ;
529
+
483
530
mPath .reset ();
484
- mPath .moveTo (underlineBounds . left - mFooterIndicatorUnderlinePadding , height - mFooterLineHeight );
485
- mPath .lineTo (underlineBounds . right + mFooterIndicatorUnderlinePadding , height - mFooterLineHeight );
486
- mPath .lineTo (underlineBounds . right + mFooterIndicatorUnderlinePadding , height - mFooterLineHeight - mFooterIndicatorHeight );
487
- mPath .lineTo (underlineBounds . left - mFooterIndicatorUnderlinePadding , height - mFooterLineHeight - mFooterIndicatorHeight );
531
+ mPath .moveTo (leftMinusPadding , heightMinusLine );
532
+ mPath .lineTo (rightPlusPadding , heightMinusLine );
533
+ mPath .lineTo (rightPlusPadding , heightMinusLineMinusIndicator );
534
+ mPath .lineTo (leftMinusPadding , heightMinusLineMinusIndicator );
488
535
mPath .close ();
489
536
490
537
mPaintFooterIndicator .setAlpha ((int )(0xFF * selectedPercent ));
0 commit comments