Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import androidx.compose.animation.core.AnimationSpec
- import androidx.compose.animation.core.FiniteAnimationSpec
- import androidx.compose.animation.core.InfiniteRepeatableSpec
- import androidx.compose.animation.core.LinearEasing
- import androidx.compose.animation.core.RepeatMode
- import androidx.compose.animation.core.animateFloat
- import androidx.compose.animation.core.infiniteRepeatable
- import androidx.compose.animation.core.rememberInfiniteTransition
- import androidx.compose.animation.core.tween
- import androidx.compose.animation.core.updateTransition
- import androidx.compose.runtime.Composable
- import androidx.compose.runtime.derivedStateOf
- import androidx.compose.runtime.getValue
- import androidx.compose.runtime.mutableFloatStateOf
- import androidx.compose.runtime.remember
- import androidx.compose.runtime.saveable.rememberSaveable
- import androidx.compose.ui.graphics.Color
- import com.google.android.gms.maps.model.ButtCap
- import com.google.android.gms.maps.model.Cap
- import com.google.android.gms.maps.model.JointType
- import com.google.android.gms.maps.model.LatLng
- import com.google.android.gms.maps.model.PatternItem
- import com.google.android.gms.maps.model.Polyline
- import com.google.maps.android.compose.Polyline
- @Composable
- fun AnimatedPolyline(
- points: List<LatLng>,
- durationMillis: Int = 2000,
- repeatMode: RepeatMode? = null,
- animationSpec: AnimationSpec<Float> = if (repeatMode != null) infiniteRepeatable(
- animation = tween(durationMillis = durationMillis, easing = LinearEasing),
- repeatMode = repeatMode
- ) else tween(durationMillis = durationMillis, easing = LinearEasing),
- animationVisibilityThreshold: Float = 0.01f,
- animationLabel: String = "PolylineAnimation",
- animationFinishedListener: ((Float) -> Unit)? = null,
- animationTargetValue: Float = 1f,
- clickable: Boolean = false,
- color: Color = Color.Black,
- endCap: Cap = ButtCap(),
- geodesic: Boolean = false,
- jointType: Int = JointType.DEFAULT,
- pattern: List<PatternItem>? = null,
- startCap: Cap = ButtCap(),
- tag: Any? = null,
- visible: Boolean = true,
- width: Float = 10f,
- zIndex: Float = 0f,
- pointRenderOnIndex: @Composable (Int) -> Unit = {},
- onClick: (Polyline) -> Unit = {}
- ) {
- val targetValue by rememberSaveable {
- mutableFloatStateOf(animationTargetValue)
- }
- val transition = updateTransition(
- targetState = targetValue,
- label = "animatedPolylineTransition"
- )
- val polylineProgress by if (repeatMode != null) {
- rememberInfiniteTransition(label = "animatedPolylineProgress")
- .animateFloat(
- initialValue = 0f,
- targetValue = targetValue,
- animationSpec = animationSpec as InfiniteRepeatableSpec<Float>,
- label = "animatedPolylineProgressAnimationSpec"
- )
- } else { // <------------- this is the block that is not working
- /*animateFloatAsState(
- targetValue = targetValue,
- label = animationLabel,
- animationSpec = animationSpec,
- visibilityThreshold = animationVisibilityThreshold,
- finishedListener = animationFinishedListener
- )*/
- transition.animateFloat(
- transitionSpec = { animationSpec as FiniteAnimationSpec<Float> },
- label = animationLabel,
- targetValueByState = { it },
- )
- }
- val displayedPoints by remember(polylineProgress) {
- derivedStateOf {
- val totalPoints = points.size
- val currentEndPoint = (polylineProgress * totalPoints).toInt().coerceAtMost(totalPoints)
- points.take(currentEndPoint)
- }
- }
- Polyline(
- points = displayedPoints,
- clickable = clickable,
- color = color,
- endCap = endCap,
- geodesic = geodesic,
- jointType = jointType,
- pattern = pattern,
- startCap = startCap,
- tag = tag,
- visible = visible,
- width = width,
- zIndex = zIndex,
- onClick = onClick,
- )
- pointRenderOnIndex(displayedPoints.size - 1)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement