@@ -44,11 +44,14 @@ final class StackHelper implements ValueAnimator.AnimatorUpdateListener{
4444 private boolean isFingerTouch = false ;
4545 private boolean enableScroll = false ;
4646 private int displayPosition = 0 ;
47+ private int mDuration = 500 ;
48+ private int mDelay = 3000 ;
4749 private StackLayout layout ;
48- private boolean debug = false ;
50+ private boolean debug = true ;
4951
5052 StackHelper (int touchSlop ) {
5153 mTouchSlop = touchSlop ;
54+ mAnimator .setDurations (mDuration );
5255 }
5356
5457 void bindLayout (StackLayout layout ) {
@@ -169,6 +172,19 @@ void setPosition(int position){
169172 }
170173 }
171174
175+ int getPosition (){
176+ return displayPosition ;
177+ }
178+
179+ void setDuration (int duration ){
180+ mDuration = duration ;
181+ mAnimator .setDurations (mDuration );
182+ }
183+
184+ void setLooperDelay (int mills ){
185+ mDelay = mills ;
186+ }
187+
172188 private View getStackView (){
173189 View view = null ;
174190 for (WeakReference <View > item ;weakViews .size () > 0 ;){
@@ -254,21 +270,22 @@ void releaseScroll(int velocity){
254270 float transX = mAnimator .getTranslationX ();
255271 if (layout .getAdapter () != null && Math .abs (transX ) > 0 && !mAnimator .isRunning ()) {
256272 //滑动速度大于限定或者滑动距离大于宽度一部分,移出当前视图可视范围
257- if (Math .abs (velocity ) >= everyWidth || Math .abs (transX ) > mMaxDistance ){
258- int sign = MODEL_LEFT == swipModel ? -1 : 1 ;
273+ int sign = MODEL_LEFT == swipModel ? -1 : 1 ;
274+ /** 加入防抖动 */
275+ if ( (velocity *sign >=0 || Math .abs (velocity )<3 *mTouchSlop ) && (Math .abs (velocity ) >= everyWidth || Math .abs (transX ) > mMaxDistance )){
259276 if (isTopRemove ){
260277 addBottomView ();
261278 transX = layout .getWidth ()*sign - transX ;
262- mAnimator .startAnimator (true ,false ,transX ,mMaxDistance , this );
279+ mAnimator .startAnimator (true ,false ,transX ,this );
263280 }else {
264281 removeBottomView ();
265282 transX = everyWidth *sign - transX ;
266- mAnimator .startAnimator (false ,false ,transX ,mMaxDistance , this );
283+ mAnimator .startAnimator (false ,false ,transX ,this );
267284 }
268285 }else {
269286 //取反恢复原样
270287 transX *= -1 ;
271- mAnimator .startAnimator (!isTopRemove ,false ,transX ,mMaxDistance , this );
288+ mAnimator .startAnimator (!isTopRemove ,false ,transX ,this );
272289 }
273290 swipModel = MODEL_NONE ;
274291 }
@@ -279,7 +296,7 @@ void autoScroll(){
279296 mAnimator .setAnimatorView (layout .getChildAt (layout .getChildCount () - 1 ));
280297 int transX = (layout .stackEdgeModel == MODEL_LEFT ? 1 : -1 )*layout .getWidth ();
281298 isTopRemove = true ;
282- mAnimator .startAnimator (true ,true ,transX ,mMaxDistance , this );
299+ mAnimator .startAnimator (true ,true ,transX ,this );
283300 }
284301 }
285302
@@ -289,7 +306,7 @@ void setAutoPlay(boolean looper){
289306 executor = new ScheduledThreadPoolExecutor (2 );
290307 }
291308 if (executor .getQueue ().size () <= 0 ) {
292- executor .scheduleWithFixedDelay (new AutoRunnable (this ), 2 , 3 , TimeUnit .SECONDS );
309+ executor .scheduleWithFixedDelay (new AutoRunnable (this ), 2000 , mDelay , TimeUnit .MILLISECONDS );
293310 }
294311 }else {
295312 if (executor != null && !executor .isShutdown ()) {
@@ -359,6 +376,16 @@ static class ReleaseAnimator{
359376 private Interpolator interpolatorAuto = new LinearInterpolator ();
360377 private boolean needRemoveTopView = false ;
361378 private boolean needAddBottomView ;
379+ private int duration ;
380+ private int mWidth ;
381+
382+ public ReleaseAnimator () {
383+ mWidth = Resources .getSystem ().getDisplayMetrics ().widthPixels ;
384+ }
385+
386+ void setDurations (int duration ){
387+ this .duration = duration ;
388+ }
362389
363390 void setAnimatorView (View view ){
364391 mAnimatorView = view ;
@@ -388,11 +415,11 @@ void setTranslationX(float value){
388415 lastValue = value ;
389416 }
390417
391- void startAnimator (boolean needRemoveTop ,boolean needAddBottom ,float transX , int distance , ValueAnimator .AnimatorUpdateListener listener ){
392- int duration = needAddBottom ? 400 : (int )(Math .abs (transX )/distance * 100 );
418+ void startAnimator (boolean needRemoveTop ,boolean needAddBottom ,float transX , ValueAnimator .AnimatorUpdateListener listener ){
419+ int mills = needAddBottom ? duration : (int )(1.2 * duration * Math .abs (transX )/mWidth );
393420 needAddBottomView = needAddBottom ;
394421 needRemoveTopView = needRemoveTop ;
395- animator = ValueAnimator .ofFloat (0f , transX ).setDuration (duration );
422+ animator = ValueAnimator .ofFloat (0f , transX ).setDuration (mills );
396423 animator .setInterpolator (needAddBottom ? interpolatorAuto : interpolator );
397424 animator .addUpdateListener (listener );
398425 animator .start ();
0 commit comments