롤리팝 출시 이후로 RecyclerView가 도입되고 AdapterView를 커스터마이징 하기가 편해졌다.
오늘은 UIPickerView 스타일로 RecyclerView의 투명도를 주고 싶어서 레이아웃매니저를 다음과 같이 적용했다.
class CustomLinearLayoutManager(context: Context) : LinearLayoutManager(context, VERTICAL, false) {
private fun updateChildrenAlpha() {
for (i in 0 until childCount) {
val child: View = getChildAt(i)
val bottom: Float = getDecoratedBottom(child).toFloat()
val top: Float = getDecoratedTop(child).toFloat()
val childCenter: Float = bottom + (top - bottom) / 2f
val center: Float = height / 2f
child.alpha = (center - Math.abs(center - childCenter)) / center
} }
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int {
updateChildrenAlpha()
return super.scrollVerticallyBy(dy, recycler, state)
}
override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
super.onLayoutChildren(recycler, state)
updateChildrenAlpha()
}
}
가운데를 중심으로 위아래가 자연스럽게 fade-in 과 fade-out 되는모습을 볼수 있다.
0개의 댓글