Skip to content

Instantly share code, notes, and snippets.

Canvas(
modifier = Modifier
.height(40.dp)
.width(300.dp)
.clip(RoundedCornerShape(50))
) {
...
}
val bitmap = Bitmap.createBitmap(size.width.toInt(), size.height.toInt(), Bitmap.Config.ARGB_8888)
val hueCanvas = Canvas(bitmap)
val huePanel = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat())
val hueColors = IntArray((huePanel.width()).toInt())
var hue = 0f
for (i in hueColors.indices) {
hueColors[i] = AndroidColor.HSVToColor(floatArrayOf(hue, 1f, 1f))
hue += 360f / hueColors.size
}
val linePaint = Paint()
linePaint.strokeWidth = 0F
for (i in hueColors.indices) {
linePaint.color = hueColors[i]
hueCanvas.drawLine(i.toFloat(), 0F, i.toFloat(), huePanel.bottom, linePaint)
}
drawIntoCanvas {
it.nativeCanvas.drawBitmap(
bitmap,
null,
panel.toRect(),
null
)
}
val pressOffset = remember {
mutableStateOf(Offset.Zero)
}
Canvas(
modifier = Modifier
.height(40.dp)
.width(300.dp)
.clip(RoundedCornerShape(50))
) {
.....
@V-Abhilash-1999
V-Abhilash-1999 / MainActivity.kt
Created May 23, 2023 12:24
Emitting Click and Drag Actions in interaction source
private fun Modifier.emitDragGesture(
interactionSource: MutableInteractionSource
): Modifier = composed {
val scope = rememberCoroutineScope()
pointerInput(Unit) {
detectDragGestures { input, _ ->
scope.launch {
interactionSource.emit(PressInteraction.Press(input.position))
}
}
scope.launch {
interactionSource.interactions.collect { interaction ->
when(interaction) {
PressInteraction.Press -> {
val pressPos = pressPosition.x.coerceIn(0f..drawScopeSize.width)
pressOffset.value = Offset(pressPos, 0f)
val selectedHue = pointToHue(pressPos)
}
}
}
@Composable
fun HueBar(
setColor: (Float) -> Unit
) {
val scope = rememberCoroutineScope()
val interactionSource = remember {
MutableInteractionSource()
}
val pressOffset = remember {
mutableStateOf(Offset.Zero)
val interactionSource = remember {
MutableInteractionSource()
}
val scope = rememberCoroutineScope()
var sat: Float
var value: Float
val pressOffset = remember {
mutableStateOf(Offset.Zero)
}
Canvas(