class InvalidationStrategyScope


Helper scope that provides some strategies to improve performance based on incoming constraints.

As a starting approach, we recommend trying the following:

MotionLayout(
...,
invalidationStrategy = remember {
InvalidationStrategy(
onIncomingConstraints = { old, new ->
// We invalidate every third frame, or when the change is higher than 5 pixels
fixedWidthRate(old, new, skipCount = 3, threshold = 5) ||
fixedHeightRate(old, new, skipCount = 3, threshold = 5)
},
onObservedStateChange = null // Default behavior
)
}
) {
// content
}

See either fixedWidthRate or fixedHeightRate to learn more about the intent behind rate-limiting invalidation.

Summary

Public functions

Boolean
fixedHeightRate(
    oldConstraints: Constraints,
    newConstraints: Constraints,
    skipCount: Int,
    threshold: Int
)

Limits the rate at which MotionLayout is invalidated while Constraints.hasFixedHeight is true.

Boolean
fixedWidthRate(
    oldConstraints: Constraints,
    newConstraints: Constraints,
    skipCount: Int,
    threshold: Int
)

Limits the rate at which MotionLayout is invalidated while Constraints.hasFixedWidth is true.

Public functions

fixedHeightRate

fun fixedHeightRate(
    oldConstraints: Constraints,
    newConstraints: Constraints,
    skipCount: Int,
    threshold: Int
): Boolean

Limits the rate at which MotionLayout is invalidated while Constraints.hasFixedHeight is true.

 

The rate limit is defined by two variables. Use skipCount to indicate how many consecutive measure passes should skip invalidation, you may then provide a threshold (in pixels) to indicate when to invalidate regardless of how many passes are left to skip. This is important since you only want to skip invalidation passes when there's not a significant change in dimensions.

 

Overall, you don't want skipCount to be too high otherwise it'll result in a "jumpy" layout behavior, but you also don't want the threshold to be too low, otherwise you'll lose the benefit of rate limiting.

A good starting point is setting skipCount to 3 and threshold to 5. You can then adjust based on your expectations of performance and perceived smoothness.

fixedWidthRate

fun fixedWidthRate(
    oldConstraints: Constraints,
    newConstraints: Constraints,
    skipCount: Int,
    threshold: Int
): Boolean

Limits the rate at which MotionLayout is invalidated while Constraints.hasFixedWidth is true.

 

The rate limit is defined by two variables. Use skipCount to indicate how many consecutive measure passes should skip invalidation, you may then provide a threshold (in pixels) to indicate when to invalidate regardless of how many passes are left to skip. This is important since you only want to skip invalidation passes when there's not a significant change in dimensions.

 

Overall, you don't want skipCount to be too high otherwise it'll result in a "jumpy" layout behavior, but you also don't want the threshold to be too low, otherwise you'll lose the benefit of rate limiting.

A good starting point is setting skipCount to 3 and threshold to 5. You can then adjust based on your expectations of performance and perceived smoothness.