Advertisement
rvinter

Untitled

Sep 11th, 2022
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.98 KB | None | 0 0
  1. @Composable
  2. private fun PulsatingRipple() {
  3.     ButtonBox {
  4.         val opacityFirstWave = remember { Animatable(0.5f) }
  5.         val sizeFirstWave = remember { Animatable(122f) }
  6.  
  7.         val opacitySecondWave = remember { Animatable(0.5f) }
  8.         val sizeSecondWave = remember { Animatable(122f) }
  9.  
  10.         ValueAnimationEffect(value = opacityFirstWave, targetValue = 0f)
  11.         ValueAnimationEffect(value = sizeFirstWave, targetValue = 170f)
  12.  
  13.         ValueAnimationEffect(
  14.             value = opacitySecondWave,
  15.             targetValue = 0f,
  16.             initialOffset = StartOffset(500)
  17.         )
  18.         ValueAnimationEffect(
  19.             value = sizeSecondWave,
  20.             targetValue = 170f,
  21.             initialOffset = StartOffset(500)
  22.         )
  23.  
  24.         PrimaryCircle(size = sizeFirstWave.value.dp, alpha = opacityFirstWave.value)
  25.         PrimaryCircle(size = sizeSecondWave.value.dp, alpha = opacitySecondWave.value)
  26.     }
  27. }
  28.  
  29. @Composable
  30. private fun ValueAnimationEffect(
  31.     value: Animatable<Float, *>,
  32.     targetValue: Float,
  33.     initialOffset: StartOffset = StartOffset(0)
  34. ) {
  35.     LaunchedEffect(key1 = true) {
  36.         value.animateTo(
  37.             targetValue = targetValue,
  38.             animationSpec = infiniteRepeatable(
  39.                 animation = tween(1000, easing = LinearEasing),
  40.                 repeatMode = RepeatMode.Restart,
  41.                 initialStartOffset = initialOffset,
  42.             ),
  43.         )
  44.     }
  45. }
  46.  
  47. @Composable
  48. private fun PrimaryCircle(size: Dp, alpha: Float) {
  49.     val color = MaterialTheme.colorScheme.primary
  50.  
  51.     Canvas(
  52.         modifier = Modifier
  53.             .size(size)
  54.             .alpha(alpha)
  55.     ) {
  56.         drawCircle(color = color)
  57.     }
  58. }
  59.  
  60. @Composable
  61. private fun ButtonBox(
  62.     modifier: Modifier = Modifier,
  63.     content: @Composable BoxScope.() -> Unit,
  64. ) {
  65.     Box(
  66.         modifier = modifier.size(BUTTON_CONTAINER_SIZE.dp),
  67.         contentAlignment = Alignment.Center,
  68.         content = content,
  69.     )
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement