Home > Net >  Create Animation in jetpack compose corresponds to below code in java?
Create Animation in jetpack compose corresponds to below code in java?

Time:10-04

Below code is in java. We need to do same thing using jetpack compose. please help us to achieve this.

val flashAnimatorSet = AnimatorSet()
val layer = ObjectAnimator.ofInt(
    image,
    alpha,
    255,
    77
)
layer.repeatMode = ValueAnimator.REVERSE
layer.repeatCount = ObjectAnimator.INFINITE
flashAnimatorSet.play(lightLayer)
flashAnimatorSet.duration = 100L
flashAnimatorSet.interpolator = AccelerateDecelerateInterpolator()
return flashAnimatorSet

CodePudding user response:

You can use a InfiniteRepeatableSpec to repeat the provided animation infinite amount of times.

Something like:

val infiniteTransition = rememberInfiniteTransition()
val alpha by infiniteTransition.animateFloat(
    initialValue = 1F,
    targetValue = 0.28F,
    animationSpec = infiniteRepeatable(
        animation = tween(
            durationMillis = 1000,
            easing = FastOutSlowInEasing
        ),
        repeatMode = RepeatMode.Reverse
    )
)

Then apply it to your Composable, for example:

Image(
    painterResource(id = R.drawable.xxx),
    modifier = Modifier
        .graphicsLayer {
           this.alpha = alpha
        },
    contentDescription = "contentDescription"
)
  • Related