Skip to content

Commit d58b422

Browse files
committed
outerItem Scale and Alpha added.
1 parent 74adbd6 commit d58b422

File tree

2 files changed

+64
-35
lines changed

2 files changed

+64
-35
lines changed

expandable-horizontal-pager/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dependencies {
4444
}
4545

4646
implementation "androidx.compose.ui:ui:$compose_ui_version"
47+
implementation "androidx.compose.ui:ui-util:$compose_ui_version"
4748
implementation "androidx.compose.material3:material3:1.0.0-alpha15"
4849
implementation "com.google.accompanist:accompanist-pager:$accompanist_version"
4950
}

expandable-horizontal-pager/src/main/java/com/commandiron/expandable_horizontal_pager/ExpandableHorizontalPager.kt

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ import androidx.compose.ui.Modifier
2121
import androidx.compose.ui.draw.drawWithContent
2222
import androidx.compose.ui.graphics.Brush
2323
import androidx.compose.ui.graphics.Color
24+
import androidx.compose.ui.graphics.graphicsLayer
2425
import androidx.compose.ui.unit.Dp
2526
import androidx.compose.ui.unit.dp
27+
import androidx.compose.ui.util.lerp
2628
import com.google.accompanist.pager.ExperimentalPagerApi
2729
import com.google.accompanist.pager.HorizontalPager
30+
import com.google.accompanist.pager.calculateCurrentOffsetForPage
31+
import kotlin.math.absoluteValue
2832

2933
@OptIn(ExperimentalPagerApi::class)
3034
@Composable
@@ -35,6 +39,10 @@ fun ExpandableHorizontalPager(
3539
itemSpacing: Dp = 0.dp,
3640
initialHorizontalPadding: Dp = 64.dp,
3741
targetHorizontalPadding: Dp = 0.dp,
42+
outerItemScaleEnabled: Boolean = true,
43+
outerItemScale: Float = 0.85f,
44+
outerItemAlphaEnabled: Boolean = true,
45+
outerItemAlpha: Float = 0.5f,
3846
key: ((page: Int) -> Any)? = null,
3947
userScrollEnabled: Boolean = true,
4048
initialWidth: Dp = 200.dp,
@@ -146,7 +154,6 @@ fun ExpandableHorizontalPager(
146154
expanded = !expanded
147155
}
148156

149-
var expandedPage by remember { mutableStateOf(0) }
150157
HorizontalPager(
151158
count = count,
152159
modifier = modifier,
@@ -163,24 +170,34 @@ fun ExpandableHorizontalPager(
163170
) {
164171
Card(
165172
modifier = Modifier
166-
.width(
167-
if(expandedPage == page) {
168-
contentWidth
169-
} else {
170-
initialWidth
171-
}
172-
)
173-
.height(
174-
if(expandedPage == page) {
175-
boxHeight
176-
} else {
177-
0.dp
178-
}
179-
)
173+
.width(contentWidth)
174+
.height(if(expanded) boxHeight else 0.dp)
180175
.offset(
181176
x = 0.dp,
182177
y = boxOffSetY
183-
),
178+
)
179+
.graphicsLayer {
180+
if(!expanded) {
181+
val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue
182+
if(outerItemScaleEnabled) {
183+
lerp(
184+
start = outerItemScale,
185+
stop = 1f,
186+
fraction = 1f - pageOffset.coerceIn(0f, 1f)
187+
).also { scale ->
188+
scaleX = scale
189+
scaleY = scale
190+
}
191+
}
192+
if(outerItemAlphaEnabled) {
193+
alpha = lerp(
194+
start = outerItemAlpha,
195+
stop = 1f,
196+
fraction = 1f - pageOffset.coerceIn(0f, 1f)
197+
)
198+
}
199+
}
200+
},
184201
shape = RoundedCornerShape(cornerSize),
185202
colors = CardDefaults.cardColors(
186203
containerColor = Color.Black.copy(0.95f),
@@ -210,35 +227,46 @@ fun ExpandableHorizontalPager(
210227
}
211228
Card(
212229
modifier = Modifier
213-
.width(
214-
if(expandedPage == page) {
215-
contentWidth
216-
} else {
217-
initialWidth
218-
}
219-
)
220-
.height(
221-
if(expandedPage == page) {
222-
contentWidth * 1 / aspectRatio
223-
} else {
224-
initialWidth * 1 / aspectRatio
225-
}
226-
)
230+
.width(contentWidth)
231+
.height(contentWidth * 1 / aspectRatio)
227232
.aspectRatio(aspectRatio)
228233
.offset(
229-
x = if(expandedPage == page) contentOffSetX else 0.dp,
230-
y = if(expandedPage == page) contentOffSetY else 0.dp
234+
x = contentOffSetX,
235+
y = contentOffSetY
231236
)
237+
.graphicsLayer {
238+
if(!expanded) {
239+
val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue
240+
if(outerItemScaleEnabled) {
241+
lerp(
242+
start = outerItemScale,
243+
stop = 1f,
244+
fraction = 1f - pageOffset.coerceIn(0f, 1f)
245+
).also { scale ->
246+
scaleX = scale
247+
scaleY = scale
248+
}
249+
}
250+
if(outerItemAlphaEnabled) {
251+
alpha = lerp(
252+
start = outerItemAlpha,
253+
stop = 1f,
254+
fraction = 1f - pageOffset.coerceIn(0f, 1f)
255+
)
256+
}
257+
}
258+
}
232259
.draggable(
260+
enabled = currentPage == page,
233261
orientation = Orientation.Vertical,
234262
state = rememberDraggableState {},
235263
onDragStarted = {
236-
expandedPage = page
237264
expand(maxHeight)
238265
}
239266
)
240-
.clickable {
241-
expandedPage = page
267+
.clickable(
268+
enabled = currentPage == page,
269+
) {
242270
expand(maxHeight)
243271
},
244272
shape = RoundedCornerShape(cornerSize)

0 commit comments

Comments
 (0)