Skip to content

Instantly share code, notes, and snippets.

@yschimke
Created March 17, 2024 14:23
Show Gist options
  • Save yschimke/2cbbf4e1fe2354beb10ce2e83216f7e3 to your computer and use it in GitHub Desktop.
Save yschimke/2cbbf4e1fe2354beb10ce2e83216f7e3 to your computer and use it in GitHub Desktop.
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.horologist.scratch
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CameraAlt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.drawscope.draw
import androidx.compose.ui.graphics.layer.GraphicsLayer
import androidx.compose.ui.graphics.rememberGraphicsLayer
import androidx.compose.ui.text.drawText
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.toIntSize
import androidx.compose.ui.unit.toSize
import androidx.wear.compose.material.MaterialTheme
import androidx.wear.compose.material.Text
import com.google.android.horologist.compose.material.Button
import java.time.LocalTime
class ScratchActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestApp()
}
}
}
@Stable
data class SavedPicture(
val i: Int = 0,
val picture: ImageBitmap? = null
)
@Composable
fun TestApp() {
var lastPicture by remember { mutableStateOf(SavedPicture()) }
val pictureTrigger = remember { mutableIntStateOf(0) }
val finalSize = DpSize(227.dp, 227.dp)
SavedPicture(
size = finalSize,
onPicture = { lastPicture = SavedPicture(lastPicture.i + 1, it) },
pictureTrigger = pictureTrigger,
) {
Box(
modifier = Modifier
.size(finalSize)
.background(Color.DarkGray),
contentAlignment = Alignment.Center
) {
Text("Snapped $finalSize\n" + LocalTime.now().withNano(0))
}
}
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background)
) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(lastPicture.i.toString())
Canvas(
modifier = Modifier
.size(100.dp)
.border(1.dp, Color.White),
) {
val picture = lastPicture.picture
if (picture != null) {
drawImage(image = picture, dstSize = this.size.toIntSize())
}
}
Button(
imageVector = Icons.Default.CameraAlt,
contentDescription = "Snap",
onClick = { pictureTrigger.intValue += 1 })
}
}
}
@Composable
fun SavedPicture(
modifier: Modifier = Modifier,
onPicture: (ImageBitmap) -> Unit,
size: DpSize,
pictureTrigger: MutableIntState,
content: @Composable () -> Unit
) {
Box(modifier = modifier.size(size)) {
val layer = rememberGraphicsLayer()
Box(
modifier = Modifier
.fillMaxSize()
.drawIntoBitmap(layer, size, pictureTrigger, onPicture)
) {
key(pictureTrigger.intValue) {
content()
}
}
}
}
fun Modifier.drawIntoBitmap(
layer: GraphicsLayer,
pictureSize: DpSize,
pictureTrigger: MutableIntState,
onPicture: (ImageBitmap) -> Unit
): Modifier = this.drawWithCache {
var lastPicture = 0
onDrawWithContent {
if (pictureTrigger.intValue != lastPicture) {
lastPicture = pictureTrigger.intValue
val pictureDensity = Density(this.density)
val captureSize = with(pictureDensity) {
pictureSize.toSize().toIntSize()
}
val bitmap = ImageBitmap(captureSize.width, captureSize.height)
val canvas = Canvas(bitmap)
draw(
density = pictureDensity,
layoutDirection = LayoutDirection.Ltr,
canvas = canvas,
size = captureSize.toSize(),
graphicsLayer = layer
) {
this@onDrawWithContent.drawContent()
}
onPicture(bitmap)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment