InvalidationStrategySpecification

class InvalidationStrategySpecification


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
shouldInvalidateOnFixedWidth(old, new, skipCount = 3, threshold = 5) ||
shouldInvalidateOnFixedHeight(old, new, skipCount = 3, threshold = 5)
},
onObservedStateChange = null // Default behavior
)
}
) {
// content
}

See either shouldInvalidateOnFixedWidth or shouldInvalidateOnFixedHeight to learn more about the intent behind rate-limiting invalidation.

Summary

Public functions

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

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

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

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

Public functions

shouldInvalidateOnFixedHeight

fun shouldInvalidateOnFixedHeight(
    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.

shouldInvalidateOnFixedWidth

fun shouldInvalidateOnFixedWidth(
    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.