android-jetpack-compose - 如何使用 HorizantalPager 为图像设置动画?
问题描述
我在 Box 中有三个背景,该框包含 HorizontalPager 和一个根据 HorizontalPager 当前页面更改的图像组件,但没有动画如何添加这样的动画
https://cdn.dribbble.com/users/1588664/screenshots/8257559 /media/d94b36cb7822008df8055c90ca6e2238.mp4
这是我的代码预览 https://drive.google.com/file/d/1i2ix1Xdx-TK2SVPgx8MhFZWfqQnhZcr5/view?usp=sharing
val pagerState = rememberPagerState(pageCount = 3)
val currentIndex = pagerState.currentPage
val currentPageOffset = pagerState.currentPageOffset
val poster = listOf(
R.drawable.red,R.drawable.blue,R.drawable.green
)
Box(modifier = Modifier.fillMaxSize()){
Image(
painter = painterResource(poster[currentIndex]),
contentDescription = null,
contentScale = ContentScale.FillWidth,
modifier = Modifier.fillMaxWidth()
)
HorizontalPager(
state = pagerState,
modifier = Modifier.fillMaxSize()
) { page ->
val maxOffset = 70.dp
val offset = maxOffset * when (page) {
currentIndex -> {
currentPageOffset.absoluteValue
}
currentIndex - 1 -> {
1 + currentPageOffset.coerceAtMost(0f)
}
currentIndex + 1 -> {
1 - currentPageOffset.coerceAtLeast(0f)
}
else -> {
1f
}
}
Card(
modifier = Modifier
.fillMaxWidth(0.7f)
.fillMaxHeight(0.7f)
.padding(22.dp)
.offset(y = offset),
shape = RoundedCornerShape(30.dp),
elevation = 110.dp
) {}
}
}
解决方案
推荐阅读
- sas - SAS:在 FIFO 基础上减去两个数字的逻辑
- php - 删除 Magento2 上某些类别中的“添加到购物车”按钮
- android - 如何在应用程序强制关闭android上添加代码
- python - 使用 Python 从进程的内存中读取数据
- angular - 如何将数据 + 多个文件从 Angular 上传到 .net core Web Api
- css - 关键帧的混合
- ruby - Ruby On Rails - 新的迁移和模型更改导致迁移失败
- javascript - 等待由不同组件触发的 2 个 API 完成,然后再触发另一个 API
- r - 列在 num 时错误地标记为 int
- c# - CountdownEvent 与 Barrier 的多线程用法?