Skip to content

Instantly share code, notes, and snippets.

@voghDev
Last active March 14, 2018 13:21
Show Gist options
  • Select an option

  • Save voghDev/8ee166e3260bc853a3b18f3d16ff6085 to your computer and use it in GitHub Desktop.

Select an option

Save voghDev/8ee166e3260bc853a3b18f3d16ff6085 to your computer and use it in GitHub Desktop.
Kotlin port of the original DepthPageTransformer class, found in Android SDK documentation
/*
* Copyright (C) 2018 Olmo Gallegos Hernández.
*
* 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.
*/
package com.appandweb.weevento.ui.adapter
import android.support.v4.view.ViewPager
import android.view.View
/**
* This is a line-to-line port to Kotlin for the DepthPageTransformer class, found here:
* https://developer.android.com/training/animation/screen-slide.html
* I am not the owner of the original code
*/
class DepthPageTransformer : ViewPager.PageTransformer {
val MIN_SCALE = 0.5f
override fun transformPage(view: View?, position: Float) {
val pageWidth = view?.width ?: 0
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view?.alpha = 0f
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when moving to the left page
view?.apply {
alpha = 1f
translationX = 0f
scaleX = 1f
scaleY = 1f
}
} else if (position <= 1) { // (0,1]
view?.apply {
// Fade the page out.
alpha = 1 - position
// Counteract the default slide transition
translationX = pageWidth * -position
// Scale the page down (between MIN_SCALE and 1)
val scaleFactor = MIN_SCALE +
(1 - MIN_SCALE) * (1 - Math.abs(position))
scaleX = scaleFactor
scaleY = scaleFactor
}
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view?.alpha = 0f
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment