Skip to content

Commit 273d399

Browse files
authored
Migrated relevant MotionCompose snippets into snippets (#411)
* Migrated relevant MotionCompose snippets into snippets * Apply Spotless --------- Co-authored-by: riggaroo <[email protected]>
1 parent c79a414 commit 273d399

File tree

14 files changed

+1262
-4
lines changed

14 files changed

+1262
-4
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright 2024 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+
* https://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 com.example.compose.snippets.animations.demo
18+
19+
import androidx.annotation.DrawableRes
20+
import com.example.compose.snippets.R
21+
22+
val CheeseImages = listOf(
23+
R.drawable.cheese_1,
24+
R.drawable.cheese_2,
25+
R.drawable.cheese_3,
26+
R.drawable.cheese_4,
27+
R.drawable.cheese_5
28+
)
29+
30+
val CheeseNames = listOf(
31+
"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
32+
"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale",
33+
"Aisy Cendre", "Allgauer Emmentaler", "Alverca", "Ambert", "American Cheese",
34+
"Ami du Chambertin", "Anejo Enchilado", "Anneau du Vic-Bilh", "Anthoriro", "Appenzell",
35+
"Aragon", "Ardi Gasna", "Ardrahan", "Armenian String", "Aromes au Gene de Marc",
36+
"Asadero", "Asiago", "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss",
37+
"Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", "Banon",
38+
"Barry's Bay Cheddar", "Basing", "Basket Cheese", "Bath Cheese", "Bavarian Bergkase",
39+
"Baylough", "Beaufort", "Beauvoorde", "Beenleigh Blue", "Beer Cheese", "Bel Paese",
40+
"Bergader", "Bergere Bleue", "Berkswell", "Beyaz Peynir", "Bierkase", "Bishop Kennedy",
41+
"Blarney", "Bleu d'Auvergne", "Bleu de Gex", "Bleu de Laqueuille",
42+
"Bleu de Septmoncel", "Bleu Des Causses", "Blue", "Blue Castello", "Blue Rathgore",
43+
"Blue Vein (Australian)", "Blue Vein Cheeses", "Bocconcini", "Bocconcini (Australian)",
44+
"Boeren Leidenkaas", "Bonchester", "Bosworth", "Bougon", "Boule Du Roves",
45+
"Boulette d'Avesnes", "Boursault", "Boursin", "Bouyssou", "Bra", "Braudostur",
46+
"Breakfast Cheese", "Brebis du Lavort", "Brebis du Lochois", "Brebis du Puyfaucon",
47+
"Bresse Bleu", "Brick", "Brie", "Brie de Meaux", "Brie de Melun", "Brillat-Savarin",
48+
"Brin", "Brin d' Amour", "Brin d'Amour", "Brinza (Burduf Brinza)",
49+
"Briquette de Brebis", "Briquette du Forez", "Broccio", "Broccio Demi-Affine",
50+
"Brousse du Rove", "Bruder Basil", "Brusselae Kaas (Fromage de Bruxelles)", "Bryndza",
51+
"Buchette d'Anjou", "Buffalo", "Burgos", "Butte", "Butterkase", "Button (Innes)",
52+
"Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille", "Caciocavallo", "Caciotta",
53+
"Caerphilly", "Cairnsmore", "Calenzana", "Cambazola", "Camembert de Normandie",
54+
"Canadian Cheddar", "Canestrato", "Cantal", "Caprice des Dieux", "Capricorn Goat",
55+
"Capriole Banon", "Carre de l'Est", "Casciotta di Urbino", "Cashel Blue", "Castellano",
56+
"Castelleno", "Castelmagno", "Castelo Branco", "Castigliano", "Cathelain",
57+
"Celtic Promise", "Cendre d'Olivet", "Cerney", "Chabichou", "Chabichou du Poitou",
58+
"Chabis de Gatine", "Chaource", "Charolais", "Chaumes", "Cheddar",
59+
"Cheddar Clothbound", "Cheshire", "Chevres", "Chevrotin des Aravis", "Chontaleno",
60+
"Civray", "Coeur de Camembert au Calvados", "Coeur de Chevre", "Colby", "Cold Pack",
61+
"Comte", "Coolea", "Cooleney", "Coquetdale", "Corleggy", "Cornish Pepper",
62+
"Cotherstone", "Cotija", "Cottage Cheese", "Cottage Cheese (Australian)",
63+
"Cougar Gold", "Coulommiers", "Coverdale", "Crayeux de Roncq", "Cream Cheese",
64+
"Cream Havarti", "Crema Agria", "Crema Mexicana", "Creme Fraiche", "Crescenza",
65+
"Croghan", "Crottin de Chavignol", "Crottin du Chavignol", "Crowdie", "Crowley",
66+
"Cuajada", "Curd", "Cure Nantais", "Curworthy", "Cwmtawe Pecorino",
67+
"Cypress Grove Chevre", "Danablu (Danish Blue)", "Danbo", "Danish Fontina",
68+
"Daralagjazsky", "Dauphin", "Delice des Fiouves", "Denhany Dorset Drum", "Derby",
69+
"Dessertnyj Belyj", "Devon Blue", "Devon Garland", "Dolcelatte", "Doolin",
70+
"Doppelrhamstufel", "Dorset Blue Vinney", "Double Gloucester", "Double Worcester",
71+
"Dreux a la Feuille", "Dry Jack", "Duddleswell", "Dunbarra", "Dunlop", "Dunsyre Blue",
72+
"Duroblando", "Durrus", "Dutch Mimolette (Commissiekaas)", "Edam", "Edelpilz",
73+
"Emental Grand Cru", "Emlett", "Emmental", "Epoisses de Bourgogne", "Esbareich",
74+
"Esrom", "Etorki", "Evansdale Farmhouse Brie", "Evora De L'Alentejo", "Exmoor Blue",
75+
"Explorateur", "Feta", "Feta (Australian)", "Figue", "Filetta", "Fin-de-Siecle",
76+
"Finlandia Swiss", "Finn", "Fiore Sardo", "Fleur du Maquis", "Flor de Guia",
77+
"Flower Marie", "Folded", "Folded cheese with mint", "Fondant de Brebis",
78+
"Fontainebleau", "Fontal", "Fontina Val d'Aosta", "Formaggio di capra", "Fougerus",
79+
"Four Herb Gouda", "Fourme d' Ambert", "Fourme de Haute Loire", "Fourme de Montbrison",
80+
"Fresh Jack", "Fresh Mozzarella", "Fresh Ricotta", "Fresh Truffles", "Fribourgeois",
81+
"Friesekaas", "Friesian", "Friesla", "Frinault", "Fromage a Raclette", "Fromage Corse",
82+
"Fromage de Montagne de Savoie", "Fromage Frais", "Fruit Cream Cheese",
83+
"Frying Cheese", "Fynbo", "Gabriel", "Galette du Paludier", "Galette Lyonnaise",
84+
"Galloway Goat's Milk Gems", "Gammelost", "Gaperon a l'Ail", "Garrotxa", "Gastanberra",
85+
"Geitost", "Gippsland Blue", "Gjetost", "Gloucester", "Golden Cross", "Gorgonzola",
86+
"Gornyaltajski", "Gospel Green", "Gouda", "Goutu", "Gowrie", "Grabetto", "Graddost",
87+
"Grafton Village Cheddar", "Grana", "Grana Padano", "Grand Vatel",
88+
"Grataron d' Areches", "Gratte-Paille", "Graviera", "Greuilh", "Greve",
89+
"Gris de Lille", "Gruyere", "Gubbeen", "Guerbigny", "Halloumi",
90+
"Halloumy (Australian)", "Haloumi-Style Cheese", "Harbourne Blue", "Havarti",
91+
"Heidi Gruyere", "Hereford Hop", "Herrgardsost", "Herriot Farmhouse", "Herve",
92+
"Hipi Iti", "Hubbardston Blue Cow", "Hushallsost", "Iberico", "Idaho Goatster",
93+
"Idiazabal", "Il Boschetto al Tartufo", "Ile d'Yeu", "Isle of Mull", "Jarlsberg",
94+
"Jermi Tortes", "Jibneh Arabieh", "Jindi Brie", "Jubilee Blue", "Juustoleipa",
95+
"Kadchgall", "Kaseri", "Kashta", "Kefalotyri", "Kenafa", "Kernhem", "Kervella Affine",
96+
"Kikorangi", "King Island Cape Wickham Brie", "King River Gold", "Klosterkaese",
97+
"Knockalara", "Kugelkase", "L'Aveyronnais", "L'Ecir de l'Aubrac", "La Taupiniere",
98+
"La Vache Qui Rit", "Laguiole", "Lairobell", "Lajta", "Lanark Blue", "Lancashire",
99+
"Langres", "Lappi", "Laruns", "Lavistown", "Le Brin", "Le Fium Orbo", "Le Lacandou",
100+
"Le Roule", "Leafield", "Lebbene", "Leerdammer", "Leicester", "Leyden", "Limburger",
101+
"Lincolnshire Poacher", "Lingot Saint Bousquet d'Orb", "Liptauer", "Little Rydings",
102+
"Livarot", "Llanboidy", "Llanglofan Farmhouse", "Loch Arthur Farmhouse",
103+
"Loddiswell Avondale", "Longhorn", "Lou Palou", "Lou Pevre", "Lyonnais", "Maasdam",
104+
"Macconais", "Mahoe Aged Gouda", "Mahon", "Malvern", "Mamirolle", "Manchego",
105+
"Manouri", "Manur", "Marble Cheddar", "Marbled Cheeses", "Maredsous", "Margotin",
106+
"Maribo", "Maroilles", "Mascares", "Mascarpone", "Mascarpone (Australian)",
107+
"Mascarpone Torta", "Matocq", "Maytag Blue", "Meira", "Menallack Farmhouse",
108+
"Menonita", "Meredith Blue", "Mesost", "Metton (Cancoillotte)", "Meyer Vintage Gouda",
109+
"Mihalic Peynir", "Milleens", "Mimolette", "Mine-Gabhar", "Mini Baby Bells", "Mixte",
110+
"Molbo", "Monastery Cheeses", "Mondseer", "Mont D'or Lyonnais", "Montasio",
111+
"Monterey Jack", "Monterey Jack Dry", "Morbier", "Morbier Cru de Montagne",
112+
"Mothais a la Feuille", "Mozzarella", "Mozzarella (Australian)",
113+
"Mozzarella di Bufala", "Mozzarella Fresh, in water", "Mozzarella Rolls", "Munster",
114+
"Murol", "Mycella", "Myzithra", "Naboulsi", "Nantais", "Neufchatel",
115+
"Neufchatel (Australian)", "Niolo", "Nokkelost", "Northumberland", "Oaxaca",
116+
"Olde York", "Olivet au Foin", "Olivet Bleu", "Olivet Cendre",
117+
"Orkney Extra Mature Cheddar", "Orla", "Oschtjepka", "Ossau Fermier", "Ossau-Iraty",
118+
"Oszczypek", "Oxford Blue", "P'tit Berrichon", "Palet de Babligny", "Paneer", "Panela",
119+
"Pannerone", "Pant ys Gawn", "Parmesan (Parmigiano)", "Parmigiano Reggiano",
120+
"Pas de l'Escalette", "Passendale", "Pasteurized Processed", "Pate de Fromage",
121+
"Patefine Fort", "Pave d'Affinois", "Pave d'Auge", "Pave de Chirac", "Pave du Berry",
122+
"Pecorino", "Pecorino in Walnut Leaves", "Pecorino Romano", "Peekskill Pyramid",
123+
"Pelardon des Cevennes", "Pelardon des Corbieres", "Penamellera", "Penbryn",
124+
"Pencarreg", "Perail de Brebis", "Petit Morin", "Petit Pardou", "Petit-Suisse",
125+
"Picodon de Chevre", "Picos de Europa", "Piora", "Pithtviers au Foin",
126+
"Plateau de Herve", "Plymouth Cheese", "Podhalanski", "Poivre d'Ane", "Polkolbin",
127+
"Pont l'Eveque", "Port Nicholson", "Port-Salut", "Postel", "Pouligny-Saint-Pierre",
128+
"Pourly", "Prastost", "Pressato", "Prince-Jean", "Processed Cheddar", "Provolone",
129+
"Provolone (Australian)", "Pyengana Cheddar", "Pyramide", "Quark",
130+
"Quark (Australian)", "Quartirolo Lombardo", "Quatre-Vents", "Quercy Petit",
131+
"Queso Blanco", "Queso Blanco con Frutas --Pina y Mango", "Queso de Murcia",
132+
"Queso del Montsec", "Queso del Tietar", "Queso Fresco", "Queso Fresco (Adobera)",
133+
"Queso Iberico", "Queso Jalapeno", "Queso Majorero", "Queso Media Luna",
134+
"Queso Para Frier", "Queso Quesadilla", "Rabacal", "Raclette", "Ragusano", "Raschera",
135+
"Reblochon", "Red Leicester", "Regal de la Dombes", "Reggianito", "Remedou",
136+
"Requeson", "Richelieu", "Ricotta", "Ricotta (Australian)", "Ricotta Salata", "Ridder",
137+
"Rigotte", "Rocamadour", "Rollot", "Romano", "Romans Part Dieu", "Roncal", "Roquefort",
138+
"Roule", "Rouleau De Beaulieu", "Royalp Tilsit", "Rubens", "Rustinu", "Saaland Pfarr",
139+
"Saanenkaese", "Saga", "Sage Derby", "Sainte Maure", "Saint-Marcellin",
140+
"Saint-Nectaire", "Saint-Paulin", "Salers", "Samso", "San Simon", "Sancerre",
141+
"Sap Sago", "Sardo", "Sardo Egyptian", "Sbrinz", "Scamorza", "Schabzieger", "Schloss",
142+
"Selles sur Cher", "Selva", "Serat", "Seriously Strong Cheddar", "Serra da Estrela",
143+
"Sharpam", "Shelburne Cheddar", "Shropshire Blue", "Siraz", "Sirene", "Smoked Gouda",
144+
"Somerset Brie", "Sonoma Jack", "Sottocenare al Tartufo", "Soumaintrain",
145+
"Sourire Lozerien", "Spenwood", "Sraffordshire Organic", "St. Agur Blue Cheese",
146+
"Stilton", "Stinking Bishop", "String", "Sussex Slipcote", "Sveciaost", "Swaledale",
147+
"Sweet Style Swiss", "Swiss", "Syrian (Armenian String)", "Tala", "Taleggio", "Tamie",
148+
"Tasmania Highland Chevre Log", "Taupiniere", "Teifi", "Telemea", "Testouri",
149+
"Tete de Moine", "Tetilla", "Texas Goat Cheese", "Tibet", "Tillamook Cheddar",
150+
"Tilsit", "Timboon Brie", "Toma", "Tomme Brulee", "Tomme d'Abondance",
151+
"Tomme de Chevre", "Tomme de Romans", "Tomme de Savoie", "Tomme des Chouans", "Tommes",
152+
"Torta del Casar", "Toscanello", "Touree de L'Aubier", "Tourmalet",
153+
"Trappe (Veritable)", "Trois Cornes De Vendee", "Tronchon", "Trou du Cru", "Truffe",
154+
"Tupi", "Turunmaa", "Tymsboro", "Tyn Grug", "Tyning", "Ubriaco", "Ulloa",
155+
"Vacherin-Fribourgeois", "Valencay", "Vasterbottenost", "Venaco", "Vendomois",
156+
"Vieux Corse", "Vignotte", "Vulscombe", "Waimata Farmhouse Blue",
157+
"Washed Rind Cheese (Australian)", "Waterloo", "Weichkaese", "Wellington",
158+
"Wensleydale", "White Stilton", "Whitestone Farmhouse", "Wigmore", "Woodside Cabecou",
159+
"Xanadu", "Xynotyro", "Yarg Cornish", "Yarra Valley Pyramid", "Yorkshire Blue",
160+
"Zamorano", "Zanetti Grana Padano", "Zanetti Parmigiano Reggiano"
161+
)
162+
163+
data class Cheese(
164+
val id: Long,
165+
val name: String,
166+
@DrawableRes val image: Int
167+
) {
168+
companion object {
169+
fun all(): List<Cheese> {
170+
return CheeseNames.mapIndexed { i, name ->
171+
Cheese(
172+
id = (i + 1).toLong(),
173+
name = name,
174+
image = CheeseImages[
175+
((name.hashCode() % CheeseImages.size) + CheeseImages.size) %
176+
CheeseImages.size
177+
]
178+
)
179+
}
180+
}
181+
}
182+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 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+
* https://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 com.example.compose.snippets.animations.demo
18+
19+
import androidx.compose.foundation.layout.Box
20+
import androidx.compose.foundation.layout.BoxScope
21+
import androidx.compose.foundation.layout.fillMaxSize
22+
import androidx.compose.foundation.layout.padding
23+
import androidx.compose.foundation.layout.statusBarsPadding
24+
import androidx.compose.material3.ExperimentalMaterial3Api
25+
import androidx.compose.material3.Scaffold
26+
import androidx.compose.material3.Text
27+
import androidx.compose.material3.TopAppBar
28+
import androidx.compose.runtime.Composable
29+
import androidx.compose.ui.Modifier
30+
31+
@OptIn(ExperimentalMaterial3Api::class)
32+
@Composable
33+
fun SimpleScaffold(
34+
title: String,
35+
modifier: Modifier = Modifier,
36+
content: @Composable BoxScope.() -> Unit
37+
) {
38+
Scaffold(
39+
topBar = {
40+
TopAppBar(
41+
title = {
42+
Text(text = title)
43+
},
44+
modifier = Modifier
45+
.statusBarsPadding()
46+
)
47+
},
48+
modifier = modifier
49+
) { padding ->
50+
Box(
51+
modifier = Modifier
52+
.fillMaxSize()
53+
.padding(padding)
54+
) {
55+
content()
56+
}
57+
}
58+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2024 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+
* https://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 com.example.compose.snippets.animations.demo.custom
18+
19+
import androidx.compose.animation.core.Animatable
20+
import androidx.compose.animation.core.Spring
21+
import androidx.compose.animation.core.SpringSpec
22+
import androidx.compose.animation.core.spring
23+
import androidx.compose.foundation.Canvas
24+
import androidx.compose.foundation.layout.Box
25+
import androidx.compose.foundation.layout.fillMaxSize
26+
import androidx.compose.foundation.layout.size
27+
import androidx.compose.runtime.Composable
28+
import androidx.compose.runtime.LaunchedEffect
29+
import androidx.compose.runtime.mutableStateOf
30+
import androidx.compose.runtime.remember
31+
import androidx.compose.ui.Alignment
32+
import androidx.compose.ui.Modifier
33+
import androidx.compose.ui.graphics.Brush
34+
import androidx.compose.ui.graphics.Color
35+
import androidx.compose.ui.graphics.graphicsLayer
36+
import androidx.compose.ui.layout.onSizeChanged
37+
import androidx.compose.ui.tooling.preview.Preview
38+
import androidx.compose.ui.unit.IntSize
39+
import androidx.compose.ui.unit.dp
40+
import kotlinx.coroutines.delay
41+
42+
/**
43+
* A custom loading animation example using Canvas and draw APIs, combined with
44+
* Animatable to show the use of the animateTo() function used sequentially.
45+
*/
46+
@Composable
47+
@Preview
48+
fun CustomCanvasBouncyLoader() {
49+
val yOffset = remember {
50+
Animatable(0f)
51+
}
52+
val scale = remember {
53+
Animatable(1f)
54+
}
55+
LaunchedEffect("bouncyLoader") {
56+
delay(400)
57+
// We use the Animatable.animateTo() API here to demonstrate the coroutine usage - each
58+
// item is animating one after the other, as the animateTo function is sequential.
59+
// Animate y to half the height
60+
yOffset.animateTo(0.5f, bouncyAnimationSpec)
61+
scale.animateTo(3f, bouncyAnimationSpec)
62+
delay(500)
63+
scale.animateTo(10f, bouncyAnimationSpec)
64+
delay(500)
65+
scale.animateTo(50f, bouncyAnimationSpec)
66+
}
67+
val size = remember {
68+
mutableStateOf(IntSize.Zero)
69+
}
70+
Box(
71+
Modifier
72+
.fillMaxSize()
73+
.onSizeChanged {
74+
// We get the size change of the whole composable, and use this to determine how
75+
// big the ball should be.
76+
size.value = it
77+
}
78+
) {
79+
GradientCircle(
80+
Modifier
81+
.align(Alignment.TopCenter)
82+
.size(26.dp)
83+
.graphicsLayer {
84+
// We use .graphicsLayer here to perform the animation as we are animating
85+
// multiple properties of our Gradient circle at once, and this is more
86+
// efficient than using multiple modifiers.
87+
// .graphicsLayer also defers these changes to the Draw phase of Compose,
88+
// therefore minimizing recompositions required to do this.
89+
scaleX = scale.value
90+
scaleY = scale.value
91+
translationY = yOffset.value * size.value.height
92+
}
93+
)
94+
}
95+
}
96+
97+
@Composable
98+
private fun GradientCircle(modifier: Modifier = Modifier) {
99+
val brush = remember {
100+
Brush.verticalGradient(listOf(Color(0xFFF56E34), Color(0xFF234EDA)))
101+
}
102+
Canvas(modifier = modifier) {
103+
drawCircle(brush = brush)
104+
}
105+
}
106+
107+
private val bouncyAnimationSpec: SpringSpec<Float> = spring(
108+
dampingRatio = Spring.DampingRatioMediumBouncy,
109+
stiffness = Spring.StiffnessLow
110+
)

0 commit comments

Comments
 (0)