Android ViewPager With different Animation like zo

2020-02-08 06:35发布

问题:

I have created ViewPager ,there are only 2 page are there,I want to put animation like fade in,scale,zoom,3d etc when pager scroll one page to another second one,means second entire page display with specific animation , I have no idea to how to do this,please any one give me a some example of put animation when move to another page that time we animate pager.

Mainly I have three class for pager :1) ViewPagerMainActivity : it call two fragment that is swipe in this main look like enter code here

   public class ViewPagerMainActivity extends FragmentActivity implements
    OnClickListener, OnPageChangeListener {

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_pager_main);
                                     <other code here>
     }

private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(
            android.support.v4.app.FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int pos) {
        switch (pos) {

        case 0:
            return FirstFragment.newInstance("");

        case 1:
            return SecondFragment.newInstance("");

        default:
            return SecondFragment.newInstance("Default");
        }
    }

    @Override
    public int getCount() {
        return 2; // return no of fragment created by us
    }
}

}

In first fragment containing swipe design and second fragment contains second xml file design when I swipe first fragment to another at that time I want to put animation. means while swiping it display any one animation effect choose from different animation option. how to I create animation and where to put this in it. please help quickly thanks in advance.

回答1:

You can attach PagerTransformer to ViewPager :

         mViewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
         @Override
         public void transformPage(View page, float position) {
                 // do transformation here
          }
         });

You can go through following links which will help you :

http://developer.android.com/training/animation/screen-slide.html#pagetransformer or http://andraskindler.com/blog/2013/create-viewpager-transitions-a-pagertransformer-example/