1
1
package com .smarteist .autoimageslider ;
2
2
3
3
import android .content .Context ;
4
+ import android .content .res .TypedArray ;
4
5
import android .database .DataSetObserver ;
6
+ import android .graphics .Color ;
5
7
import android .os .Handler ;
6
- import androidx .viewpager .widget .PagerAdapter ;
7
- import androidx .viewpager .widget .ViewPager ;
8
8
import android .util .AttributeSet ;
9
+ import android .view .Gravity ;
9
10
import android .view .LayoutInflater ;
10
11
import android .view .View ;
11
12
import android .widget .FrameLayout ;
13
+
14
+ import androidx .viewpager .widget .PagerAdapter ;
15
+ import androidx .viewpager .widget .ViewPager ;
16
+
12
17
import com .smarteist .autoimageslider .IndicatorView .PageIndicatorView ;
13
18
import com .smarteist .autoimageslider .IndicatorView .animation .type .AnimationType ;
19
+ import com .smarteist .autoimageslider .IndicatorView .animation .type .BaseAnimation ;
20
+ import com .smarteist .autoimageslider .IndicatorView .animation .type .ColorAnimation ;
14
21
import com .smarteist .autoimageslider .IndicatorView .draw .controller .DrawController ;
15
22
import com .smarteist .autoimageslider .IndicatorView .draw .data .Orientation ;
23
+ import com .smarteist .autoimageslider .IndicatorView .draw .data .RtlMode ;
24
+ import com .smarteist .autoimageslider .IndicatorView .utils .DensityUtils ;
16
25
import com .smarteist .autoimageslider .Transformations .AntiClockSpinTransformation ;
17
26
import com .smarteist .autoimageslider .Transformations .Clock_SpinTransformation ;
18
27
import com .smarteist .autoimageslider .Transformations .CubeInDepthTransformation ;
36
45
import com .smarteist .autoimageslider .Transformations .VerticalShutTransformation ;
37
46
import com .smarteist .autoimageslider .Transformations .ZoomOutTransformation ;
38
47
48
+ import static com .smarteist .autoimageslider .IndicatorView .draw .controller .AttributeController .getRtlMode ;
49
+
39
50
public class SliderView extends FrameLayout {
40
51
52
+ public static final int AUTO_CYCLE_DIRECTION_RIGHT = 0 ;
53
+ public static final int AUTO_CYCLE_DIRECTION_LEFT = 1 ;
54
+ public static final int AUTO_CYCLE_DIRECTION_BACK_AND_FORTH = 2 ;
55
+
41
56
private final Handler mHandler = new Handler ();
42
- private boolean mIsAutoCycle = true ;
43
- private int mScrollTimeInSec = 2 ;
57
+ private boolean mIsAutoCycle ;
58
+ private int mScrollTimeInSec ;
44
59
private CircularSliderHandle mCircularSliderHandle ;
45
60
private PageIndicatorView mPagerIndicator ;
46
61
private DataSetObserver mDataSetObserver ;
47
62
private PagerAdapter mPagerAdapter ;
48
63
private Runnable mSliderRunnable ;
49
64
private SliderPager mSliderPager ;
65
+ private int mAutoCycleDirection ;
66
+ private boolean mFlagBackAndForth ;
50
67
51
68
52
69
public SliderView (Context context ) {
@@ -57,11 +74,60 @@ public SliderView(Context context) {
57
74
public SliderView (Context context , AttributeSet attrs ) {
58
75
super (context , attrs );
59
76
setupSlideView (context );
77
+ setUpAttributes (context , attrs );
60
78
}
61
79
62
80
public SliderView (Context context , AttributeSet attrs , int defStyleAttr ) {
63
81
super (context , attrs , defStyleAttr );
64
82
setupSlideView (context );
83
+ setUpAttributes (context , attrs );
84
+ }
85
+
86
+ private void setUpAttributes (Context context , AttributeSet attrs ) {
87
+ TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .SliderView , 0 , 0 );
88
+
89
+ int indicatorOrientation = typedArray .getInt (R .styleable .SliderView_sliderIndicatorOrientation , Orientation .HORIZONTAL .ordinal ());
90
+ Orientation orientation ;
91
+ if (indicatorOrientation == 0 ) {
92
+ orientation = Orientation .HORIZONTAL ;
93
+ } else {
94
+ orientation = Orientation .VERTICAL ;
95
+ }
96
+ int indicatorRadius = (int ) typedArray .getDimension (R .styleable .SliderView_sliderIndicatorRadius , DensityUtils .dpToPx (2 ));
97
+ int indicatorPadding = (int ) typedArray .getDimension (R .styleable .SliderView_sliderIndicatorPadding , DensityUtils .dpToPx (3 ));
98
+ int indicatorMargin = (int ) typedArray .getDimension (R .styleable .SliderView_sliderIndicatorMargin , DensityUtils .dpToPx (12 ));
99
+ int indicatorGravity = typedArray .getInt (R .styleable .SliderView_sliderIndicatorGravity , Gravity .CENTER | Gravity .BOTTOM );
100
+ int indicatorUnselectedColor = typedArray .getColor (R .styleable .SliderView_sliderIndicatorUnselectedColor , Color .parseColor (ColorAnimation .DEFAULT_UNSELECTED_COLOR ));
101
+ int indicatorSelectedColor = typedArray .getColor (R .styleable .SliderView_sliderIndicatorSelectedColor , Color .parseColor (ColorAnimation .DEFAULT_SELECTED_COLOR ));
102
+ int indicatorAnimationDuration = typedArray .getInt (R .styleable .SliderView_sliderIndicatorAnimationDuration , BaseAnimation .DEFAULT_ANIMATION_TIME );
103
+ int indicatorRtlMode = typedArray .getInt (R .styleable .PageIndicatorView_piv_rtl_mode , RtlMode .Off .ordinal ());
104
+ RtlMode rtlMode = getRtlMode (indicatorRtlMode );
105
+ int sliderAnimationDuration = typedArray .getInt (R .styleable .SliderView_sliderAnimationDuration , SliderPager .DEFAULT_SCROLL_DURATION );
106
+ int sliderScrollTimeInSec = typedArray .getInt (R .styleable .SliderView_sliderScrollTimeInSec , 2 );
107
+ boolean sliderCircularHandlerEnabled = typedArray .getBoolean (R .styleable .SliderView_sliderCircularHandlerEnabled , true );
108
+ boolean sliderAutoCycleEnabled = typedArray .getBoolean (R .styleable .SliderView_sliderAutoCycleEnabled , true );
109
+ boolean sliderStartAutoCycle = typedArray .getBoolean (R .styleable .SliderView_sliderStartAutoCycle , false );
110
+ int sliderAutoCycleDirection = typedArray .getInt (R .styleable .SliderView_sliderAutoCycleDirection , AUTO_CYCLE_DIRECTION_RIGHT );
111
+
112
+ setIndicatorOrientation (orientation );
113
+ setIndicatorRadius (indicatorRadius );
114
+ setIndicatorPadding (indicatorPadding );
115
+ setIndicatorMargin (indicatorMargin );
116
+ setIndicatorGravity (indicatorGravity );
117
+ setIndicatorUnselectedColor (indicatorUnselectedColor );
118
+ setIndicatorSelectedColor (indicatorSelectedColor );
119
+ setIndicatorAnimationDuration (indicatorAnimationDuration );
120
+ setIndicatorRtlMode (rtlMode );
121
+ setSliderAnimationDuration (sliderAnimationDuration );
122
+ setScrollTimeInSec (sliderScrollTimeInSec );
123
+ setCircularHandlerEnabled (sliderCircularHandlerEnabled );
124
+ setAutoCycle (sliderAutoCycleEnabled );
125
+ setAutoCycleDirection (sliderAutoCycleDirection );
126
+ if (sliderStartAutoCycle ) {
127
+ startAutoCycle ();
128
+ }
129
+
130
+ typedArray .recycle ();
65
131
}
66
132
67
133
private void setupSlideView (Context context ) {
@@ -96,6 +162,10 @@ public void setSliderAdapter(final PagerAdapter pagerAdapter) {
96
162
mPagerIndicator .setDynamicCount (true );
97
163
}
98
164
165
+ public PagerAdapter getSliderAdapter () {
166
+ return mPagerAdapter ;
167
+ }
168
+
99
169
private void registerDataObserver () {
100
170
if (mDataSetObserver != null ) {
101
171
mPagerAdapter .unregisterDataSetObserver (mDataSetObserver );
@@ -111,10 +181,6 @@ public void onChanged() {
111
181
mPagerAdapter .registerDataSetObserver (mDataSetObserver );
112
182
}
113
183
114
- public PagerAdapter getSliderAdapter () {
115
- return mPagerAdapter ;
116
- }
117
-
118
184
public boolean isAutoCycle () {
119
185
return mIsAutoCycle ;
120
186
}
@@ -128,10 +194,9 @@ public void setAutoCycle(boolean autoCycle) {
128
194
}
129
195
130
196
public void setCircularHandlerEnabled (boolean enable ) {
197
+ mSliderPager .clearOnPageChangeListeners ();
131
198
if (enable ) {
132
199
mSliderPager .addOnPageChangeListener (mCircularSliderHandle );
133
- } else {
134
- mSliderPager .clearOnPageChangeListeners ();
135
200
}
136
201
}
137
202
@@ -227,19 +292,6 @@ public void setSliderAnimationDuration(int duration) {
227
292
mSliderPager .setScrollDuration (duration );
228
293
}
229
294
230
- public void setIndicatorAnimationDuration (long duration ) {
231
- mPagerIndicator .setAnimationDuration (duration );
232
- }
233
-
234
-
235
- public void setIndicatorPadding (int padding ) {
236
- mPagerIndicator .setPadding (padding );
237
- }
238
-
239
- public void setIndicatorOrientation (Orientation orientation ) {
240
- mPagerIndicator .setOrientation (orientation );
241
- }
242
-
243
295
public void setCurrentPagePosition (int position ) {
244
296
245
297
if (getSliderAdapter () != null ) {
@@ -258,6 +310,24 @@ public int getCurrentPagePosition() {
258
310
}
259
311
}
260
312
313
+ public void setIndicatorAnimationDuration (long duration ) {
314
+ mPagerIndicator .setAnimationDuration (duration );
315
+ }
316
+
317
+ public void setIndicatorGravity (int gravity ) {
318
+ FrameLayout .LayoutParams layoutParams = (LayoutParams ) mPagerIndicator .getLayoutParams ();
319
+ layoutParams .gravity = gravity ;
320
+ mPagerIndicator .setLayoutParams (layoutParams );
321
+ }
322
+
323
+ public void setIndicatorPadding (int padding ) {
324
+ mPagerIndicator .setPadding (padding );
325
+ }
326
+
327
+ public void setIndicatorOrientation (Orientation orientation ) {
328
+ mPagerIndicator .setOrientation (orientation );
329
+ }
330
+
261
331
public void setIndicatorAnimation (IndicatorAnimations animations ) {
262
332
263
333
switch (animations ) {
@@ -294,7 +364,7 @@ public void setIndicatorAnimation(IndicatorAnimations animations) {
294
364
}
295
365
}
296
366
297
- public void setPagerIndicatorVisibility (boolean visibility ) {
367
+ public void setIndicatorVisibility (boolean visibility ) {
298
368
if (visibility ) {
299
369
mPagerIndicator .setVisibility (VISIBLE );
300
370
} else {
@@ -321,14 +391,35 @@ public void run() {
321
391
322
392
int currentPosition = mSliderPager .getCurrentItem ();
323
393
324
- if (currentPosition == getSliderAdapter ().getCount () - 1 ) {
325
- // if is last item return to the first position
326
- mSliderPager .setCurrentItem (0 , true );
394
+ if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_BACK_AND_FORTH ) {
395
+ if (currentPosition == 0 ) {
396
+ mFlagBackAndForth = true ;
397
+ }
398
+ if (currentPosition == getSliderAdapter ().getCount () - 1 ) {
399
+ mFlagBackAndForth = false ;
400
+ }
401
+ if (mFlagBackAndForth ) {
402
+ mSliderPager .setCurrentItem (++currentPosition , true );
403
+ } else {
404
+ mSliderPager .setCurrentItem (--currentPosition , true );
405
+ }
406
+ } else if (mAutoCycleDirection == AUTO_CYCLE_DIRECTION_LEFT ) {
407
+ if (currentPosition == 0 ) {
408
+ mSliderPager .setCurrentItem (getSliderAdapter ().getCount () - 1 , true );
409
+ } else {
410
+ mSliderPager .setCurrentItem (--currentPosition , true );
411
+ }
327
412
} else {
328
- // continue smooth transition between pager
329
- mSliderPager .setCurrentItem (++currentPosition , true );
413
+ if (currentPosition == getSliderAdapter ().getCount () - 1 ) {
414
+ // if is last item return to the first position
415
+ mSliderPager .setCurrentItem (0 , true );
416
+ } else {
417
+ // continue smooth transition between pager
418
+ mSliderPager .setCurrentItem (++currentPosition , true );
419
+ }
330
420
}
331
421
422
+
332
423
} finally {
333
424
mHandler .postDelayed (this , mScrollTimeInSec * 1000 );
334
425
}
@@ -340,27 +431,45 @@ public void run() {
340
431
mHandler .postDelayed (mSliderRunnable , mScrollTimeInSec * 1000 );
341
432
}
342
433
343
- public int getSliderIndicatorRadius () {
434
+ public void setAutoCycleDirection (int direction ) {
435
+ mAutoCycleDirection = direction ;
436
+ }
437
+
438
+ public int getAutoCycleDirection () {
439
+ return mAutoCycleDirection ;
440
+ }
441
+
442
+ public int getIndicatorRadius () {
344
443
return mPagerIndicator .getRadius ();
345
444
}
346
445
347
- public void setSliderIndicatorRadius (int pagerIndicatorRadius ) {
446
+ public void setIndicatorRtlMode (RtlMode rtlMode ) {
447
+ mPagerIndicator .setRtlMode (rtlMode );
448
+ }
449
+
450
+ public void setIndicatorRadius (int pagerIndicatorRadius ) {
348
451
this .mPagerIndicator .setRadius (pagerIndicatorRadius );
349
452
}
350
453
351
- public void setSliderIndicatorSelectedColor (int color ) {
454
+ public void setIndicatorMargin (int margin ) {
455
+ FrameLayout .LayoutParams layoutParams = (LayoutParams ) mPagerIndicator .getLayoutParams ();
456
+ layoutParams .setMargins (margin , margin , margin , margin );
457
+ mPagerIndicator .setLayoutParams (layoutParams );
458
+ }
459
+
460
+ public void setIndicatorSelectedColor (int color ) {
352
461
this .mPagerIndicator .setSelectedColor (color );
353
462
}
354
463
355
- public int getSliderIndicatorSelectedColor () {
464
+ public int getIndicatorSelectedColor () {
356
465
return this .mPagerIndicator .getSelectedColor ();
357
466
}
358
467
359
- public void setSliderIndicatorUnselectedColor (int color ) {
468
+ public void setIndicatorUnselectedColor (int color ) {
360
469
this .mPagerIndicator .setUnselectedColor (color );
361
470
}
362
471
363
- public int getSliderIndicatorUnselectedColor () {
472
+ public int getIndicatorUnselectedColor () {
364
473
return this .mPagerIndicator .getUnselectedColor ();
365
474
}
366
475
0 commit comments