Android官方提供的两种viewpager滑动动画

1.ZoomOutPageTransformer

package com.gelonghui.glhapp.view;

import android.view.View;

public class ZoomOutPageTransformer implements android.support.v4.view.ViewPager.PageTransformer {
    private static float MIN_SCALE = 0.85f;
    private static 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)
            // 这个页面正在向左远离屏幕
            view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]
            // 修改默认幻灯片动画,缩小页面
            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);
            }

            // 按比例缩小页面(比例在 MIN_SCALE 和 1之间)
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // 相对于其尺寸进行渐隐
            view.setAlpha(MIN_ALPHA +
                    (scaleFactor - MIN_SCALE) /
                    (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else { // (1,+Infinity]
            // 该页面正在向右远离屏幕
            view.setAlpha(0);
        }
    }
}

2.DepthPageTransformer

package com.gelonghui.glhapp.view;


import android.view.View;


public class DepthPageTransformer implements android.support.v4.view.ViewPager.PageTransformer {
    private static float MIN_SCALE = 0.75f;


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


        if (position < -1) { // [-Infinity,-1)
            // 页面正在向左离开屏幕
            view.setAlpha(0);


        } else if (position <= 0) { // [-1,0]
            // 使用正常的滑动效果,
            view.setAlpha(1);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);


        } else if (position <= 1) { // (0,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 { // (1,+Infinity]
            // 页面向右离开屏幕
            view.setAlpha(0);
        }
    }
}

3.mViewPager.setPageTransformer(true, new DepthPageTransformer());

你可能感兴趣的:(Android官方提供的两种viewpager滑动动画)