Roar Gronmo
11/29/2019, 12:57 PMContainer(
width = 40.dp,
height = 40.dp
) {
DrawImage(image)
}
RGContainer
and resize the inner one by calculating the aspect ratio of the image.
Column()
{
var imageWidth: Int = image.width;
var imageHeight: Int = image.height;
var imageAspect: Double = image.width.toDouble()/image.height.toDouble()
var justWidth: Double = 0.0
var justHeight: Double = 0.0
FlexRow {
inflexible{
Padding(padding = 4.dp){
Container(
width = 40.dp,
height = 40.dp) {
if(imageAspect>1.0) {
justWidth = 40.0
justHeight = 40/imageAspect
} else {
justWidth = 40*imageAspect
justHeight = 40.0
}
Container(
width = justWidth.dp,
height = justHeight.dp
) {
DrawImage(image)
}
}
}
}
inflexible {
Column {
Text(text = "w=${imageWidth} h=${imageHeight}" +
" aspect=${imageAspect}")
Text(text = "jw=${justWidth} jh=${justHeight}")
}
}
}
}
RGAndrey Kulikov
11/29/2019, 5:03 PMRoar Gronmo
11/29/2019, 5:28 PM@Composable
function version, where the containing size of the image can be of any aspect ratio, and the image fits in center of that aspect ratio.
@Composable
fun DrawImageFitCenter(
image: Image,
tint: Color? = null,
containerWidthDp: Int,
containerHeightDp: Int
){
val imageWidth: Int = image.width
val imageHeight: Int = image.height
val imageAspect: Double = imageWidth.toDouble()/imageHeight.toDouble()
val containerAspect: Double = containerWidthDp.toDouble()/containerHeightDp.toDouble()
var justWidth: Double = 0.0
var justHeight: Double = 0.0
Container(
width = containerWidthDp.dp,
height = containerHeightDp.dp
) {
if(containerAspect>imageAspect)
{
justWidth = containerWidthDp.toDouble()*(imageAspect/containerAspect)
justHeight = containerHeightDp.toDouble()
} else {
justWidth = containerWidthDp.toDouble()
justHeight = containerHeightDp.toDouble()*(containerAspect/imageAspect)
}
Container(
width = justWidth.dp,
height = justHeight.dp
) {
DrawImage(
image = image,
tint = tint )
}
}
}
RGDp
to Int
?Zach Klippenstein (he/him) [MOD]
12/02/2019, 3:57 PMDp
to pixels by using withDensity
Roar Gronmo
12/02/2019, 4:04 PMZach Klippenstein (he/him) [MOD]
12/02/2019, 4:15 PMDp
kdoc, it already supports all the arithmetic operators directly so I don't think you need to do any conversions.
https://developer.android.com/reference/kotlin/androidx/ui/core/Dp.htmlRoar Gronmo
12/02/2019, 4:25 PMZach Klippenstein (he/him) [MOD]
12/02/2019, 4:26 PMRoar Gronmo
12/02/2019, 8:55 PMZach Klippenstein (he/him) [MOD]
12/03/2019, 3:34 PMimageWidth.div(imageHeight)
directly, do imageWidth / imageHeight
. More info here: https://kotlinlang.org/docs/reference/operator-overloading.htmlRoar Gronmo
12/12/2019, 11:57 AM@Composable
fun DrawImageFitCenter(
modifier: Modifier = Modifier.None,
image: Image,
tint: Color? = null,
containerWidth: Dp,
containerHeight: Dp
){
val imageAspect = image.width.dp / image.height.dp
val containerAspect = containerWidth / containerHeight
var justWidth: Dp
var justHeight: Dp
Container(
modifier = modifier,
width = containerWidth,
height = containerHeight
) {
if(containerAspect>imageAspect)
{
justWidth = containerWidth*(imageAspect/containerAspect)
justHeight = containerHeight
} else {
justWidth = containerWidth
justHeight = containerHeight*(containerAspect/imageAspect)
}
Container(width = justWidth, height = justHeight) {
DrawImage(image = image,
tint = tint)
}
}
}