Last active
May 31, 2018 01:21
-
-
Save Alec-DeSouza/0b4577ccfc2571147703392149030b6f to your computer and use it in GitHub Desktop.
ViewPager and TabLayout Dynamic Tab Opacity Interpolation [Kotlin]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
view_pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() { | |
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { | |
// Interpolate all tab opacity values | |
for (index in 0 until tab_layout.tabCount) { | |
tab_layout.getTabAt(index)?.updateOpacity(position, positionOffset) | |
} | |
} | |
fun TabLayout.Tab.updateOpacity(position: Int, offset: Float) { | |
val diffTab = 1 - abs(position - this.position + offset) | |
// Update opacity of containing elements | |
customView?.alpha = 0.5f + if (diffTab in 0.0..1.0) (diffTab * 0.5).toFloat() else 0f | |
icon?.alpha = 127 + if (diffTab in 0.0..1.0) (diffTab * 128).toInt() else 0 | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment