Skip to content

Instantly share code, notes, and snippets.

@hernan
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hernan/1293180deb01a3a1a706 to your computer and use it in GitHub Desktop.
Save hernan/1293180deb01a3a1a706 to your computer and use it in GitHub Desktop.
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);
}
}
}
public class ScaleFadePageTransformer implements PageTransformer {
private int mScreenXOffset;
public ScaleFadePageTransformer(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
mScreenXOffset = display.getWidth()/2;
}
@SuppressLint("NewApi")
@Override
public void transformPage(View page, float position) {
final float transformValue = Math.abs(Math.abs(position) - 1);
// apply fade effect
page.setAlpha(transformValue);
if (position > 0) {
// apply zoom effect only for pages to the right
page.setScaleX(transformValue);
page.setScaleY(transformValue);
page.setPivotX(0.5f);
final float translateValue = position * -mScreenXOffset;
if (translateValue > -mScreenXOffset) {
page.setTranslationX(translateValue);
} else {
page.setTranslationX(0);
}
}
}
}
// ------------------------
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);
} 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);
} 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment