美文网首页Android开发常用代码
ViewPager水平缩放滑动图片效果

ViewPager水平缩放滑动图片效果

作者: 才兄说 | 来源:发表于2017-06-12 14:54 被阅读88次

    xml:

    <android.support.v4.view.ViewPager
            android:id="@+id/vp_guide"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    

    使用:

    ViewPager mViewPager = (ViewPager) findViewById(R.id.vp_guide);
    mViewPager.setOffscreenPageLimit(4-1);//缓存,根据需求自定义
    mViewPager.setPageMargin(-200);//处理两边碎片显示
    mViewPager.setPageTransformer(true, new ZoomOutPageTransformer());
    mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public int getCount() {
            return 4;
        }
    
        @Override
        public Fragment getItem(int position) {
            return new PageFragment();
        }
    });
    

    关键api:

    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.85f;
        private static final float MIN_ALPHA = 0.5f;
    
        /**
         * 当ViewPager滑动时,每一个界面都会回掉该方法,并且该方法跟随滑动不断回调
         * 参数:
         * view:当前停留显示的页面(注意:下面的循环不要直接用该view,因为该view会被自动加上一层FrameLayout布局)
         * position:当前页面滑动的位置,以当前页面的左侧边为0,两边终点分别为-1和1
         */
        @SuppressLint("NewApi")
        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) // a页滑动至b页 ; a页从 0.0 -1 ;b页从1 ~ 0.0
            { // [-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) {
                    //api版本>=11,view相对原始位置的偏移量,不影响view属性
                    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 PageFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_page, null);
            return view;
        }
    }
    

    碎片fragment_page.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
    
        <ImageView
            android:src="@mipmap/page_icon"
            android:layout_width="250dp"
            android:layout_height="250dp" />
    
    </LinearLayout>
    

    补充:page_icon替换成自己的图片

    相关文章

      网友评论

      • 冰冰的冻结:我的app 如果默认左边没有 只有中间和右边时,第一次进来右侧不会缩放,向左滑动后才 会显示正常
        冰冰的冻结:@天鬼 我发现如果初始化假数据放到了oncreate 里而不是等待接口成功后初始化数据,我的view 显示也是正常的
        才兄说:你是用了相同的代码?

      本文标题:ViewPager水平缩放滑动图片效果

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