1 /*
<lambda>null2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.animation.core.samples
18 
19 import androidx.annotation.Sampled
20 import androidx.compose.animation.core.AnimationState
21 import androidx.compose.animation.core.RepeatMode
22 import androidx.compose.animation.core.Spring
23 import androidx.compose.animation.core.animate
24 import androidx.compose.animation.core.animateTo
25 import androidx.compose.animation.core.infiniteRepeatable
26 import androidx.compose.animation.core.isFinished
27 import androidx.compose.animation.core.spring
28 import androidx.compose.animation.core.tween
29 import androidx.compose.foundation.layout.Box
30 import androidx.compose.foundation.layout.fillMaxSize
31 import androidx.compose.material.Icon
32 import androidx.compose.material.icons.Icons
33 import androidx.compose.material.icons.filled.Favorite
34 import androidx.compose.runtime.Composable
35 import androidx.compose.runtime.LaunchedEffect
36 import androidx.compose.runtime.mutableStateOf
37 import androidx.compose.runtime.remember
38 import androidx.compose.ui.Alignment
39 import androidx.compose.ui.Modifier
40 import androidx.compose.ui.graphics.Color
41 import androidx.compose.ui.graphics.graphicsLayer
42 
43 @Sampled
44 fun animateToOnAnimationState() {
45     @Composable
46     fun simpleAnimate(
47         target: Float,
48     ): Float {
49         // Create an AnimationState to be updated by the animation.
50         val animationState = remember { AnimationState(target) }
51 
52         // Launch the suspend animation into the composition's CoroutineContext, and pass
53         // `target` to LaunchedEffect so that when`target` changes the old animation job is
54         // canceled, and a new animation is created with a new target.
55         LaunchedEffect(target) {
56             // This starts an animation that updates the animationState on each frame
57             animationState.animateTo(
58                 targetValue = target,
59                 // Use a low stiffness spring. This can be replaced with any type of `AnimationSpec`
60                 animationSpec = spring(stiffness = Spring.StiffnessLow),
61                 // If the previous animation was interrupted (i.e. not finished), configure the
62                 // animation as a sequential animation to continue from the time the animation was
63                 // interrupted.
64                 sequentialAnimation = !animationState.isFinished
65             )
66             // When the function above returns, the animation has finished.
67         }
68         // Return the value updated by the animation.
69         return animationState.value
70     }
71 }
72 
73 @Sampled
suspendAnimateFloatVariantnull74 fun suspendAnimateFloatVariant() {
75     @Composable
76     fun InfiniteAnimationDemo() {
77         // Create a mutable state for alpha, and update it in the animation.
78         val alpha = remember { mutableStateOf(1f) }
79         LaunchedEffect(Unit) {
80             // Animate from 1f to 0f using an infinitely repeating animation
81             animate(
82                 initialValue = 1f,
83                 targetValue = 0f,
84                 animationSpec =
85                     infiniteRepeatable(animation = tween(1000), repeatMode = RepeatMode.Reverse)
86             ) { value, /* velocity */ _ ->
87                 // Update alpha mutable state with the current animation value
88                 alpha.value = value
89             }
90         }
91         Box(Modifier.fillMaxSize()) {
92             Icon(
93                 Icons.Filled.Favorite,
94                 contentDescription = null,
95                 modifier =
96                     Modifier.align(Alignment.Center)
97                         .graphicsLayer(scaleX = 3.0f, scaleY = 3.0f, alpha = alpha.value),
98                 tint = Color.Red
99             )
100         }
101     }
102 }
103