ViewPager animation fade in/out instead of slide

42,066

Solution 1

This should work better for the fade in/out transform:

public void transformPage(View view, float position) {
    view.setTranslationX(view.getWidth() * -position);
       
    if(position <= -1.0F || position >= 1.0F) {
        view.setAlpha(0.0F);
    } else if( position == 0.0F ) {
        view.setAlpha(1.0F);
    } else { 
        // position is between -1.0F & 0.0F OR 0.0F & 1.0F
        view.setAlpha(1.0F - Math.abs(position));
    }
}

Solution 2

Based on a @murena's answer, this should work better for the fade in/out transform. At the end of animation the position is restored to default value.

public void transformPage(View view, float position) {
    if(position <= -1.0F || position >= 1.0F) {
        view.setTranslationX(view.getWidth() * position);
        view.setAlpha(0.0F);
    } else if( position == 0.0F ) {
        view.setTranslationX(view.getWidth() * position);
        view.setAlpha(1.0F);
    } else { 
        // position is between -1.0F & 0.0F OR 0.0F & 1.0F
        view.setTranslationX(view.getWidth() * -position);
        view.setAlpha(1.0F - Math.abs(position));
    }
}
  

Solution 3

Fade according to Google: https://developer.android.com/training/animation/reveal-or-hide-view

viewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
    @Override
    public void transformPage(@NonNull View page, float position) {
        page.setAlpha(0f);
        page.setVisibility(View.VISIBLE);

        // Start Animation for a short period of time
        page.animate()
            .alpha(1f)
            .setDuration(page.getResources().getInteger(android.R.integer.config_shortAnimTime));
    }
});

Solution 4

A simple fading transformer could be implemented like this:

private static class FadePageTransformer implements ViewPager.PageTransformer {
    public void transformPage(View view, float position) {
        view.setAlpha(1 - Math.abs(position));
        if (position < 0) {
            view.setScrollX((int)((float)(view.getWidth()) * position));
        } else if (position > 0) {
            view.setScrollX(-(int) ((float) (view.getWidth()) * -position));
        } else {
            view.setScrollX(0);
        }
    }
}

Maybe not exactly what your looking for, but a good starting point.

Solution 5

I found @murena's answer super helpful. However, I ran into a problem when I tried to add an OnClickListener to each tab. I needed each page to open a different URL link. I modified the code to hide the views that are out of sight and added a little extra effect to slide the view at .5 the speed:

public void transformPage(View view, float position) {

    if (position <= -1.0F || position >= 1.0F) {        // [-Infinity,-1) OR (1,+Infinity]
        view.setAlpha(0.0F);
        view.setVisibility(View.GONE);
    } else if( position == 0.0F ) {     // [0]
        view.setAlpha(1.0F);
        view.setVisibility(View.VISIBLE);
    } else {

        // Position is between [-1,1]
        view.setAlpha(1.0F - Math.abs(position));
        view.setTranslationX(-position * (view.getWidth() / 2));
        view.setVisibility(View.VISIBLE);
    }
}
Share:
42,066
KKO
Author by

KKO

Updated on July 09, 2022

Comments

  • KKO
    KKO almost 2 years

    I got the FragmentBasics example from here. Is there a way make the ViewPager animation simply fade in and out when I swipe instead of sliding left and right? I've been trying some stuff with PageTransformer, but no success, I can still see it sliding. So I guess I need to somehow force its position to stay put, while sliding my finger only affects the alpha.

    public class SecondActivity extends Activity {
    
    SectionsPagerAdapter mSectionsPagerAdapter;
    
    ViewPager mViewPager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
    
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setPageTransformer(false, new FadePageTransformer());
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
    
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }
    
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }
    
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";
    
        /**
         * Returns a new instance of this fragment for the given section number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_second, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
    
    public class FadePageTransformer implements ViewPager.PageTransformer {
            public void transformPage(View view, float position) {
    
                if (position < -1 || position > 1) {
                    view.setAlpha(0);
                }
                else if (position <= 0 || position <= 1) {
                    // Calculate alpha. Position is decimal in [-1,0] or [0,1]
                    float alpha = (position <= 0) ? position + 1 : 1 - position;
                    view.setAlpha(alpha);
                }
                else if (position == 0) {
                    view.setAlpha(1);
                }
            }
        }
    
  • android developer
    android developer about 9 years
    The scrolling part can be minimized to just: view.setScrollX(position == 0 ? 0 : (int) (Math.signum(position) * ((float) (view.getWidth()) * position)));
  • m02ph3u5
    m02ph3u5 almost 9 years
    It'd be great if you added some explanation / background information.
  • Nikolay Hristov
    Nikolay Hristov over 8 years
    Yes it is! It works even great! Thanks a lot for the idea!
  • Morteza Rastgoo
    Morteza Rastgoo over 8 years
    The answer above makes bad behaviour in inner views. this works better! thanks.
  • Morteza Rastgoo
    Morteza Rastgoo over 8 years
    This makes bad behaviour like avoid touch event in inner views in my case. @umberto_sidoti 's answer workes better.
  • Arlind
    Arlind over 8 years
    The animation doesnt work when I call setCurrentItem(0)
  • peter.bartos
    peter.bartos about 8 years
    This works better. Just a warning: There's a problem if Pager has a leftPadding set.
  • Israel Meshileya
    Israel Meshileya almost 6 years
    Is there a way i can do this for a viewpager that scrolls automatically, if yes, I will like to know the best way to go through this, pls. @stephan
  • A. N
    A. N almost 6 years
    where do you write this?
  • Cícero Moura
    Cícero Moura almost 6 years
    I had to add this line on proguard rules to get this working on release version: -keep public class android.support.v4.view.ViewPager { *; }
  • Cícero Moura
    Cícero Moura almost 6 years
    I had to add this line to the proguard rules to get this working: -keep public class android.support.v4.view.ViewPager { *; }
  • umberto_sidoti
    umberto_sidoti over 5 years
    @CíceroMoura Was a problem with proguard in your project but not related to the current question/solution.
  • Cícero Moura
    Cícero Moura over 5 years
    Yes, but I think it could help whoever wants to get this working on released apk considering that minify is enabled.
  • Anup
    Anup almost 5 years
    position is not restored using this and click position is going bonkers, instead use stackoverflow.com/a/32633721/2584794
  • Julius
    Julius over 4 years
    Much better & more modern than the accepted answer +1
  • HBG
    HBG over 4 years
    This answer is better because the above solutions expose intermediary pages during their fade transition. This implementation prevents that completely.
  • HBG
    HBG over 4 years
    If you have 3 pages in your ViewPager and you go from index 0 to 2, this implementation will show you a glimpse of the page at index 1 before landing on 2. This solution from @João Dinis addresses this.
  • Tom
    Tom over 2 years
    This will only fade the new page in - the old page will abruptly be removed