我需要更改 Jetpack Compose for TV 的默认滚动动画。 对于我们的一些老客户来说,默认设置太快了。
这是如何实现的?
这是改变滚动动画的方法。
创建一个 BringIntoViewSpec 本地提供程序。
这是复制旧的 Pivot 行为的行为
fun PositionFocusedItemInLazyLayout(
parentFraction: Float = 0.3f,
childFraction: Float = 0f,
content: @Composable () -> Unit,) {
// This bring-into-view spec pivots around the center of the scrollable container
val bringIntoViewSpec = object : BringIntoViewSpec {
private val cardAnimationSpec : AnimationSpec<Float> = tween( durationMillis = 50, easing = FastOutLinearInEasing )
override val scrollAnimationSpec: AnimationSpec<Float>
get() = cardAnimationSpec
override fun calculateScrollDistance(
// Initial position of item requesting focus
offset: Float,
// Size of item requesting focus
size: Float,
// Size of the lazy container
containerSize: Float
): Float {
val childSmallerThanParent = size <= containerSize
val initialTargetForLeadingEdge =
parentFraction * containerSize - (childFraction * size)
val spaceAvailableToShowItem = containerSize - initialTargetForLeadingEdge
val targetForLeadingEdge =
if (childSmallerThanParent && spaceAvailableToShowItem < size) {
containerSize - size
} else {
initialTargetForLeadingEdge
}
return offset - targetForLeadingEdge
}
}
// LocalBringIntoViewSpec will apply to all scrollable(s) in the hierarchy
CompositionLocalProvider(
LocalBringIntoViewSpec provides bringIntoViewSpec,
content = content,
)
}
重写scrollAnimationSpec Getter并提供您自己的,焦点系统使用它来设置滚动动画。
这可以通过放置在 LazyList/Row 上方来使用。
PositionFocusedItemInLazyLayout(
parentFraction = ParentRowFraction,
childFraction = ChildRowFraction
) {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(InternalPadding),
horizontalAlignment = Alignment.CenterHorizontally,
contentPadding = PaddingValues(top = VerticalPadding, bottom = VerticalPadding),
state = columnState,
) { ....