[英]Android ViewPager2, previous and next item darken processing
As shown in the picture above, I want to darken the previous & next items in the ViewPager2
.如上图所示,我想使
ViewPager2
中的上一个和下一个项目变暗。
setPageTransformer(object : ViewPager2.PageTransformer {
override fun transformPage(page: View, position: Float) {
page.translationX = -pageTranslationX * position
page.scaleY = 1 - (0.15f * abs(position))
}
})
I'm showing the preview with the above snippet, but I couldn't find a way to darken it.我正在使用上面的代码片段显示预览,但我找不到使它变暗的方法。
Is there a good solution?有好的解决办法吗?
You can utilize the position
parameter, that maps to the current position of the page, to get a color shade based on the page position;您可以利用映射到页面当前 position 的
position
参数来获取基于页面 position 的色度; this also will benefit in grading the colors between the most darken & most lighten colors so that the color changes gradually:这也将有助于在最暗和最亮的 colors 之间对 colors 进行分级,以便颜色逐渐变化:
ColorUtils.blendARGB()
to get a color based on percentage of the position.ColorUtils.blendARGB()
获取基于 position 百分比的颜色。page
page
的根视图setPageTransformer(object : ViewPager2.PageTransformer {
override fun transformPage(page: View, position: Float) {
page.translationX = -pageTranslationX * position
page.scaleY = 1 - (0.15f * abs(position))
val startColor = ContextCompat.getColor(this, R.color.light_blue)
val endColor = ContextCompat.getColor(this, R.color.dark_blue)
val color = ColorUtils.blendARGB(startColor, endColor, abs(position))
val view = page.findViewById<LinearLayout>(R.id.page_root) // this is the page fragment root view (make sure to set its id)
view?.setBackgroundColor(color)
}
})
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.