|
| 1 | +package com.smarttoolfactory.composeimage.demo |
| 2 | + |
| 3 | +import androidx.compose.foundation.* |
| 4 | +import androidx.compose.foundation.layout.* |
| 5 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 6 | +import androidx.compose.material3.* |
| 7 | +import androidx.compose.runtime.* |
| 8 | +import androidx.compose.ui.Modifier |
| 9 | +import androidx.compose.ui.graphics.Color |
| 10 | +import androidx.compose.ui.graphics.ImageBitmap |
| 11 | +import androidx.compose.ui.layout.ContentScale |
| 12 | +import androidx.compose.ui.platform.LocalContext |
| 13 | +import androidx.compose.ui.res.imageResource |
| 14 | +import androidx.compose.ui.text.font.FontWeight |
| 15 | +import androidx.compose.ui.unit.dp |
| 16 | +import androidx.compose.ui.unit.sp |
| 17 | +import com.smarttoolfactory.composeimage.ContentScaleSelectionMenu |
| 18 | +import com.smarttoolfactory.composeimage.ImageSelectionButton |
| 19 | +import com.smarttoolfactory.composeimage.R |
| 20 | +import com.smarttoolfactory.image.ImageWithConstraints |
| 21 | + |
| 22 | +/** |
| 23 | + * This demo is for comparing results with [Image] and [ImageWithConstraints] |
| 24 | + */ |
| 25 | +@OptIn(ExperimentalMaterial3Api::class) |
| 26 | +@Composable |
| 27 | +fun ImageWithConstraintsDemo() { |
| 28 | + |
| 29 | + val imageBitmapLarge = ImageBitmap.imageResource( |
| 30 | + LocalContext.current.resources, R.drawable.landscape2 |
| 31 | + ) |
| 32 | + |
| 33 | + var imageBitmap by remember { mutableStateOf(imageBitmapLarge) } |
| 34 | + |
| 35 | + Scaffold( |
| 36 | + floatingActionButton = { |
| 37 | + ImageSelectionButton { |
| 38 | + imageBitmap = it |
| 39 | + } |
| 40 | + }, |
| 41 | + floatingActionButtonPosition = FabPosition.End, |
| 42 | + content = { paddingValues: PaddingValues -> |
| 43 | + Column( |
| 44 | + modifier = Modifier |
| 45 | + .padding(paddingValues) |
| 46 | + .fillMaxSize() |
| 47 | + .verticalScroll(rememberScrollState()) |
| 48 | + .padding(10.dp) |
| 49 | + ) { |
| 50 | + ImageScale(imageBitmap = imageBitmap) |
| 51 | + } |
| 52 | + } |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +@Composable |
| 57 | +fun ImageScale(imageBitmap: ImageBitmap) { |
| 58 | + |
| 59 | + val modifier = Modifier |
| 60 | + .background(Color.LightGray) |
| 61 | + .border(2.dp, Color.Red) |
| 62 | + .fillMaxWidth() |
| 63 | + .aspectRatio(4 / 3f) |
| 64 | + |
| 65 | + var text by remember { mutableStateOf("") } |
| 66 | + |
| 67 | + Spacer(modifier = Modifier.height(20.dp)) |
| 68 | + Text( |
| 69 | + text = "ImageWithConstraints ContentScale", |
| 70 | + fontSize = 20.sp, |
| 71 | + fontWeight = FontWeight.Bold, |
| 72 | + color = MaterialTheme.colorScheme.primary, |
| 73 | + modifier = Modifier.padding(8.dp) |
| 74 | + ) |
| 75 | + |
| 76 | + var contentScale by remember { mutableStateOf(ContentScale.Fit) } |
| 77 | + ContentScaleSelectionMenu(contentScale = contentScale) { |
| 78 | + contentScale = it |
| 79 | + } |
| 80 | + |
| 81 | + ImageWithConstraints( |
| 82 | + modifier = modifier, |
| 83 | + imageBitmap = imageBitmap, |
| 84 | + contentDescription = null, |
| 85 | + contentScale = contentScale |
| 86 | + ) { |
| 87 | + |
| 88 | + val imageWidth = this.imageWidth.value.toInt() |
| 89 | + val imageHeight = this.imageHeight.value.toInt() |
| 90 | + val bitmapRect = this.rect |
| 91 | + |
| 92 | + text = "Image width: ${imageWidth}dp, height: ${imageHeight}dp\n" + |
| 93 | + "Bitmap Rect: $bitmapRect" |
| 94 | + |
| 95 | + Spacer( |
| 96 | + modifier = Modifier |
| 97 | + .size(this.imageWidth, this.imageHeight) |
| 98 | + .border(3.dp, Color.Yellow) |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + Spacer(modifier = Modifier.height(8.dp)) |
| 103 | + |
| 104 | + Surface( |
| 105 | + modifier = Modifier |
| 106 | + .fillMaxWidth() |
| 107 | + .wrapContentHeight(), |
| 108 | + shape = RoundedCornerShape(12.dp), |
| 109 | + shadowElevation = 1.dp, |
| 110 | + tonalElevation = 4.dp, |
| 111 | + color = Color.LightGray, |
| 112 | + contentColor = Color.White |
| 113 | + ) { |
| 114 | + Text( |
| 115 | + text = text, |
| 116 | + fontSize = 14.sp, |
| 117 | + fontWeight = FontWeight.Bold, |
| 118 | + modifier = Modifier.padding(8.dp) |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + Text( |
| 123 | + text = "Image Content Scale", |
| 124 | + fontSize = 20.sp, |
| 125 | + fontWeight = FontWeight.Bold, |
| 126 | + color = MaterialTheme.colorScheme.primary, |
| 127 | + modifier = Modifier.padding(8.dp) |
| 128 | + ) |
| 129 | + |
| 130 | + Image( |
| 131 | + modifier = modifier, |
| 132 | + bitmap = imageBitmap, |
| 133 | + contentDescription = null, |
| 134 | + contentScale = contentScale |
| 135 | + ) |
| 136 | + |
| 137 | +} |
0 commit comments