Skip to content

Commit

Permalink
增加控制时间
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglu1209 committed Mar 15, 2017
1 parent 3c9a876 commit b078e41
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public void setCurrentPager(int page, boolean isSmooth){
mPager.setCurrentItem(page, isSmooth);
}

public Banner setScrollDuration(int duration){
mPager.setScrollDuration(duration);
return this;
}

public Banner setDotGravity(int gravity) {
mDotGroup.setGravity(gravity | Gravity.BOTTOM);
float density = mContext.getResources().getDisplayMetrics().density;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class BannerViewPager extends ViewPager {
private static final int MSG_WHAT = -00001;
private int SEND_TIME = 5000;
private int position;

private ViewPagerScroller mPagerScroller;

private Handler mHandler = new Handler() {
@Override
Expand All @@ -36,6 +36,13 @@ public BannerViewPager(Context context) {

public BannerViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
mPagerScroller = new ViewPagerScroller(context);
mPagerScroller.initViewPagerScroll(this);
}

public void setScrollDuration(int duration){
mPagerScroller.setScrollDuration(duration);
mPagerScroller.initViewPagerScroll(this);
}

public BannerViewPager startAutoPlay() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.wanglu1209.bannerlibrary;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import java.lang.reflect.Field;

/**
* Created by WangLu on 2017/3/15.
*/

public class ViewPagerScroller extends Scroller {
private int mScrollDuration = 1000; // 滑动速度

public ViewPagerScroller(Context context) {
super(context);
}

public ViewPagerScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}

public ViewPagerScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, mScrollDuration);
}

@Override
public void startScroll(int startX, int startY, int dx, int dy) {
super.startScroll(startX, startY, dx, dy, mScrollDuration);
}

public void initViewPagerScroll(ViewPager viewPager) {
try {
Field mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
mScroller.set(viewPager, this);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 设置速度速度
*
* @param duration
*/
public void setScrollDuration(int duration) {
this.mScrollDuration = duration;
}
}

0 comments on commit b078e41

Please sign in to comment.