[go: nahoru, domu]

blob: d19dbe1e36f24958a207db07edfc77346c38a037 [file] [log] [blame]
Andrey Kulikoveb7a2852019-10-18 18:04:57 +01001/*
2 * Copyright 2019 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
17package androidx.ui.animation.samples
18
19import androidx.animation.transitionDefinition
20import androidx.annotation.Sampled
21import androidx.compose.Composable
22import androidx.ui.animation.ColorPropKey
Doris Liu4eb10322020-02-11 17:59:27 -080023import androidx.ui.animation.DpPropKey
Andrey Kulikoveb7a2852019-10-18 18:04:57 +010024import androidx.ui.animation.Transition
25import androidx.ui.foundation.ColoredRect
26import androidx.ui.graphics.Color
Doris Liu4eb10322020-02-11 17:59:27 -080027import androidx.ui.unit.dp
Andrey Kulikoveb7a2852019-10-18 18:04:57 +010028
29private enum class State {
30 First,
31 Second
32}
33
34@Sampled
35fun TransitionSample() {
Doris Liu4eb10322020-02-11 17:59:27 -080036 val colorKey = ColorPropKey()
37 val widthKey = DpPropKey()
38 val heightKey = DpPropKey()
39
Andrey Kulikoveb7a2852019-10-18 18:04:57 +010040 val definition = transitionDefinition {
41 state(State.First) {
Doris Liu4eb10322020-02-11 17:59:27 -080042 this[colorKey] = Color.Red
43 this[widthKey] = 200.dp
44 this[heightKey] = 400.dp
Andrey Kulikoveb7a2852019-10-18 18:04:57 +010045 }
46 state(State.Second) {
Doris Liu4eb10322020-02-11 17:59:27 -080047 this[colorKey] = Color.Green
48 this[widthKey] = 300.dp
49 this[heightKey] = 300.dp
Andrey Kulikoveb7a2852019-10-18 18:04:57 +010050 }
51 }
52
53 @Composable
54 fun TransitionBasedColoredRect() {
Doris Liua2419912019-11-27 14:18:59 -080055 // This puts the transition in State.First. Any subsequent state change will trigger a
56 // transition animation, as defined in the transition definition.
57 Transition(definition = definition, toState = State.First) { state ->
Doris Liu4eb10322020-02-11 17:59:27 -080058 ColoredRect(color = state[colorKey], width = state[widthKey], height = state[heightKey])
Andrey Kulikoveb7a2852019-10-18 18:04:57 +010059 }
60 }
Doris Liua2419912019-11-27 14:18:59 -080061
62 @Composable
63 fun ColorRectWithInitState() {
64 // This starts the transition going from State.First to State.Second when this composable
65 // gets composed for the first time.
66 Transition(definition = definition, initState = State.First, toState = State.Second) {
67 state ->
Doris Liu4eb10322020-02-11 17:59:27 -080068 ColoredRect(color = state[colorKey], width = state[widthKey], height = state[heightKey])
Doris Liua2419912019-11-27 14:18:59 -080069 }
70 }
71}