ViewPager几乎在每一个app中都会有使用,可以做导航页,也可以做广告、图片的播放。最近在做的app也用到了ViewPager,具体要实现的功能是能够无限轮播,并且在自动轮播时,图片在切换时能够有动画效果,当手指触摸时停止自动轮播,手指抬起时继续自动轮播。
闲话不多说,直接上代码
自定义ViewPager:
public class MyViewPager extends ViewPager { // 当前页 private int currentItem; // ViewPagerAdapter实例 private ViewPagerAdapter mAdapter; public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setAdapter(PagerAdapter adapter) { mAdapter = (ViewPagerAdapter) adapter; super.setAdapter(mAdapter); } /** * 获取当前页 * * @return */ public int getCurrentItem() { return mAdapter.getMyPosition(); } /** * 获取MyViewPager实例 * * @return */ public MyViewPager getMyViewPager() { return mAdapter.getMyViewPager(); } /** * 设置当前页 * * @param item * @param smoothScroll */ public void setCurrentItem(int item, boolean smoothScroll) { super.setCurrentItem(item, smoothScroll); } // 是否自动轮播 private boolean isStart = true; // Handler private Handler mHandler = new Handler(); // 图片自动轮播Task private Runnable mImageTimerTask = new Runnable() { @Override public void run() { try {// 自定义图片切换速度 Field field = ViewPager.class.getDeclaredField("mScroller"); field.setAccessible(true); FixedSpeedScroller scroller = new FixedSpeedScroller( getContext(), new AccelerateInterpolator()); field.set(getMyViewPager(), scroller); scroller.setmDuration(500); } catch (Exception e) { e.printStackTrace(); } currentItem = getCurrentItem(); int amount = currentItem; /** * 解决往左滑动后变成向左自动轮播的bug */ if(oldCurrentItem < currentItem){ setCurrentItem(currentItem, true); }else{ currentItem += 2; setCurrentItem(currentItem, true); } oldCurrentItem = amount; if (isStart) mHandler.postDelayed(mImageTimerTask, 2000); } }; /** * 触摸事件: ACTION_DOWN 停止自动轮播 ACTION_UP 开始自动轮播 */ @Override public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { // 开始图片滚动 startImageTimerTask(); } else if (event.getAction() == MotionEvent.ACTION_DOWN) { // 停止图片滚动 stopImageTimerTask(); } return super.dispatchTouchEvent(event); } /** * 停止自动轮播 */ public void stopImageTimerTask() { isStart = false; mHandler.removeCallbacks(mImageTimerTask); } /** * 开始自动轮播 */ public void startImageTimerTask() { isStart = true; mHandler.postDelayed(mImageTimerTask, 2000); } }
自定义PagerAdapter:
public class ViewPagerAdapter extends PagerAdapter { private List
切换动画google给的DepthPageTransformer:
public class DepthPageTransformer implements PageTransformer { private static final float MIN_SCALE = 0.75f; @Override public void transformPage(View view, float position) { int pageWidth = view.getWidth(); if (position < -1) { view.setAlpha(0); } else if (position <= 0) { view.setAlpha(1); view.setTranslationX(0); view.setScaleX(1); view.setScaleY(1); } else if (position <= 1) { view.setAlpha(1 - position); view.setTranslationX(pageWidth * -position); float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position)); view.setScaleX(scaleFactor); view.setScaleY(scaleFactor); } else { view.setAlpha(0); } } }
切换速度自定义:
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) { super.startScroll(startX, startY, dx, dy, mDuration); } @Override public void startScroll(int startX, int startY, int dx, int dy) { super.startScroll(startX, startY, dx, dy, mDuration); } public void setmDuration(int time) { mDuration = time; } public int getmDuration() { return mDuration; } }
小圆点的变换:
public void onPageSelected(int position) { dots.get(oldPosition % viewList.size()).setBackgroundResource( R.drawable.dot_normal); dots.get(position % viewList.size()).setBackgroundResource( R.drawable.dot_focused); oldPosition = position; }
最后:
mViewPager.setCurrentItem(500); mViewPager.setPageTransformer(true, new DepthPageTransformer()); mViewPager.startImageTimerTask();