Kurukshetran
8/19/2016 - 3:34 PM

PageTransformer

PageTransformer

public class MainActivity extends FragmentActivity {

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will keep every loaded fragment in memory.
	 * If this becomes too memory intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// activity_main.xml should contain a ViewPager with the id "@+id/pager"
		setContentView(R.layout.activity_main);

		// Create the adapter that will return a fragment for each of the three
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		// set the card transformer and set reverseDrawingOrder to true, so the fragments are drawn from the right to
		// the left
		mViewPager.setPageTransformer(true, new CardTransformer(0.7f));

	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the sections/tabs/pages.
	 */
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = new DummyFragment();
			return fragment;
		}

		@Override
		public int getCount() {
			return 10;
		}
	}

	/**
	 * A dummy fragment
	 */
	public static class DummyFragment extends Fragment {

		public DummyFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

			LinearLayout root = new LinearLayout(getActivity());
			root.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			root.setOrientation(LinearLayout.VERTICAL);

			for (int r = 0; r < 5; r++) {

				LinearLayout row = new LinearLayout(getActivity());
				row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1.0f));
				row.setOrientation(LinearLayout.HORIZONTAL);
				row.setGravity(Gravity.CENTER);

				for (int c = 0; c < 4; c++) {

					ImageView icon = new ImageView(getActivity());
					icon.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f));
					icon.setScaleType(ScaleType.CENTER);
					icon.setImageResource(R.drawable.ic_launcher);

					row.addView(icon);

				}

				root.addView(row);

			}

			return root;

		}
	}

	/**
	 * Awesome Launcher-inspired page transformer
	 * 
	 */
	public class CardTransformer implements PageTransformer {

		private final float scalingStart;

		public CardTransformer(float scalingStart) {
			super();
			this.scalingStart = 1 - scalingStart;
		}

		@Override
		public void transformPage(View page, float position) {

			if (position >= 0) {
				final int w = page.getWidth();
				float scaleFactor = 1 - scalingStart * position;

				page.setAlpha(1 - position);
				page.setScaleX(scaleFactor);
				page.setScaleY(scaleFactor);
				page.setTranslationX(w * (1 - position) - w);
			}
		}
	}

}