tan.chesley.rssfeedreader.DepthPageTransformer.java Source code

Java tutorial

Introduction

Here is the source code for tan.chesley.rssfeedreader.DepthPageTransformer.java

Source

package tan.chesley.rssfeedreader;

import android.support.v4.view.ViewPager;
import android.view.View;

/*
Copyright 2014 Google Inc
Used with modification.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software distributed under the License is
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions and limitations under the License.
*/
public class DepthPageTransformer implements ViewPager.PageTransformer {

    private static final float MIN_SCALE = 0.75f;

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

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);
        } else if (position <= 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
            view.setAlpha(1 + position);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);

            // Prevent old views from stealing touches
            if (position >= -0.01) {
                view.bringToFront();
                view.getParent().requestLayout();
                ((View) view.getParent()).invalidate();
            }
        } else if (position <= 1) { // (0,1]
            // Fade the page out.
            view.setAlpha(1 - position);

            // Counteract the default slide transition
            view.setTranslationX(pageWidth * -position);

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]
                 // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}