1
1
package com .heinrichreimersoftware .materialintro .app ;
2
2
3
+ import android .animation .Animator ;
4
+ import android .animation .AnimatorListenerAdapter ;
3
5
import android .animation .ArgbEvaluator ;
6
+ import android .animation .ValueAnimator ;
4
7
import android .annotation .SuppressLint ;
5
8
import android .app .ActivityManager ;
6
9
import android .content .res .ColorStateList ;
18
21
import android .support .annotation .ColorRes ;
19
22
import android .support .annotation .IntDef ;
20
23
import android .support .annotation .IntRange ;
24
+ import android .support .annotation .InterpolatorRes ;
21
25
import android .support .annotation .NonNull ;
22
26
import android .support .annotation .StringRes ;
23
27
import android .support .v4 .app .Fragment ;
33
37
import android .util .TypedValue ;
34
38
import android .view .View ;
35
39
import android .view .WindowManager ;
40
+ import android .view .animation .AnimationUtils ;
41
+ import android .view .animation .Interpolator ;
36
42
import android .widget .Button ;
37
43
import android .widget .ImageButton ;
38
44
import android .widget .LinearLayout ;
@@ -122,10 +128,16 @@ public class IntroActivity extends AppCompatActivity {
122
128
private int autoplayCounter ;
123
129
private long autoplayDelay ;
124
130
131
+ private Interpolator pagerInterpolator ;
132
+ private long pagerScrollDuration ;
133
+
125
134
@ Override
126
135
protected void onCreate (Bundle savedInstanceState ) {
127
136
super .onCreate (savedInstanceState );
128
137
138
+ pagerInterpolator = AnimationUtils .loadInterpolator (this , android .R .interpolator .accelerate_decelerate );
139
+ pagerScrollDuration = getResources ().getInteger (android .R .integer .config_shortAnimTime );
140
+
129
141
if (savedInstanceState != null ) {
130
142
if (savedInstanceState .containsKey (KEY_CURRENT_ITEM )) {
131
143
position = savedInstanceState .getInt (KEY_CURRENT_ITEM , position );
@@ -254,7 +266,7 @@ private void findViews(){
254
266
255
267
pager .setAdapter (adapter );
256
268
pager .addOnPageChangeListener (listener );
257
- pager .setCurrentItem (position );
269
+ pager .setCurrentItem (position , false );
258
270
259
271
pagerIndicator .setViewPager (pager );
260
272
@@ -274,12 +286,60 @@ public void onClick(View v) {
274
286
CheatSheet .setup (buttonBack );
275
287
}
276
288
289
+ private void smoothScrollPagerTo (final int position ) {
290
+ ValueAnimator animator = ValueAnimator .ofFloat (pager .getCurrentItem (), position );
291
+ animator .addListener (new AnimatorListenerAdapter () {
292
+ @ Override
293
+ public void onAnimationEnd (Animator animation ) {
294
+ pager .endFakeDrag ();
295
+ pager .setCurrentItem (position );
296
+ }
297
+
298
+ @ Override
299
+ public void onAnimationCancel (Animator animation ) {
300
+ pager .endFakeDrag ();
301
+ }
302
+ });
303
+ animator .addUpdateListener (new ValueAnimator .AnimatorUpdateListener () {
304
+ @ Override
305
+ public void onAnimationUpdate (ValueAnimator animation ) {
306
+ float position = (Float ) animation .getAnimatedValue ();
307
+
308
+ fakeDragToPosition (position );
309
+ }
310
+
311
+ private boolean fakeDragToPosition (float position ) {
312
+ // The following mimics the underlying calculations in ViewPager
313
+ float scrollX = pager .getScrollX ();
314
+ int pagerWidth = pager .getWidth ();
315
+ int currentPosition = pager .getCurrentItem ();
316
+
317
+ if (position > currentPosition && Math .floor (position ) != currentPosition && position % 1 != 0 ) {
318
+ pager .setCurrentItem ((int ) Math .floor (position ), false );
319
+ }
320
+ else if (position < currentPosition && Math .ceil (position ) != currentPosition && position % 1 != 0 ) {
321
+ pager .setCurrentItem ((int ) Math .ceil (position ), false );
322
+ }
323
+
324
+ if (!pager .isFakeDragging () && !pager .beginFakeDrag ())
325
+ return false ;
326
+
327
+ pager .fakeDragBy (scrollX - pagerWidth * position );
328
+ return true ;
329
+ }
330
+ });
331
+
332
+ animator .setInterpolator (pagerInterpolator );
333
+ animator .setDuration (pagerScrollDuration );
334
+ animator .start ();
335
+ }
336
+
277
337
public void nextSlide () {
278
338
int currentItem = pager .getCurrentItem ();
279
339
if (currentItem > adapter .getCount () - 1 ) finishIfNeeded ();
280
340
281
341
if (canGoForward (currentItem , true )) {
282
- pager . setCurrentItem (++currentItem , true );
342
+ smoothScrollPagerTo (++currentItem );
283
343
}
284
344
else {
285
345
AnimUtils .applyShakeAnimation (this , buttonNext );
@@ -308,7 +368,7 @@ else if (canGoForward(endPosition, true)) {
308
368
if (endPosition == pager .getCurrentItem ())
309
369
return false ;
310
370
311
- pager . setCurrentItem (endPosition );
371
+ smoothScrollPagerTo (endPosition );
312
372
313
373
return autoplayCounter != 0 ;
314
374
@@ -319,7 +379,7 @@ public void previousSlide() {
319
379
if (currentItem <= 0 ) return ;
320
380
321
381
if (canGoBackward (currentItem , true )) {
322
- pager . setCurrentItem (--currentItem , true );
382
+ smoothScrollPagerTo (--currentItem );
323
383
}
324
384
else {
325
385
AnimUtils .applyShakeAnimation (this , buttonBack );
@@ -334,7 +394,7 @@ private void performButtonBackPress() {
334
394
while (endPosition < count && canGoForward (endPosition , true )) {
335
395
endPosition ++;
336
396
}
337
- pager . setCurrentItem (endPosition );
397
+ smoothScrollPagerTo (endPosition );
338
398
} else if (buttonBackFunction == BUTTON_BACK_FUNCTION_BACK ) {
339
399
previousSlide ();
340
400
}
@@ -768,6 +828,26 @@ public boolean isAutoplaying() {
768
828
return autoplayCallback != null ;
769
829
}
770
830
831
+ public long getPagerScrollDuration () {
832
+ return pagerScrollDuration ;
833
+ }
834
+
835
+ public void setPagerScrollDuration (@ IntRange (from = 1 ) long pagerScrollDuration ) {
836
+ this .pagerScrollDuration = pagerScrollDuration ;
837
+ }
838
+
839
+ public Interpolator getPagerInterpolator () {
840
+ return pagerInterpolator ;
841
+ }
842
+
843
+ public void setPagerInterpolator (Interpolator pagerInterpolator ) {
844
+ this .pagerInterpolator = pagerInterpolator ;
845
+ }
846
+
847
+ public void setPagerInterpolator (@ InterpolatorRes int interpolatorRes ) {
848
+ this .pagerInterpolator = AnimationUtils .loadInterpolator (this , interpolatorRes );
849
+ }
850
+
771
851
@ SuppressWarnings ("unused" )
772
852
public boolean isFullscreen () {
773
853
return fullscreen ;
@@ -1122,7 +1202,7 @@ public void onClick(View v) {
1122
1202
while (endPosition < count && canGoForward (endPosition , true )) {
1123
1203
endPosition ++;
1124
1204
}
1125
- pager . setCurrentItem (endPosition );
1205
+ smoothScrollPagerTo (endPosition );
1126
1206
}
1127
1207
}
1128
1208
}
0 commit comments