1
+ /*
2
+ * Copyright (C) 2022 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
+ package com.example.android.compose.motion.demo.visibility
17
+
18
+ import androidx.compose.animation.AnimatedVisibility
19
+ import androidx.compose.animation.fadeIn
20
+ import androidx.compose.animation.fadeOut
21
+ import androidx.compose.foundation.Image
22
+ import androidx.compose.foundation.layout.Box
23
+ import androidx.compose.foundation.layout.fillMaxSize
24
+ import androidx.compose.foundation.layout.padding
25
+ import androidx.compose.foundation.layout.size
26
+ import androidx.compose.foundation.shape.RoundedCornerShape
27
+ import androidx.compose.material3.Button
28
+ import androidx.compose.material3.Text
29
+ import androidx.compose.runtime.Composable
30
+ import androidx.compose.runtime.getValue
31
+ import androidx.compose.runtime.mutableStateOf
32
+ import androidx.compose.runtime.remember
33
+ import androidx.compose.runtime.setValue
34
+ import androidx.compose.ui.Alignment
35
+ import androidx.compose.ui.Modifier
36
+ import androidx.compose.ui.draw.clip
37
+ import androidx.compose.ui.layout.ContentScale
38
+ import androidx.compose.ui.res.painterResource
39
+ import androidx.compose.ui.tooling.preview.Preview
40
+ import androidx.compose.ui.unit.dp
41
+ import com.example.android.compose.motion.demo.CheeseImages
42
+
43
+ @Preview
44
+ @Composable
45
+ fun AnimatedVisibilityExample () {
46
+ var visible by remember {
47
+ mutableStateOf(true )
48
+ }
49
+ Box (modifier = Modifier .fillMaxSize()) {
50
+ AnimatedVisibility (visible,
51
+ modifier = Modifier .align(Alignment .Center ),
52
+ enter = fadeIn(),
53
+ exit = fadeOut()
54
+ ) {
55
+ Image (
56
+ painter = painterResource(id = CheeseImages [0 ]),
57
+ contentDescription = " Cheese" ,
58
+ modifier = Modifier
59
+ .size(256 .dp, 192 .dp)
60
+ .clip(shape = RoundedCornerShape (16 .dp)),
61
+ contentScale = ContentScale .Crop
62
+ )
63
+ }
64
+ Button (
65
+ onClick = { visible = ! visible },
66
+ modifier = Modifier
67
+ .align(Alignment .BottomCenter )
68
+ .padding(64 .dp)
69
+ ) {
70
+ Text (text = " TOGGLE VISIBILITY" )
71
+ }
72
+ }
73
+ }
0 commit comments