ViewPager的动画效果(学习自慕课网)

先上效果一:


先上效果二:

先上效果三:

ViewPager的动画效果(学习自慕课网)_第1张图片

首先定义好viewPager控件,其次,是调用一行代码实现动画效果,

pager.setPageTransformer(true,new DepthPageTransformer());

前提是需要做一些准备操作的,且只有api3.0以上才可以有效。

注:因为属性动画是3.0以后才出现的,如果想在3.0以下的手机上实现动画,需要做一些更改,用nineoldandroids框架来代替属性动画。这里先不向下兼容了。如有想向下兼容的朋友,可以访问宏阳大神的博客,地址  http://blog.csdn.net/lmj623565791/article/details/40411921/

public class MainActivity extends AppCompatActivity {

    private ViewPager pager;
    private int[] mImgIds=new int[]{R.drawable.ic_guaid1,R.drawable.ic_guaid2,R.drawable.ic_guaid3,R.drawable.ic_guaid4};
   private List<ImageView> mImages=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager = ((ViewPager) findViewById(R.id.pager));
        //为viewpager添加切换动画效果(api3.0以上)
//效果三 // pager.setPageTransformer(true,new ZoomOutPageTransformer());
//效果二
// pager.setPageTransformer(true,new RotateDownPageTransformer());
//效果一
pager.setPageTransformer(true,new DepthPageTransformer());
 
pager.setOffscreenPageLimit(3); pager.setAdapter(new PagerAdapter() { @Override public int getCount() { return mImgIds.length; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(mImgIds[position]); container.addView(imageView); return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((ImageView)object); } }); }}
public class DepthPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();

        if (position < -1) { // [-Infinity,-1)  // This page is way off-screen to the left.  view.setAlpha(0);

        } else if (position <= 0) { // [-1,0]  // Use the default slide transition when moving to the left page  view.setAlpha(1);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);

        } else if (position <= 1) { // (0,1]  // Fade the page out.  view.setAlpha(1 - position);

            // Counteract the default slide transition  view.setTranslationX(pageWidth * -position);

            // Scale the page down (between MIN_SCALE and 1)  float scaleFactor = MIN_SCALE  + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]  // This page is way off-screen to the right.  view.setAlpha(0);
        }
    }
}

public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.85f;
    private static final float MIN_ALPHA = 0.5f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        if (position < -1) { // [-Infinity,-1)  // This page is way off-screen to the left.  view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]  // Modify the default slide transition to shrink the page as well  float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                view.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 2);
            }

            // Scale the page down (between MIN_SCALE and 1)  view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // Fade the page relative to its size.  view.setAlpha(MIN_ALPHA +
                    (scaleFactor - MIN_SCALE) /
                    (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else { // (1,+Infinity]  // This page is way off-screen to the right.  view.setAlpha(0);
        }
    }
}
public class RotateDownPageTransformer implements ViewPager.PageTransformer
{  
      
    private static final float ROT_MAX = 20.0f;  
    private float mRot;  
      
  
      
    public void transformPage(View view, float position)
    {  
  
        Log.e("TAG", view + " , " + position + "");
  
        if (position < -1)  
        { // [-Infinity,-1)  // This page is way off-screen to the left.  view.setRotation( 0);
  
        } else if (position <= 1) // a页滑动至b页 ; a页从 0.0 ~ -1 ;b页从1 ~ 0.0  { // [-1,1]  // Modify the default slide transition to shrink the page as well  if (position < 0)  
            {  
  
                mRot = (ROT_MAX * position);
                view.setPivotX( view.getMeasuredWidth() * 0.5f);
                view.setPivotY( view.getMeasuredHeight());
                view.setRotation( mRot);
            } else  {  
  
                mRot = (ROT_MAX * position);
                view.setPivotX( view.getMeasuredWidth() * 0.5f);
                view.setPivotY(view.getMeasuredHeight());
                view.setRotation( mRot);
            }  
  
            // Scale the page down (between MIN_SCALE and 1)   // Fade the page relative to its size.   } else  { // (1,+Infinity]  // This page is way off-screen to the right.  view.setRotation( 0);
        }  
    }  
}  



你可能感兴趣的:(ViewPager的动画效果(学习自慕课网))