Coo*_*ter 4 viewpagerindicator android-jetpack-compose
如何在 Jetpack Compose 中为 HorizontalPager 创建这样的点指示器?
我找到了一些库和示例,但没有一个是这样的动画。
Abh*_*bhi 11
一个复制粘贴解决方案,具有我能想到的尽可能多的自定义功能。
结果
用法
@Composable
fun PageIndicatorSample() {
val numberOfPages = 3
val (selectedPage, setSelectedPage) = remember {
mutableStateOf(0)
}
// NEVER use this, this is just for example
LaunchedEffect(
key1 = selectedPage,
) {
delay(3000)
setSelectedPage((selectedPage + 1) % numberOfPages)
}
PageIndicator(
numberOfPages = numberOfPages,
selectedPage = selectedPage,
defaultRadius = 60.dp,
selectedLength = 120.dp,
space = 30.dp,
animationDurationInMillis = 1000,
)
}
Run Code Online (Sandbox Code Playgroud)
页面指示器
@Composable
fun PageIndicator(
numberOfPages: Int,
modifier: Modifier = Modifier,
selectedPage: Int = 0,
selectedColor: Color = Color.Blue,
defaultColor: Color = Color.LightGray,
defaultRadius: Dp = 20.dp,
selectedLength: Dp = 60.dp,
space: Dp = 30.dp,
animationDurationInMillis: Int = 300,
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(space),
modifier = modifier,
) {
for (i in 0 until numberOfPages) {
val isSelected = i == selectedPage
PageIndicatorView(
isSelected = isSelected,
selectedColor = selectedColor,
defaultColor = defaultColor,
defaultRadius = defaultRadius,
selectedLength = selectedLength,
animationDurationInMillis = animationDurationInMillis,
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
页面指示器视图
@Composable
fun PageIndicatorView(
isSelected: Boolean,
selectedColor: Color,
defaultColor: Color,
defaultRadius: Dp,
selectedLength: Dp,
animationDurationInMillis: Int,
modifier: Modifier = Modifier,
) {
val color: Color by animateColorAsState(
targetValue = if (isSelected) {
selectedColor
} else {
defaultColor
},
animationSpec = tween(
durationMillis = animationDurationInMillis,
)
)
val width: Dp by animateDpAsState(
targetValue = if (isSelected) {
selectedLength
} else {
defaultRadius
},
animationSpec = tween(
durationMillis = animationDurationInMillis,
)
)
Canvas(
modifier = modifier
.size(
width = width,
height = defaultRadius,
),
) {
drawRoundRect(
color = color,
topLeft = Offset.Zero,
size = Size(
width = width.toPx(),
height = defaultRadius.toPx(),
),
cornerRadius = CornerRadius(
x = defaultRadius.toPx(),
y = defaultRadius.toPx(),
),
)
}
}
Run Code Online (Sandbox Code Playgroud)
在此博客中也分享了相同的内容 -使用 Canvas 和动画的 Jetpack Compose 的页面指示器
| 归档时间: |
|
| 查看次数: |
2619 次 |
| 最近记录: |