|
| 1 | +/* |
| 2 | + * Copyright 2023 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.layouts |
| 18 | + |
| 19 | +import androidx.compose.foundation.Image |
| 20 | +import androidx.compose.foundation.background |
| 21 | +import androidx.compose.foundation.layout.Box |
| 22 | +import androidx.compose.foundation.layout.fillMaxSize |
| 23 | +import androidx.compose.foundation.layout.padding |
| 24 | +import androidx.compose.foundation.layout.requiredSize |
| 25 | +import androidx.compose.foundation.layout.size |
| 26 | +import androidx.compose.foundation.layout.wrapContentSize |
| 27 | +import androidx.compose.foundation.shape.CircleShape |
| 28 | +import androidx.compose.runtime.Composable |
| 29 | +import androidx.compose.ui.Alignment |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.draw.clip |
| 32 | +import androidx.compose.ui.graphics.Color |
| 33 | +import androidx.compose.ui.res.painterResource |
| 34 | +import androidx.compose.ui.tooling.preview.Preview |
| 35 | +import androidx.compose.ui.unit.dp |
| 36 | +import com.example.compose.snippets.R |
| 37 | + |
| 38 | +val containerModifier = Modifier.requiredSize(300.dp, 200.dp).background(Color(0xFFD1F1FE)) |
| 39 | + |
| 40 | +@Preview |
| 41 | +@Composable |
| 42 | +private fun Demo1() { |
| 43 | + Box(containerModifier, contentAlignment = Alignment.Center) { |
| 44 | + // [START android_compose_layout_constraints_modifiers_1] |
| 45 | + Image( |
| 46 | + painterResource(R.drawable.hero), |
| 47 | + contentDescription = null, |
| 48 | + Modifier |
| 49 | + .fillMaxSize() |
| 50 | + .size(50.dp) |
| 51 | + ) |
| 52 | + // [END android_compose_layout_constraints_modifiers_1] |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +@Preview |
| 57 | +@Composable |
| 58 | +private fun Demo2() { |
| 59 | + Box(containerModifier, contentAlignment = Alignment.Center) { |
| 60 | + // [START android_compose_layout_constraints_modifiers_2] |
| 61 | + Image( |
| 62 | + painterResource(R.drawable.hero), |
| 63 | + contentDescription = null, |
| 64 | + Modifier |
| 65 | + .fillMaxSize() |
| 66 | + .wrapContentSize() |
| 67 | + .size(50.dp) |
| 68 | + ) |
| 69 | + // [END android_compose_layout_constraints_modifiers_2] |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +@Preview |
| 74 | +@Composable |
| 75 | +private fun Demo3() { |
| 76 | + Box(containerModifier, contentAlignment = Alignment.Center) { |
| 77 | + // [START android_compose_layout_constraints_modifiers_3] |
| 78 | + Image( |
| 79 | + painterResource(R.drawable.hero), |
| 80 | + contentDescription = null, |
| 81 | + Modifier |
| 82 | + .clip(CircleShape) |
| 83 | + .padding(10.dp) |
| 84 | + .size(100.dp) |
| 85 | + ) |
| 86 | + // [END android_compose_layout_constraints_modifiers_3] |
| 87 | + } |
| 88 | +} |
0 commit comments