2

I am trying to achieve translate animation in Jetpack compose but i am not able to find Suitable Source for this.Can any one Help me to achieve translate Animation in jetpack compose in which i can set start and edning positionl Manually..

4 Answers4

7

The alternative of translate animation in jetpack compose is OFFSET ANIMATION yes, I was able to achieve this through offset animation.I am sharing the code below with comments in detail so it will be easier for the reader to understand it

// Courtine Scope to Run the animation in thread
val coroutineScope = rememberCoroutineScope()
val offsetX = remember { Animatable(0f) }
val offsetY = remember { Animatable(0f) }

 Image(
            painter = rememberDrawablePainter(
                ContextCompat.getDrawable(
                    context,
                    R.drawable.image
                )
            ),
            contentDescription = "s", contentScale = ContentScale.Crop,
            modifier = Modifier
                .offset {
                    IntOffset(
                        offsetX.value.toInt(),
                        offsetY.value.toInt()
                    )
                }
                .width(300.dp)
                .height(300.dp)
        )
//Finally run the animation on the Click of your button or whenever you wants to start it...

  coroutineScope.launch {

       launch {
                    offsetXFirst.animateTo(
                    targetValue = targetValue,
                    animationSpec = tween(
                    durationMillis = 2000,
                    delayMillis = 0))}

                launch {
                offsetYFirst.animateTo(
                targetValue = size.height.toFloat(),
                animationSpec = tween(
                durationMillis = 2000,
                delayMillis = 0))}
   }
xaif
  • 553
  • 6
  • 27
1

Using the offset was my solution as well. However used animateDpAsState instead. There a tab indicator is moved on the x axis:

val offsetState = animateDpAsState(targetValue = targetPositionDp)
Box(modifier = Modifier
        .offset(offsetState.value, 0.dp)
        .background(color = Color.Red)
        .size(tabWidth, tabHeight))
Oliver Metz
  • 2,712
  • 2
  • 20
  • 32
1

If your goal is to show/hide some composable with translate animation, the best way is to use AnimatedVisibility:

AnimatedVisibility(
        visible = isVisible,
        enter = slideInVertically { it },
        exit = slideOutVertically { it }
) {
   SomeComposable()
}

Also, there is a good scheme of how to select suitable animation in Jetpack Compose: https://developer.android.com/jetpack/compose/animation

anro
  • 1,300
  • 16
  • 30
1

Just chipping in to the discussion. As others have said, using the offset modifier is the quickest way to apply some translation to an element.

However, the offset modifier only affects the content being displayed and not the actual size measurement of the element. If you are offsetting an element A based on which another element B is positioned, the position of element B will remain unchanged regardless of the .offset applied to element A. This usually means you will be left with some empty space. To address this issue, one could apply actual translation using the .graphicsLayer { translationY = translationYPx } modifier.

To sum up, use the .offset modifier whenever possible as it is much more efficient and only resort to .graphicsLayer when you need the size of the element to also change as it goes offscreen for example.

V Mircan
  • 203
  • 5
  • 13