GithubHelp home page GithubHelp logo

droidsonroids / pagetransformerdemo Goto Github PK

View Code? Open in Web Editor NEW
36.0 27.0 6.0 9.96 MB

Simple example of screen transitions with ViewPager and PageTransformer.

Home Page: http://www.thedroidsonroids.com/blog/android/how-to-create-fancy-screen-transitions-used-in-walkthrough-in-android-google-drive-app/

License: MIT License

Java 100.00%

pagetransformerdemo's Introduction

Cooler Screen Transitions in Android

Inspiration

Recently I found very cool introduction screen in Google Drive App, shown below:

drive

I really liked google color transition between screens. It was my inspiration to create this cool color transition by myself. I started searching about screen transition in Android Framework and I've found this: Using ViewPager for Screen Slides. So the easiest way to create screen transition effects is to use ViewPager and PageTransformer. I will focus on PageTransformer through this post.

Introduction to PageTransformer

PageTransformer is simple Interface that has single method transformPage(View page, float position). This method is triggered every time the screen position changed. It provides reference to the root View of the page and value which represents visible fraction of that page.

position value provides information about page fraction:

    if (position < -1) { // [-Infinity,-1)
        // This page is way off-screen to the left.
    } else if (position < 0) { // [-1,0)
        // Left visible page fraction
    } else if (position == 0) {
        // One page centered
    } else if (position > 0 && position <= 1) { // (0, 1]
        // Right visible page fraction
    } else { // (1, Infinity]
        // This page is way off-screen to the right.
    }

So we have everything to create this beautiful color effect !!!

Preparation

To play with transitions I created small example project which can be found here: Github.

So I've started with default transition:

default

There is no straight way to get current page index, but I found easy solution:

    @Override
    public void onViewCreated(final View view, final Bundle savedInstanceState) {
   		final int position = getArguments().getInt(ARG_POSITION);
   		final String text = MainActivity.Content.values()[position].getText();

   		view.setTag(position); // saving index

   		final TextView contentTextView = (TextView) view.findViewById(R.id.contentTextView);
   		contentTextView.setText(text);
   	}
    @Override
	public void transformPage(final View page, final float position) {
		final int pageIndex = (Integer) page.getTag(); // reading index
		transformPage(page, pageIndex, position);
	}

Because we get the same view in onViewCreated(...) and transformPage(...), I used View.setTag(Object), View.getTag() to save current ViewPager index. With this simple utility I created BasePageTransformer class which adds abstract transformPage(View page, int pageIndex, float position) method. It will help me with color transition later.

Color Transition

    public class ColorTransformer extends BasePageTransformer {

        private static int blendColors(int color1, int color2, float ratio) {
            final float inverseRation = 1f - ratio;
            float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
            float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
            float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
            return Color.rgb((int) r, (int) g, (int) b);
        }

        @SuppressWarnings("UnnecessaryLocalVariable")
        @Override
        public void transformPage(final View page, final int pageIndex, final float position) {

            if (inRange(position)) { // [-1, 1]
                if (isRightPage(position)) { //(0, 1]

                    final int leftIndex = pageIndex - 1;
                    final int rightIndex = pageIndex;

                    final int leftColor = MainActivity.Content.values()[leftIndex].getColor();
                    final int rightColor = MainActivity.Content.values()[rightIndex].getColor();

                    final int composedColor = blendColors(leftColor, rightColor, position);
                    page.setBackgroundColor(composedColor);

                } else if (isLeftPage(position)) { //[-1, 0)

                    final int leftIndex = pageIndex;
                    final int rightIndex = leftIndex + 1;

                    final int leftColor = MainActivity.Content.values()[leftIndex].getColor();
                    final int rightColor = MainActivity.Content.values()[rightIndex].getColor();

                    final int composedColor = blendColors(leftColor, rightColor, 1 - Math.abs(position));
                    page.setBackgroundColor(composedColor);

                } else { // position == 0
                    page.setBackgroundColor(MainActivity.Content.values()[pageIndex].getColor());
                }
            } else { //(-Infinity, -1) or (1, + Infinity)
                page.setBackgroundColor(MainActivity.Content.values()[pageIndex].getColor());
            }
        }
    }

I found blendColors(int color1, int color2, float ratio) in SlidingTabStrip class written by google. It basically blends two color with specific ratio - exactly what we want.

I think that the code is really straightforward, find neighbour pages indexes, get appropriate colors, blend them with proper ratio, set background color with new color.

Result:

color

Other transitions

I was playing with transformation a little longer, the results you can see below. Every transformation source code can be found on Github.

Enjoy playing with transformations !!!!

Cube Transformer

cube

Text Switch Transformer

text_switch

Rotation Transformer

rotation

Up Transformer

up

Sebastian Staszyński, Android Developer in DroidOnRoids [email protected]

pagetransformerdemo's People

Contributors

makorowy avatar michallankof avatar sebastian-staszynski-droidsonroids avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pagetransformerdemo's Issues

java.lang.ArrayIndexOutOfBoundsException

at com.solutiontab.dalilk4ielts.Walkthrough.ColorTransformer.transformPage (ColorTransformer.java:39) at com.solutiontab.dalilk4ielts.Walkthrough.BasePageTransformer.transformPage (BasePageTransformer.java:23) at android.support.v4.view.ViewPager.onPageScrolled (ViewPager.java:1935) at android.support.v4.view.ViewPager.pageScrolled (ViewPager.java:1862) at android.support.v4.view.ViewPager.performDrag (ViewPager.java:2370) at android.support.v4.view.ViewPager.onTouchEvent (ViewPager.java:2262)
This issue

How to scroll back with button click

Hallo,
I try to slide back intro with transformer but if I scroll back by click a button, previous page blank or not work normally show previous page . why should i do ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.