美文网首页
viewpager自定义滑动速度

viewpager自定义滑动速度

作者: CQ_TYL | 来源:发表于2018-09-30 17:54 被阅读0次

android原生的viewpager默认不支持自定义滑动速度,直接复制FixedSpeedScroller 类使用即可:

使用:

   controlViewPagerSpeed(this,splash_viewpager,400);//毫秒 速度
//    自定义viewpager滑动速度
    private void controlViewPagerSpeed(Context context, ViewPager viewpager, int DurationSwitch) {
        try {
            Field mField;
            mField = ViewPager.class.getDeclaredField("mScroller");
            mField.setAccessible(true);
            FixedSpeedScroller mScroller =
            new FixedSpeedScroller(context, new AccelerateInterpolator());
            mScroller.setmDuration(DurationSwitch);
            mField.set(viewpager, mScroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

/**
 * Created by tyl
 * 2018/9/30/030
 * Describe:
 */
public class FixedSpeedScroller extends Scroller {

    private int mDuration = 1500; // 默认滑动速度 1500ms

    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);
    }

    /**
     * set animation time
     *
     * @param time
     */
    public void setmDuration(int time) {
        mDuration = time;
    }

    /**
     * get current animation time
     *
     * @return
     */
    public int getmDuration() {
        return mDuration;
    }
}

相关文章

网友评论

      本文标题:viewpager自定义滑动速度

      本文链接:https://www.haomeiwen.com/subject/cmiwoftx.html