@@ -68,24 +68,19 @@ fun WheelPicker(
6868 flingBehavior = flingBehavior
6969 ) {
7070 items(count) { index ->
71- val rotationX = calculateAnimatedRotationX (
71+ val (newAlpha, newRotationX) = calculateAnimatedAlphaAndRotationX (
7272 lazyListState = lazyListState,
7373 index = index,
7474 rowCount = rowCount
7575 )
76+
7677 Box (
7778 modifier = Modifier
7879 .height(size.height / rowCount)
7980 .width(size.width)
80- .alpha(
81- calculateAnimatedAlpha(
82- lazyListState = lazyListState,
83- index = index,
84- rowCount = rowCount
85- )
86- )
81+ .alpha(newAlpha)
8782 .graphicsLayer {
88- this . rotationX = rotationX
83+ rotationX = newRotationX
8984 },
9085 contentAlignment = Alignment .Center
9186 ) {
@@ -106,11 +101,11 @@ private fun calculateSnappedItemIndex(lazyListState: LazyListState): Int {
106101}
107102
108103@Composable
109- private fun calculateAnimatedAlpha (
104+ private fun calculateAnimatedAlphaAndRotationX (
110105 lazyListState : LazyListState ,
111106 index : Int ,
112107 rowCount : Int
113- ): Float {
108+ ): Pair < Float , Float > {
114109
115110 val layoutInfo = remember { derivedStateOf { lazyListState.layoutInfo } }.value
116111 val viewPortHeight = layoutInfo.viewportSize.height.toFloat()
@@ -121,48 +116,18 @@ private fun calculateAnimatedAlpha(
121116
122117 val distanceToCenterIndex = index - centerIndex
123118
124- val distanceToIndexSnap = abs(distanceToCenterIndex) * singleViewPortHeight.toInt() - when {
125- distanceToCenterIndex > 0 -> centerIndexOffset
126- distanceToCenterIndex <= 0 -> - centerIndexOffset
127- else -> 0
128- }
119+ val distanceToIndexSnap = distanceToCenterIndex * singleViewPortHeight.toInt() - centerIndexOffset
120+ val distanceToIndexSnapAbs = abs(distanceToIndexSnap)
129121
130- return if (distanceToIndexSnap in 0 .. singleViewPortHeight.toInt()) {
131- 1.2f - (distanceToIndexSnap / singleViewPortHeight)
122+ val animatedAlpha = if (abs( distanceToIndexSnap) in 0 .. singleViewPortHeight.toInt()) {
123+ 1.2f - (distanceToIndexSnapAbs / singleViewPortHeight)
132124 } else {
133125 0.2f
134126 }
135- }
136127
137- @Composable
138- private fun calculateAnimatedRotationX (
139- lazyListState : LazyListState ,
140- index : Int ,
141- rowCount : Int
142- ): Float {
143-
144- val layoutInfo = remember { derivedStateOf { lazyListState.layoutInfo } }.value
145- val viewPortHeight = layoutInfo.viewportSize.height.toFloat()
146- val singleViewPortHeight = viewPortHeight / rowCount
128+ val animatedRotationX = (- 20 * (distanceToIndexSnap / singleViewPortHeight)).takeUnless { it.isNaN() } ? : 0f
147129
148- val centerIndex = remember { derivedStateOf { lazyListState.firstVisibleItemIndex } }.value
149- val centerIndexOffset = remember { derivedStateOf { lazyListState.firstVisibleItemScrollOffset } }.value
150-
151- val distanceToCenterIndex = index - centerIndex
152-
153- val distanceToIndexSnap = abs(distanceToCenterIndex) * singleViewPortHeight.toInt() - when {
154- distanceToCenterIndex > 0 -> centerIndexOffset
155- distanceToCenterIndex <= 0 -> - centerIndexOffset
156- else -> 0
157- }
158-
159- val animatedRotationX = - 20f * (distanceToIndexSnap / singleViewPortHeight)
160-
161- return if (animatedRotationX.isNaN()) {
162- 0f
163- } else {
164- animatedRotationX
165- }
130+ return animatedAlpha to animatedRotationX
166131}
167132
168133object WheelPickerDefaults {
0 commit comments