|
| 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