kotlin 如何在水平寻呼机Android Compose中实现淡入淡出动画

q3qa4bjr  于 2023-02-13  发布在  Kotlin
关注(0)|答案(1)|浏览(151)

我正在设计一个水平寻呼机在Android Jetpack组成使用谷歌伴奏寻呼机(实现“com。谷歌伴奏:伴奏寻呼机:0.24.2-阿尔法”)。我实现这个水平寻呼机正确。但我想改变默认的动画寻呼机从水平左/右滑动淡入/淡出动画。我是新的组成,我没有找到任何有用的资源在互联网上。如果有人知道这一点,帮助我找到正确的解决方案。提前感谢。

val pagerState = rememberPagerState()
    HorizontalPager(count = 3, state = pagerState
    ) { pagePosition ->
            PagerScreen(onBoardingData = onBoardingDataList[pagePosition])
    }

这里PagerScreen是每个屏幕的可组合函数。
Like this Gif I want fadein / fadeout for horizontal Pager

izj3ouym

izj3ouym1#

我不确定使用HorizontalPager是否有更好的解决方案,所以我使用swipeable修饰符来实现这个解决方案。

@ExperimentalMaterialApi
@Composable
fun SwipeableSampleScreen5() {
    val pages = listOf(
        596 to Color(203, 41, 61),
        801 to Color(107, 0, 141),
        492 to Color(40, 38, 149),
        718 to Color(56, 144, 244),
        550 to Color(60, 149, 159)
    )
    val swipeableState = rememberSwipeableState(0)

    // This Box is only to get the screen's width
    BoxWithConstraints(Modifier.fillMaxWidth()) {
        val widthPx = with(LocalDensity.current) {
            maxWidth.toPx()
        }
        // Each anchor of the swipeable is the index * screen width
        val anchors = remember(pages) {
            List(pages.size) { index ->
                -(index * widthPx) to index
            }.toMap()
        }
        // This Box is capturing the swipe gesture
        Box(
            modifier = Modifier
                .fillMaxSize()
                .swipeable(
                    state = swipeableState,
                    anchors = anchors,
                    thresholds = { _, _ -> FractionalThreshold(0.5f) },
                    orientation = Orientation.Horizontal,
                )
        ) {
            // swipeableState provides the current index and the next index
            val currentIndex = swipeableState.currentValue
            val nextIndex = swipeableState.progress.to

            // This Box is behind and will display the nextValue
            Box(
                Modifier
                    // graphicsLayer is applied to the content
                    .graphicsLayer { 
                        alpha = 1f
                    },
            ) {
                Box(
                    Modifier
                        .fillMaxSize()
                        .background(pages[nextIndex].second),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = pages[nextIndex].first.toString(),
                        style = MaterialTheme.typography.h1.copy(color = Color.White),
                    )
                }
            }

            // This Box is showing the current value which will change 
            // the alpha during the swiping
            Box(
                Modifier
                    .graphicsLayer {
                        alpha = 1f - swipeableState.progress.fraction
                    },
            ) {
                Box(
                    Modifier
                        .fillMaxSize()
                        .background(pages[currentIndex].second),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = pages[currentIndex].first.toString(),
                        style = MaterialTheme.typography.h1.copy(color = Color.White),
                    )
                }
            }
        }
    }
}

解决方案是评论,但为了更好地理解this article帮助了我很大。
结果如下:

您可以找到完整的源代码here

相关问题