Skip to content

Commit 131a0a2

Browse files
authored
Merge pull request #2659 from DataDog/yl/compose/add-content-scale-screen
RUM-9726: Add sample screen for compose image content scale
2 parents dc61c07 + c200786 commit 131a0a2

File tree

2 files changed

+103
-12
lines changed

2 files changed

+103
-12
lines changed

sample/kotlin/src/main/kotlin/com/datadog/android/sample/compose/ImageSample.kt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ package com.datadog.android.sample.compose
88

99
import androidx.compose.foundation.Image
1010
import androidx.compose.foundation.background
11+
import androidx.compose.foundation.layout.Column
1112
import androidx.compose.foundation.layout.Row
13+
import androidx.compose.foundation.layout.Spacer
1214
import androidx.compose.foundation.layout.fillMaxSize
15+
import androidx.compose.foundation.layout.height
1316
import androidx.compose.foundation.layout.padding
1417
import androidx.compose.foundation.layout.size
1518
import androidx.compose.foundation.lazy.LazyColumn
1619
import androidx.compose.foundation.shape.CircleShape
1720
import androidx.compose.foundation.shape.RoundedCornerShape
1821
import androidx.compose.material.Icon
1922
import androidx.compose.material.IconButton
23+
import androidx.compose.material.MaterialTheme
2024
import androidx.compose.material.Text
2125
import androidx.compose.runtime.Composable
2226
import androidx.compose.ui.Alignment
@@ -30,24 +34,39 @@ import androidx.compose.ui.unit.dp
3034
import androidx.compose.ui.unit.sp
3135
import coil.compose.AsyncImage
3236
import com.datadog.android.sample.R
37+
3338
private const val SMALL_IMAGE_URL = "https://picsum.photos/100/100"
3439

3540
@Composable
3641
internal fun ImageSample() {
37-
LazyColumn(modifier = Modifier.fillMaxSize()) {
38-
item {
39-
LocalImageNoBackground()
40-
}
42+
Column {
43+
Text(
44+
"Content Scale Section",
45+
style = MaterialTheme.typography.h5,
46+
modifier = Modifier.padding(8.dp)
47+
)
48+
ImageScaling()
49+
Spacer(modifier = Modifier.height(16.dp))
50+
Text(
51+
"Image Loading Section",
52+
style = MaterialTheme.typography.h5,
53+
modifier = Modifier.padding(8.dp)
54+
)
55+
LazyColumn(modifier = Modifier.fillMaxSize()) {
56+
item {
57+
LocalImageNoBackground()
58+
}
4159

42-
item {
43-
LocalIconDoubleBackground()
44-
}
60+
item {
61+
LocalIconDoubleBackground()
62+
}
4563

46-
item {
47-
IconButtonSingleBackground()
48-
}
49-
item {
50-
CoilImage()
64+
item {
65+
IconButtonSingleBackground()
66+
}
67+
item {
68+
CoilImage()
69+
}
5170
}
5271
}
5372
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.android.sample.compose
8+
9+
import androidx.compose.foundation.Image
10+
import androidx.compose.foundation.background
11+
import androidx.compose.foundation.layout.Column
12+
import androidx.compose.foundation.layout.padding
13+
import androidx.compose.foundation.layout.size
14+
import androidx.compose.foundation.layout.wrapContentSize
15+
import androidx.compose.foundation.lazy.grid.GridCells
16+
import androidx.compose.foundation.lazy.grid.LazyGridScope
17+
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
18+
import androidx.compose.material.Text
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.ui.Alignment
21+
import androidx.compose.ui.Modifier
22+
import androidx.compose.ui.graphics.Color
23+
import androidx.compose.ui.layout.ContentScale
24+
import androidx.compose.ui.res.painterResource
25+
import androidx.compose.ui.tooling.preview.Preview
26+
import androidx.compose.ui.unit.dp
27+
import androidx.compose.ui.unit.sp
28+
import com.datadog.android.sample.R
29+
30+
@Composable
31+
@Suppress("MagicNumber")
32+
internal fun ImageScaling() {
33+
LazyVerticalGrid(
34+
modifier = Modifier.wrapContentSize(),
35+
columns = GridCells.Fixed(4),
36+
userScrollEnabled = false
37+
) {
38+
imageScalingItem(ContentScale.Fit, "Fit")
39+
imageScalingItem(ContentScale.Crop, "Crop")
40+
imageScalingItem(ContentScale.Inside, "Inside")
41+
imageScalingItem(ContentScale.FillWidth, "FillWidth")
42+
imageScalingItem(ContentScale.FillHeight, "FillHeight")
43+
imageScalingItem(ContentScale.FillBounds, " FillBounds")
44+
imageScalingItem(ContentScale.None, "None")
45+
}
46+
}
47+
48+
private fun LazyGridScope.imageScalingItem(
49+
contentScale: ContentScale,
50+
text: String
51+
) {
52+
item {
53+
Column(
54+
modifier = Modifier.padding(8.dp).wrapContentSize(),
55+
horizontalAlignment = Alignment.CenterHorizontally
56+
) {
57+
Image(
58+
modifier = Modifier.background(color = Color.Green).size(72.dp),
59+
painter = painterResource(R.drawable.example_appwidget_preview),
60+
contentDescription = "image",
61+
contentScale = contentScale
62+
)
63+
Text(text = text, fontSize = 12.sp)
64+
}
65+
}
66+
}
67+
68+
@Composable
69+
@Preview(showBackground = true)
70+
internal fun PreviewImageScaling() {
71+
ImageScaling()
72+
}

0 commit comments

Comments
 (0)