mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
89 lines
2.3 KiB
Kotlin
89 lines
2.3 KiB
Kotlin
package com.geeksville.mesh.ui
|
|
|
|
import android.graphics.Bitmap
|
|
import androidx.compose.Composable
|
|
import androidx.ui.core.DensityAmbient
|
|
import androidx.ui.core.DrawModifier
|
|
import androidx.ui.core.Modifier
|
|
import androidx.ui.core.toModifier
|
|
import androidx.ui.foundation.Box
|
|
import androidx.ui.graphics.*
|
|
import androidx.ui.graphics.colorspace.ColorSpace
|
|
import androidx.ui.graphics.colorspace.ColorSpaces
|
|
import androidx.ui.graphics.painter.ImagePainter
|
|
import androidx.ui.unit.Density
|
|
import androidx.ui.unit.PxSize
|
|
import androidx.ui.unit.toRect
|
|
|
|
/// Stolen from the Compose SimpleImage, replace with their real Image component someday
|
|
// TODO(mount, malkov) : remove when RepaintBoundary is a modifier: b/149982905
|
|
// This is class and not val because if b/149985596
|
|
private object ClipModifier : DrawModifier {
|
|
override fun draw(density: Density, drawContent: () -> Unit, canvas: Canvas, size: PxSize) {
|
|
canvas.save()
|
|
canvas.clipRect(size.toRect())
|
|
drawContent()
|
|
canvas.restore()
|
|
}
|
|
}
|
|
|
|
|
|
/// Stolen from the Compose SimpleImage, replace with their real Image component someday
|
|
@Composable
|
|
fun ScaledImage(
|
|
image: Image,
|
|
modifier: Modifier = Modifier.None,
|
|
tint: Color? = null
|
|
) {
|
|
with(DensityAmbient.current) {
|
|
val imageModifier = ImagePainter(image).toModifier(
|
|
scaleFit = ScaleFit.FillMaxDimension,
|
|
colorFilter = tint?.let { ColorFilter(it, BlendMode.srcIn) }
|
|
)
|
|
Box(modifier + ClipModifier + imageModifier)
|
|
}
|
|
}
|
|
|
|
|
|
/// Borrowed from Compose
|
|
class AndroidImage(val bitmap: Bitmap) : Image {
|
|
|
|
/**
|
|
* @see Image.width
|
|
*/
|
|
override val width: Int
|
|
get() = bitmap.width
|
|
|
|
/**
|
|
* @see Image.height
|
|
*/
|
|
override val height: Int
|
|
get() = bitmap.height
|
|
|
|
override val config: ImageConfig get() = ImageConfig.Argb8888
|
|
|
|
/**
|
|
* @see Image.colorSpace
|
|
*/
|
|
override val colorSpace: ColorSpace
|
|
get() = ColorSpaces.Srgb
|
|
|
|
/**
|
|
* @see Image.hasAlpha
|
|
*/
|
|
override val hasAlpha: Boolean
|
|
get() = bitmap.hasAlpha()
|
|
|
|
/**
|
|
* @see Image.nativeImage
|
|
*/
|
|
override val nativeImage: NativeImage
|
|
get() = bitmap
|
|
|
|
/**
|
|
* @see
|
|
*/
|
|
override fun prepareToDraw() {
|
|
bitmap.prepareToDraw()
|
|
}
|
|
}
|