jfeinstein10 / JazzyViewPager

An easy to use ViewPager that adds an awesome set of custom swiping animations. Just change your ViewPagers to JazzyViewPagers, two more steps, and you're good to go!
Apache License 2.0
2.67k stars 812 forks source link

Request: add animations as shown on Google's example #41

Open AndroidDeveloperLB opened 10 years ago

AndroidDeveloperLB commented 10 years ago

This is just a request:

Please add animations as shown on Google's example: http://developer.android.com/training/animation/screen-slide.html#pagetransformer

Here's what I suggest, and I think it works fine :

I've added an enum value "GOOGLE_ZOOM" , and used it just like any other enum value was used on this library (BTW, why weren't they valued in capital letters, as used in the conventions of enum values, and why did you use a string array ?) .

here's what i've tried (inside JazzyViewPager.java ) :

@Override
public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
    ...
    case GOOGLE_ZOOM:
        animateGoogleZoom(mLeft, mRight, effectOffset);
        break;
    }
    ...

private void animateGoogleZoom(final View left, final View right, final float positionOffset) {
    for (int i = 0; i < 2; ++i) {
        final View view = i == 0 ? left : right;
        if (view == null)
            continue;
        final float position = i == 0 ? positionOffset : 1 - positionOffset;
        final int pageWidth = view.getWidth();
        final int pageHeight = view.getHeight();
        final float MIN_SCALE = 0.85f;
        final float MIN_ALPHA = 0.5f;

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            ViewHelper.setAlpha(view, 0);
        } else if (position <= 1) { // [-1,1]
            // Modify the default slide transition to shrink the page as well
            final float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            final float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            final float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                ViewHelper.setTranslationX(view, horzMargin - vertMargin / 2);
            } else {
                ViewHelper.setTranslationX(view, -horzMargin + vertMargin / 2);
            }

            // Scale the page down (between MIN_SCALE and 1)
            ViewHelper.setScaleX(view, scaleFactor);
            ViewHelper.setScaleY(view, scaleFactor);

            // Fade the page relative to its size.
            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.
            ViewHelper.setAlpha(view, 0);
        }
    }
}