com.waz.zclient.pages.main.conversationpager.CustomPagerTransformer.java Source code

Java tutorial

Introduction

Here is the source code for com.waz.zclient.pages.main.conversationpager.CustomPagerTransformer.java

Source

/**
 * Wire
 * Copyright (C) 2016 Wire Swiss GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.waz.zclient.pages.main.conversationpager;

import android.support.annotation.IntDef;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.waz.zclient.utils.ViewUtils;

public class CustomPagerTransformer implements ViewPager.PageTransformer {

    @IntDef({ FLOW, DEPTH, ZOOM, SLIDE_OVER, SLIDE_IN

    })
    public @interface TransformType {
    }

    public static final int FLOW = 0;
    public static final int DEPTH = 1;
    public static final int ZOOM = 2;
    public static final int SLIDE_OVER = 3;
    public static final int SLIDE_IN = 4;

    @TransformType
    private final int transformType;

    public CustomPagerTransformer(@TransformType int transformType) {
        this.transformType = transformType;
    }

    private static final float MIN_SCALE_DEPTH = 0.75f;
    private static final float MIN_SCALE_ZOOM = 0.85f;
    private static final float MIN_ALPHA_ZOOM = 0.5f;
    private static final float SCALE_FACTOR_SLIDE = 0.85f;
    private static final float MIN_ALPHA_SLIDE = 0.35f;

    public void transformPage(View page, float position) {
        final float alpha;
        final float scale;
        final float translationX;

        switch (transformType) {
        case FLOW:
            page.setRotationY(position * -30f);
            return;

        case SLIDE_OVER:
            if (position < 0 && position > -1) {
                // this is the page to the left
                scale = Math.abs(Math.abs(position) - 1) * (1.0f - SCALE_FACTOR_SLIDE) + SCALE_FACTOR_SLIDE;
                alpha = Math.max(MIN_ALPHA_SLIDE, 1 - Math.abs(position));
                int pageWidth = page.getWidth();
                float translateValue = position * (-pageWidth);
                if (translateValue > -pageWidth) {
                    translationX = translateValue;
                } else {
                    translationX = 0;
                }
            } else {
                alpha = 1;
                scale = 1;
                translationX = 0;
            }
            break;

        case SLIDE_IN:
            if (position < 0 && position > -1) {
                // this is the page to the left
                scale = 1;
                alpha = 1;
                int pageWidth = page.getWidth();
                float translateValue = position * -pageWidth + position * (-ViewUtils.toPx(page.getContext(), 64));
                if (translateValue > -pageWidth) {
                    translationX = translateValue;
                } else {
                    translationX = 0;
                }

            } else {
                alpha = 1;
                scale = 1;
                translationX = 0;
            }
            break;

        case DEPTH:
            if (position > 0 && position < 1) {
                // moving to the right
                alpha = (1 - position);
                scale = MIN_SCALE_DEPTH + (1 - MIN_SCALE_DEPTH) * (1 - Math.abs(position));
                translationX = (page.getWidth() * -position);
            } else {
                // use default for all other cases
                alpha = 1;
                scale = 1;
                translationX = 0;
            }
            break;

        case ZOOM:
            if (position >= -1 && position <= 1) {
                scale = Math.max(MIN_SCALE_ZOOM, 1 - Math.abs(position));
                alpha = MIN_ALPHA_ZOOM + (scale - MIN_SCALE_ZOOM) / (1 - MIN_SCALE_ZOOM) * (1 - MIN_ALPHA_ZOOM);
                float vMargin = page.getHeight() * (1 - scale) / 2;
                float hMargin = page.getWidth() * (1 - scale) / 2;
                if (position < 0) {
                    translationX = (hMargin - vMargin / 2);
                } else {
                    translationX = (-hMargin + vMargin / 2);
                }
            } else {
                alpha = 1;
                scale = 1;
                translationX = 0;
            }
            break;

        default:
            return;
        }

        page.setAlpha(alpha);
        page.setTranslationX(translationX);
        page.setScaleX(scale);
        page.setScaleY(scale);
    }
}