之前做引导页的时候,一直使用的是ViewPager, 只是后来滑动的时候会卡顿的情况。看了半天,才发现viewPager是重量级的控件,一般和fragment来配套使用。看ViewPager的官方介绍就知道,以下截取了一点:
/** * Layout manager that allows the user to flip left and right * through pages of data. You supply an implementation of a * {@link PagerAdapter} to generate the pages that the view shows. * * <p>Note this class is currently under early design and * development. The API will likely change in later updates of * the compatibility library, requiring changes to the source code * of apps when they are compiled against the newer version.</p> * * <p>ViewPager is most often used in conjunction with {@link android.app.Fragment}, * which is a convenient way to supply and manage the lifecycle of each page. * There are standard adapters implemented for using fragments with the ViewPager, * which cover the most common use cases. These are * {@link android.support.v4.app.FragmentPagerAdapter} and * {@link android.support.v4.app.FragmentStatePagerAdapter}; each of these * classes have simple code showing how to build a full user interface * with them. * * <p>For more information about how to use ViewPager, read <a * href="{@docRoot}training/implementing-navigation/lateral.html">Creating Swipe Views with * Tabs</a>.</p> * * <p>Below is a more complicated example of ViewPager, using it in conjunction * with {@link android.app.ActionBar} tabs. You can find other examples of using * ViewPager in the API 4+ Support Demos and API 13+ Support Demos sample code. * * {@sample development/samples/Support13Demos/src/com/example/android/supportv13/app/ActionBarTabsPager.java * complete} */ public class ViewPager extends ViewGroup {
红字的的部分可以很清新的表达。。应该不需要多解释!
好,下面来看看ViewFlipper。。
/** * Simple {@link ViewAnimator} that will animate between two or more views * that have been added to it. Only one child is shown at a time. If * requested, can automatically flip between each child at a regular interval. * * @attr ref android.R.styleable#ViewFlipper_flipInterval * @attr ref android.R.styleable#ViewFlipper_autoStart */ @RemoteView public class ViewFlipper extends ViewAnimator {
/** * Base class for a {@link FrameLayout} container that will perform animations * when switching between its views. * * @attr ref android.R.styleable#ViewAnimator_inAnimation * @attr ref android.R.styleable#ViewAnimator_outAnimation * @attr ref android.R.styleable#ViewAnimator_animateFirstView */ public class ViewAnimator extends FrameLayout {
它的解释很简单,它继承于ViewAnimator,可以在两个或多个View中切换,当然也可以自定义切换动画。如果有需要的话,可以在各个View之间自动切换。最后这点很有意思哦,一下子让我想到了轮播广告的显示,以后有时间写个demo看看。
好,回到我们的引导页面来。。
原理:
根据页面的手势的滑动距离来判断是左滑,还是右滑。然后进行切换页面
主要代码如下:
@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if(e1.getX()-e2.getX()>120){ if (viewFlipper.getDisplayedChild() == 2){ return false; } viewFlipper.setInAnimation(leftInAnimation); viewFlipper.setOutAnimation(leftOutAnimation); viewFlipper.showNext();//向右滑动 showCurrent(viewFlipper.getDisplayedChild()); return true; }else if(e1.getX()-e2.getY()<-120){ if (viewFlipper.getDisplayedChild() == 0){ return false; } viewFlipper.setInAnimation(rightInAnimation); viewFlipper.setOutAnimation(rightOutAnimation); viewFlipper.showPrevious();//向左滑动 showCurrent(viewFlipper.getDisplayedChild()); return true; } return false; }
另附上Demo路径:
...