【问题标题】:How to Implement Fade In / Fade Out Animation in Horizontal Pager Android Compose如何在 Horizo​​ntal Pager Android Compose 中实现淡入/淡出动画
【发布时间】:2023-02-11 14:19:58
【问题描述】:

我正在使用 googles accompanist Pager 在 Android Jetpack Compose 中设计一个 Horizo​​ntal Pager (实施“com.google.accompanist:accompanist-pager:0.24.2-alpha”)。 我正确地实现了这个水平寻呼机。但我想将寻呼机的默认动画从水平向左/向右滑动更改为淡入/淡出动画。我是新手,我没有在互联网上找到任何有用的资源。如果有人知道这可以帮助我找到正确的解决方案。提前致谢。

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

这里的 PagerScreen 是每个屏幕的可组合函数。

Like this Gif I want fadein / fadeout for horizontal Pager

【问题讨论】:

  • 您是否有预期行为的 GIF 或视频?也许这个链接可以帮助你:google.github.io/accompanist/pager/#item-scroll-effects
  • @nglauber。我现在在我的帖子里添加了一个gif i.stack.imgur.com/CoeHx.gif
  • @nglauber 我添加了 gif
  • 似乎您不需要为此使用 View Pager。您只需要在滑动手势后在两个可组合项之间切换...如果我有空闲时间,我会尝试发布一些内容。
  • @nglauber 如果你有时间帮助我!!!

标签: android android-studio kotlin android-viewpager android-jetpack-compose


【解决方案1】:

我不确定使用 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。

【讨论】:

    【解决方案2】:

    我在 Medium 上写了this article。我希望它能帮助你。问候!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-24
      • 2011-10-11
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2014-01-20
      相关资源
      最近更新 更多