美文网首页
viewPager实现显示3个item

viewPager实现显示3个item

作者: 木小伍 | 来源:发表于2019-10-12 20:07 被阅读0次

    效果图


    微信图片_20190830162638.jpg
    import android.support.annotation.NonNull;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    
    /**
     * @ClassName: BannerTransformer
     * @Author: 伍跃武
     * @Date: 2019/4/11 9:44
     * @Description: viewPager 的偏移量
     */
    public class BannerTransformer implements ViewPager.PageTransformer {
        private static final float mMinScale = 0.7f;
        public static final float DEFAULT_CENTER = 0.5f;
        @Override
    
        public void transformPage(@NonNull View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();
    
            view.setPivotY(pageHeight / 2);
            view.setPivotX(pageWidth / 2);
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setScaleX(mMinScale);
                view.setScaleY(mMinScale);
                view.setPivotX(pageWidth);
            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as well
                if (position < 0) //1-2:1[0,-1] ;2-1:1[-1,0]
                {
                    float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
                    view.setScaleX(scaleFactor);
                    view.setScaleY(scaleFactor);
                    view.setPivotX(pageWidth * (DEFAULT_CENTER + (DEFAULT_CENTER * -position)));
                } else //1-2:2[1,0] ;2-1:2[0,1]
                {
                    float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
                    view.setScaleX(scaleFactor);
                    view.setScaleY(scaleFactor);
                    view.setPivotX(pageWidth * ((1 - position) * DEFAULT_CENTER));
                }
            } else { // (1,+Infinity]
                view.setPivotX(0);
                view.setScaleX(mMinScale);
                view.setScaleY(mMinScale);
            }
        }
    }
    

    使用方法:

          viewPager.setPageTransformer(true,new BannerTransformer());
    

    相关文章

      网友评论

          本文标题:viewPager实现显示3个item

          本文链接:https://www.haomeiwen.com/subject/sydoectx.html