Andrey Kulikov | eb7a285 | 2019-10-18 18:04:57 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.ui.animation.samples |
| 18 | |
| 19 | import androidx.animation.transitionDefinition |
| 20 | import androidx.annotation.Sampled |
| 21 | import androidx.compose.Composable |
| 22 | import androidx.ui.animation.ColorPropKey |
Doris Liu | 4eb1032 | 2020-02-11 17:59:27 -0800 | [diff] [blame] | 23 | import androidx.ui.animation.DpPropKey |
Andrey Kulikov | eb7a285 | 2019-10-18 18:04:57 +0100 | [diff] [blame] | 24 | import androidx.ui.animation.Transition |
| 25 | import androidx.ui.foundation.ColoredRect |
| 26 | import androidx.ui.graphics.Color |
Doris Liu | 4eb1032 | 2020-02-11 17:59:27 -0800 | [diff] [blame] | 27 | import androidx.ui.unit.dp |
Andrey Kulikov | eb7a285 | 2019-10-18 18:04:57 +0100 | [diff] [blame] | 28 | |
| 29 | private enum class State { |
| 30 | First, |
| 31 | Second |
| 32 | } |
| 33 | |
| 34 | @Sampled |
| 35 | fun TransitionSample() { |
Doris Liu | 4eb1032 | 2020-02-11 17:59:27 -0800 | [diff] [blame] | 36 | val colorKey = ColorPropKey() |
| 37 | val widthKey = DpPropKey() |
| 38 | val heightKey = DpPropKey() |
| 39 | |
Andrey Kulikov | eb7a285 | 2019-10-18 18:04:57 +0100 | [diff] [blame] | 40 | val definition = transitionDefinition { |
| 41 | state(State.First) { |
Doris Liu | 4eb1032 | 2020-02-11 17:59:27 -0800 | [diff] [blame] | 42 | this[colorKey] = Color.Red |
| 43 | this[widthKey] = 200.dp |
| 44 | this[heightKey] = 400.dp |
Andrey Kulikov | eb7a285 | 2019-10-18 18:04:57 +0100 | [diff] [blame] | 45 | } |
| 46 | state(State.Second) { |
Doris Liu | 4eb1032 | 2020-02-11 17:59:27 -0800 | [diff] [blame] | 47 | this[colorKey] = Color.Green |
| 48 | this[widthKey] = 300.dp |
| 49 | this[heightKey] = 300.dp |
Andrey Kulikov | eb7a285 | 2019-10-18 18:04:57 +0100 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | @Composable |
| 54 | fun TransitionBasedColoredRect() { |
Doris Liu | a241991 | 2019-11-27 14:18:59 -0800 | [diff] [blame] | 55 | // 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 Liu | 4eb1032 | 2020-02-11 17:59:27 -0800 | [diff] [blame] | 58 | ColoredRect(color = state[colorKey], width = state[widthKey], height = state[heightKey]) |
Andrey Kulikov | eb7a285 | 2019-10-18 18:04:57 +0100 | [diff] [blame] | 59 | } |
| 60 | } |
Doris Liu | a241991 | 2019-11-27 14:18:59 -0800 | [diff] [blame] | 61 | |
| 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 Liu | 4eb1032 | 2020-02-11 17:59:27 -0800 | [diff] [blame] | 68 | ColoredRect(color = state[colorKey], width = state[widthKey], height = state[heightKey]) |
Doris Liu | a241991 | 2019-11-27 14:18:59 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |