/* * Copyright (c) 2019 Hemanth Savarala. * * Licensed under the GNU General Public License v3 * * This 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 software 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. */ package code.name.monkey.retromusic.transform import android.view.View import androidx.viewpager.widget.ViewPager import kotlin.math.abs import kotlin.math.max /** * @author Hemanth S (h4h13). */ class NormalPageTransformer : ViewPager.PageTransformer { override fun transformPage(view: View, position: Float) { val pageWidth = view.width val pageHeight = view.height when { position < -1 -> { // [-Infinity,-1) // This page is way off-screen to the left. view.alpha = 1f view.scaleY = 0.7f } position <= 1 -> { // [-1,1] // Modify the default slide transition to shrink the page as well val scaleFactor = max(MIN_SCALE, 1 - abs(position)) val vertMargin = pageHeight * (1 - scaleFactor) / 2 val horzMargin = pageWidth * (1 - scaleFactor) / 2 if (position < 0) { view.translationX = horzMargin - vertMargin / 2 } else { view.translationX = -horzMargin + vertMargin / 2 } // Scale the page down (between MIN_SCALE and 1) view.scaleX = scaleFactor view.scaleY = scaleFactor // Fade the page relative to its size. //view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA)); } else -> { // (1,+Infinity] // This page is way off-screen to the right. view.alpha = 1f view.scaleY = 0.7f } } } companion object { private const val MIN_SCALE = 0.85f private const val MIN_ALPHA = 0.5f } }