Skip to content

Instantly share code, notes, and snippets.

@voghDev
Created March 14, 2018 13:20
Show Gist options
  • Save voghDev/03ecad885f89451d6c1b526609228822 to your computer and use it in GitHub Desktop.
Save voghDev/03ecad885f89451d6c1b526609228822 to your computer and use it in GitHub Desktop.
Kotlin port of the original ZoomOutPageTransformer 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 ZoomOutPageTransformer class, found here:
* https://developer.android.com/training/animation/screen-slide.html
* I am not the owner of the original code
*/
class ZoomOutPageTransformer : ViewPager.PageTransformer {
val MIN_SCALE = 0.85f
val MIN_ALPHA = 0.5f
override fun transformPage(view: View?, position: Float) {
val pageWidth = view?.width ?: 0
val pageHeight = view?.height ?: 0
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view?.alpha = 0f
} else if (position <= 1) { // [-1, 1]
val scaleFactor = Math.max(MIN_SCALE, 1 - Math.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?.alpha = 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 = 0f
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment