How to swipe images

前端 未结 7 1193
孤城傲影
孤城傲影 2021-02-05 14:34

I am trying to implement a simple gallery of images in which I have an image to be displayed at a time on the device screen. When we swipe the screen from left to right it shoul

7条回答
  •  渐次进展
    2021-02-05 15:18

    This is how i achieved:

    package com.demoimageslider;
    
    import java.util.List;
    import java.util.Vector;
    
    import com.nineoldandroids.view.ViewHelper;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    
    public class MainActivity extends FragmentActivity {
    
        private PagerAdapter mPagerAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            this.initialisePaging();
        }
    
        private void initialisePaging() {
    
            List fragments = new Vector();
            fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
            fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName()));
            fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName()));
            this.mPagerAdapter = new PagerAdapter(
                    super.getSupportFragmentManager(), fragments);
    
            ViewPager pager = (ViewPager) super.findViewById(R.id.pager);
            pager.setAdapter(this.mPagerAdapter);
            pager.setPageTransformer(true, new ZoomOutPageTransformer());
        }
    
        public static class Tab1Fragment extends Fragment {
    
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                if (container == null) {
                    return null;
                }
                return (ImageView) inflater.inflate(R.layout.tab_frag1_layout,
                        container, false);
            }
        }
    
        public static class Tab2Fragment extends Fragment {
    
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                if (container == null) {
                    return null;
                }
                return (ImageView) inflater.inflate(R.layout.tab_frag2_layout,
                        container, false);
            }
        }
    
        public static class Tab3Fragment extends Fragment {
    
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                if (container == null) {
                    return null;
                }
                return (ImageView) inflater.inflate(R.layout.tab_frag3_layout,
                        container, false);
            }
        }
    
        public static class PagerAdapter extends FragmentPagerAdapter {
    
            private List fragments;
    
            public PagerAdapter(FragmentManager fm, List fragments) {
                super(fm);
                this.fragments = fragments;
            }
    
            @Override
            public Fragment getItem(int position) {
                return this.fragments.get(position);
            }
    
            @Override
            public int getCount() {
                return this.fragments.size();
            }
        }
    
        public static class ZoomOutPageTransformer implements
                ViewPager.PageTransformer {
            private static final float MIN_SCALE = 0.85f;
            private static final 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)
                    // This page is way off-screen to the left.
                    // view.setAlpha(0);
                    ViewHelper.setAlpha(view, 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);
                        ViewHelper.setTranslationX(view, horzMargin - vertMargin
                                / 2);
                    } else {
                        // view.setTranslationX(-horzMargin + vertMargin / 2);
                        ViewHelper.setTranslationX(view, -horzMargin + vertMargin
                                / 2);
                    }
    
                    // Scale the page down (between MIN_SCALE and 1)
                    // view.setScaleX(scaleFactor);
                    // view.setScaleY(scaleFactor);
                    ViewHelper.setScaleX(view, scaleFactor);
                    ViewHelper.setScaleY(view, scaleFactor);
    
                    // Fade the page relative to its size.
                    // view.setAlpha(MIN_ALPHA +
                    // (scaleFactor - MIN_SCALE) /
                    // (1 - MIN_SCALE) * (1 - MIN_ALPHA));
    
                    ViewHelper.setAlpha(view, 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);
                    ViewHelper.setAlpha(view, 0);
                }
            }
        }
    }
    

    I used nineoldroids library to support effects on older android versions.

提交回复
热议问题