Advertisement
yohannesTz

Untitled

Nov 28th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.14 KB | None | 0 0
  1.  
  2. import androidx.compose.animation.core.AnimationSpec
  3. import androidx.compose.animation.core.FiniteAnimationSpec
  4. import androidx.compose.animation.core.InfiniteRepeatableSpec
  5. import androidx.compose.animation.core.LinearEasing
  6. import androidx.compose.animation.core.RepeatMode
  7. import androidx.compose.animation.core.animateFloat
  8. import androidx.compose.animation.core.infiniteRepeatable
  9. import androidx.compose.animation.core.rememberInfiniteTransition
  10. import androidx.compose.animation.core.tween
  11. import androidx.compose.animation.core.updateTransition
  12. import androidx.compose.runtime.Composable
  13. import androidx.compose.runtime.derivedStateOf
  14. import androidx.compose.runtime.getValue
  15. import androidx.compose.runtime.mutableFloatStateOf
  16. import androidx.compose.runtime.remember
  17. import androidx.compose.runtime.saveable.rememberSaveable
  18. import androidx.compose.ui.graphics.Color
  19. import com.google.android.gms.maps.model.ButtCap
  20. import com.google.android.gms.maps.model.Cap
  21. import com.google.android.gms.maps.model.JointType
  22. import com.google.android.gms.maps.model.LatLng
  23. import com.google.android.gms.maps.model.PatternItem
  24. import com.google.android.gms.maps.model.Polyline
  25. import com.google.maps.android.compose.Polyline
  26.  
  27. @Composable
  28. fun AnimatedPolyline(
  29.     points: List<LatLng>,
  30.     durationMillis: Int = 2000,
  31.     repeatMode: RepeatMode? = null,
  32.     animationSpec: AnimationSpec<Float> = if (repeatMode != null) infiniteRepeatable(
  33.         animation = tween(durationMillis = durationMillis, easing = LinearEasing),
  34.         repeatMode = repeatMode
  35.     ) else tween(durationMillis = durationMillis, easing = LinearEasing),
  36.     animationVisibilityThreshold: Float = 0.01f,
  37.     animationLabel: String = "PolylineAnimation",
  38.     animationFinishedListener: ((Float) -> Unit)? = null,
  39.     animationTargetValue: Float = 1f,
  40.     clickable: Boolean = false,
  41.     color: Color = Color.Black,
  42.     endCap: Cap = ButtCap(),
  43.     geodesic: Boolean = false,
  44.     jointType: Int = JointType.DEFAULT,
  45.     pattern: List<PatternItem>? = null,
  46.     startCap: Cap = ButtCap(),
  47.     tag: Any? = null,
  48.     visible: Boolean = true,
  49.     width: Float = 10f,
  50.     zIndex: Float = 0f,
  51.     pointRenderOnIndex: @Composable (Int) -> Unit = {},
  52.     onClick: (Polyline) -> Unit = {}
  53. ) {
  54.     val targetValue by rememberSaveable {
  55.         mutableFloatStateOf(animationTargetValue)
  56.     }
  57.  
  58.     val transition = updateTransition(
  59.         targetState = targetValue,
  60.         label = "animatedPolylineTransition"
  61.     )
  62.  
  63.     val polylineProgress by if (repeatMode != null) {
  64.         rememberInfiniteTransition(label = "animatedPolylineProgress")
  65.             .animateFloat(
  66.                 initialValue = 0f,
  67.                 targetValue = targetValue,
  68.                 animationSpec = animationSpec as InfiniteRepeatableSpec<Float>,
  69.                 label = "animatedPolylineProgressAnimationSpec"
  70.             )
  71.     } else { // <------------- this is the block that is not working
  72.         /*animateFloatAsState(
  73.             targetValue = targetValue,
  74.             label = animationLabel,
  75.             animationSpec = animationSpec,
  76.             visibilityThreshold = animationVisibilityThreshold,
  77.             finishedListener = animationFinishedListener
  78.         )*/
  79.  
  80.         transition.animateFloat(
  81.             transitionSpec = { animationSpec as FiniteAnimationSpec<Float> },
  82.             label = animationLabel,
  83.             targetValueByState = { it },
  84.         )
  85.     }
  86.  
  87.     val displayedPoints by remember(polylineProgress) {
  88.         derivedStateOf {
  89.             val totalPoints = points.size
  90.             val currentEndPoint = (polylineProgress * totalPoints).toInt().coerceAtMost(totalPoints)
  91.             points.take(currentEndPoint)
  92.         }
  93.     }
  94.  
  95.     Polyline(
  96.         points = displayedPoints,
  97.         clickable = clickable,
  98.         color = color,
  99.         endCap = endCap,
  100.         geodesic = geodesic,
  101.         jointType = jointType,
  102.         pattern = pattern,
  103.         startCap = startCap,
  104.         tag = tag,
  105.         visible = visible,
  106.         width = width,
  107.         zIndex = zIndex,
  108.         onClick = onClick,
  109.     )
  110.  
  111.     pointRenderOnIndex(displayedPoints.size - 1)
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement