码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager切换动画PageTransformer的使用

时间:2015-07-16 16:45:21      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:viewpager   动画   pagetransformer   

Android从3.0开始添加了属性动画后,诸多难以实现的动画都可以轻松解决了,v4包下的ViewPager控件当然也不例外,相对于非常平庸的默认切换动画,Google官方给我们展示了两个动画例子:DepthPageTransformer和ZoomOutPageTransformer,比较炫。

实际上我们也可以通过实现ViewPager.PageTransformer接口来做出完全不同的切换动画效果。实现PageTransformer接口就只要实现一个方法:transformPage(View view,float position);该方法的两个参数尤为重要,理解它是我们自定义ViewPager动画的重点。

transformPage(View view,float position);

  • 第一个参数view顾名思义就是我们需要滑动的那个View。
  • 第二个参数position,这个float类型的参数并不是我们平常所理解的位置,而是表示滑动的View的一种状态,比如:当滑动到正全屏时,position是0,而向左滑动,使得右边刚好有一部被进入屏幕时,position是1,如果前一页和下一页基本各在屏幕占一半时,前一页的position是-0.5,后一页的posiotn是0.5,所以根据position的值我们就可以自行设置需要的alpha,ScaleX/Y,translationX/Y等参数值。

下面,我们就来看看Google官方提供的两种动画效果的源码及对应的动画,理解了原理自定义动画可以仿照源码了:

DepthPageTransformer:

import android.annotation.SuppressLint;
import android.support.v4.view.ViewPager.PageTransformer;
import android.view.View;

public class DepthPageTransformer implements PageTransformer {
    private static float MIN_SCALE = 0.75f;

    @SuppressLint("NewApi")
    @Override
    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);

        }
    }

}
效果为:
技术分享


ZoomOutPageTransformer:

import android.support.v4.view.ViewPager.PageTransformer;
import android.view.View;

public class ZoomOutPageTransformer implements PageTransformer {
    private static float MIN_SCALE = 0.85f;

    private static float MIN_ALPHA = 0.5f;

    @Override
    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);
        }
    }
}
效果为:

技术分享

下面我们来看看怎么使用ViewPager,让它加入动画。

其实就是通过mViewPager.setPageTransformer(true, new DepthPageTransformer());来设置。

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

代码:

public class MainActivity extends ActionBarActivity {
    private int[] imageId = new int[]{R.mipmap.bg,R.mipmap.one, R.mipmap.two, R.mipmap.three};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(new ViewPagerAdapter(this,imageId));
//      mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
        mViewPager.setPageTransformer(true,new DepthPageTransformer());
    }

    class ViewPagerAdapter extends PagerAdapter {
        private ArrayList<ImageView> mImages ;
        private int[] imagesId;
        private Context context;
        public ViewPagerAdapter(Context context,int[] resourceId){
            this.context = context;
            this.imagesId = resourceId;
            mImages = new ArrayList<>();
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imagesId[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            container.addView(imageView);
            mImages.add(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);//这行要去除,否则会报错
            container.removeView(mImages.get(position));
        }

        @Override
        public int getCount() {
            return imagesId.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
}

另外说明一点:

关于ViewPager的适配器,有两种实现:

  • 直接继承PagerAdapter,即:
class ViewPagerAdapter extends PagerAdapter {
        private ArrayList<ImageView> mImages ;
        private int[] imagesId;
        private Context context;
        public ViewPagerAdapter(Context context,int[] resourceId){
            this.context = context;
            this.imagesId = resourceId;
            mImages = new ArrayList<>();
        }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imagesId[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            container.addView(imageView);
            mImages.add(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);//这行要去除,否则会报错
            container.removeView(mImages.get(position));
        }

        @Override
        public int getCount() {
            return imagesId.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
  • 继承至FragmentPagerAdapter:即
class FragmentViewPagerAdapter extends FragmentPagerAdapter{
        private final List<Fragment> mFragments = new ArrayList<>();//添加的Fragment的集合
        public FragmentViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        public void addFragment(Fragment fragment) {
            mFragments.add(fragment);
        }
        @Override
        public Fragment getItem(int position) {
            //得到对应position的Fragment
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            //返回Fragment的数量
            return mFragments.size();
        }
    }

好了,就说到这了。。。


版权声明:本文为博主原创文章,未经博主允许不得转载。

ViewPager切换动画PageTransformer的使用

标签:viewpager   动画   pagetransformer   

原文地址:http://blog.csdn.net/u010687392/article/details/46912255

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!