ViewPager.setPagerTransformer设置各种翻滚动画效果

最近看到ViewPager.setPagerTransformer可以设置一些滑入滑出的类似Android桌面滑动效果的动画效果,就在业余时间写了一个简单的demo;实现的滑动动画效果如图:


具体代码如下:

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * 通过ViewPager的setPagerTransformer来设置viewpager滑动过程中的各种动画效果
 */
public class MainActivity extends Activity {
    private int[] pictures = {
            R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d,
            R.drawable.e
    };
    private ArrayList list;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        list = new ArrayList<>();
        for (int i = 0; i < pictures.length; i++) {
            ImageView iv = new ImageView(getApplicationContext());
            iv.setScaleType(ImageView.ScaleType.FIT_XY);//设置图片拉伸
            iv.setImageResource(pictures[i]);
            list.add(iv);
        }
        viewPager.setAdapter(new MyPagerAdapter());
        viewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
            /**
             * 设置ViewPager滑动时的动画
             * @param page 要执行动画的View对象
             * @param position 滑动的View的一种状态,当a向左滑出b滑入时,a对应的position为(0,-1],b对应的position为[1,0]
             */
            @Override
            public void transformPage(View page, float position) {
                if (position<-1){
                    page.setRotationY(0);
                }else if (position<=0){ //[-1,0]a向左滑出
                    page.setScaleX(1-Math.abs(position));//设置缩放
                    page.setScaleY(1-Math.abs(position));
                    page.setRotationY(-30*position); //设置以Y轴旋转动画

                }else if (position<=1){ //[0,1]b滑入
                    page.setTranslationX(-page.getWidth()*position);//设置向左平移动画,page.getWidth()为页面宽度
                    page.setScaleX(Math.max(0.7f,Math.abs(position)));
                    page.setScaleY(Math.max(0.7f,Math.abs(position)));
                    page.setRotationY(-30*position);
                }else {
                    page.setRotationY(0);
                }
            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

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

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}


可以参考:http://gqdy365.iteye.com/blog/2114968

你可能感兴趣的:(Android)