Nat Strangerweather
04/06/2021, 2:56 PMZach Klippenstein (he/him) [MOD]
04/06/2021, 3:11 PMNat Strangerweather
04/06/2021, 3:13 PMZach Klippenstein (he/him) [MOD]
04/06/2021, 3:14 PMComposeView to draw into a canvasNat Strangerweather
04/06/2021, 3:20 PMAndroidView . When I move around the objects on the canvas, I want to save the bitmap. It works from the ordinary Kotlin class. Maybe I should consider using ComposeView in my ordinary Kotlin class? Sorry if I don't make much sense. I am still a bit confused with Compose Interop.Nat Strangerweather
04/06/2021, 3:21 PMromainguy
04/06/2021, 3:36 PMCanvas as “the thing you save”romainguy
04/06/2021, 3:36 PMCanvas really is is an interface to draw primitives onto a backing bufferromainguy
04/06/2021, 3:37 PMSurface)romainguy
04/06/2021, 3:38 PMCanvas only records commands when you draw to the window, etc.)romainguy
04/06/2021, 3:38 PMCanvas . What you do is ask a View/Composable/whatever to issue Canvas commands that are target to a different backing buffer: a BitmapNat Strangerweather
04/06/2021, 3:43 PMNat Strangerweather
04/06/2021, 3:46 PMsaveBitmapToStorage(touchEventsClass.mutableBitmap, context)Zach Klippenstein (he/him) [MOD]
04/06/2021, 3:46 PMsaveBitmapToStorage?Nat Strangerweather
04/06/2021, 3:47 PMNat Strangerweather
04/06/2021, 3:47 PMNat Strangerweather
04/06/2021, 3:48 PMfun saveBitmapToStorage(bitmap: Bitmap, context: Context) {
val filename = "${System.currentTimeMillis()}.jpg"
var fos: OutputStream? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
context.contentResolver?.also { resolver ->
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, filename)
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg")
put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES)
}
val imageUri: Uri? = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
fos = imageUri?.let { resolver.openOutputStream(it) }
}
} else {
val imagesDir = Environment.DIRECTORY_PICTURES
val image = File(imagesDir, filename)
fos = FileOutputStream(image)
}
fos?.use {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it)
Toast.makeText(context, "Saved to Pictures!", Toast.LENGTH_SHORT).show()
}
fos?.close()
}Zach Klippenstein (he/him) [MOD]
04/06/2021, 3:50 PMtouchEventsClass.mutableBitmap come from?Nat Strangerweather
04/06/2021, 3:52 PMprivate val bitmap: Bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888)
val mutableBitmap: Bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
private val bitmapCanvas = Canvas(mutableBitmap)
then in my onDraw I have:
override fun onDraw(canvas: Canvas?) {
canvas?.apply {
if (collision) {
bitmapCanvas.drawColor(Color.RED)
} else {
bitmapCanvas.drawColor(Color.GREEN)
}
bitmapCanvas.drawRect(rect1, paint)
bitmapCanvas.drawRect(rect2, paint)
}
canvas?.drawBitmap(mutableBitmap, 0f, 0f, paint)
}Nat Strangerweather
04/06/2021, 3:53 PMNat Strangerweather
04/06/2021, 3:54 PMval touchEventsClass = TouchEvents(context)romainguy
04/06/2021, 3:54 PMcopy the Bitmap you just created?Nat Strangerweather
04/06/2021, 3:55 PMZach Klippenstein (he/him) [MOD]
04/06/2021, 3:55 PMAndroidView that’s sitting inside your composition to draw itself. The reason the composable isn’t showing up is because you’re not actually asking compose to draw anything into the canvas.
What might work is to ask the ComposeView that hosts your composition to draw itself into the Canvas, but of course then you will get the whole composition and not just the button composable.romainguy
04/06/2021, 3:55 PMromainguy
04/06/2021, 3:56 PMonDraw method is also unnecessarily complex and goes through the software rendererromainguy
04/06/2021, 3:56 PMCanvas you receiveromainguy
04/06/2021, 3:57 PMonDraw again but with the Canvas that wraps your BitmapNat Strangerweather
04/06/2021, 3:57 PMromainguy
04/06/2021, 3:58 PMNat Strangerweather
04/06/2021, 3:58 PMromainguy
04/06/2021, 4:00 PMonDraw method hasn’t been executed yet at the point you save the bitmap maybe?Nat Strangerweather
04/06/2021, 4:01 PMZach Klippenstein (he/him) [MOD]
04/06/2021, 4:05 PM1. Activity
2. ComposeView
3. AndroidView
4. Button composable
Then if you’re only asking (3) to draw itself, only it will draw itself. The composition won’t draw anything. If you want to draw (4) into the canvas, you need to ask the compose view (2) to draw.Nat Strangerweather
04/06/2021, 4:09 PM