Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a Fade In/Out effect? #7

Open
paulocoutinhox opened this issue Apr 27, 2016 · 4 comments
Open

There is a Fade In/Out effect? #7

paulocoutinhox opened this issue Apr 27, 2016 · 4 comments
Assignees

Comments

@paulocoutinhox
Copy link

There is a Fade In/Out effect?

@geftimov
Copy link
Owner

geftimov commented May 9, 2016

No sorry , but you can create pr for it.

It should be relatively easy , but it is mostly based on what do you want to do.

@paulocoutinhox
Copy link
Author

Can you create a class for it?

/**
 * Set this PageTransformer on a ViewPager. It will be invoked anytime it's pages are scrolled.
 * This transformer has the effect of fading in and out pages on top of each other.
 */
public class FadePageTransformer implements ViewPager.PageTransformer {

    @Override
    public final void transformPage(final View view, final float position) {
        final int pageWidth = view.getWidth();

        /*
         * When a page's alpha is set to 0 it's visibility should also be set to gone.
         * Even though the view isn't visible it can still be interacted with if it isn't gone and is drawn on top.
         */

        /*
         * Position is checked right up next to -1 and 1. The reason is because sometimes the position doesn't seem to come
         * all the way through as a whole number. Meaning it seems it would stop so very close to -1 or 0 (for example) and
         * the code to make necessary views 'gone' never gets called. So then there could be an invisible view on top that is
         * still able to be interacted with.
         */

        if (position < -0.999f) { // [-Infinity,-1)
            // This page is way off-screen to the left so hide it.
            view.setAlpha(0);
            //view.setVisibility(View.GONE);
            view.setTranslationX(pageWidth);
        } else if (position <= 0.999f) { // (-1, 1)
            // The further the page is from being center page the more transparent it is.
            view.setAlpha(getAlpha(position));
            // Counteract the default slide transition
            view.setTranslationX(pageWidth * -position);
            // Make sure the page is visible
            //view.setVisibility(View.VISIBLE);
        } else { // (1,+Infinity]
            // This page is way off-screen to the right so hide it.
            view.setAlpha(0);
            //view.setVisibility(View.GONE);
            view.setTranslationX(-pageWidth);
        }
    }

    /**
     * Get the alpha value that should be applied to a position.
     *
     * @param position Position to find an alpha for.
     * @return An alpha value.
     */
    private static final float getAlpha(final float position) {
        return getSlowQuadraticAlpha(position);
    }

    private static final float getLinearAlpha(final float position) {
        if (position <= 0) {
            return 1 + position;
        }
        return 1 - position;
    }

    private static final float getFastQuadraticAlpha(final float position) {
        final float linearAlpha = getLinearAlpha(position);
        return linearAlpha * linearAlpha;
    }

    private static final float getSlowQuadraticAlpha(final float position) {
        return 1 - position * position;
    }

}

@geftimov
Copy link
Owner

@prsolucoes I will test it and upload it today.

@geftimov geftimov self-assigned this May 25, 2016
@khalid-mahmood
Copy link

@prsolucoes doesn't work correctly while scroll forward. backwards works correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants