Android 修改ViewPager 滑动速率

package com.as.apprehendschool.customviews.viewpager;

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

public class FixedSpeedScroller extends Scroller {
    private int mDuration = 1500;
                                                                                                            
    public FixedSpeedScroller(Context context) {
        super(context);
    }
                                                                                                            
    public FixedSpeedScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }
                                                                                                            
    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }
                                                                                                            
    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        // Ignore received duration, use fixed one instead
        super.startScroll(startX, startY, dx, dy, mDuration);
    }
                                                                                                            
    public void setmDuration(int time) {
        mDuration = time;
    }
                                                                                                            
    public int getmDuration() {
        return mDuration;
    }
}
   try {
            Field field = ViewPager.class.getDeclaredField("mScroller");
            field.setAccessible(true);
            FixedSpeedScroller scroller = new FixedSpeedScroller(mViewBinding.bannerVp.getContext(),
                    new AccelerateInterpolator());
            field.set(mViewBinding.bannerVp, scroller);
            scroller.setmDuration(300);
        } catch (Exception e) {

        }

 

你可能感兴趣的:(安卓进阶)