Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android update underlying bitmap
package com.feresr.composebitmap
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.lifecycle.lifecycleScope
import com.feresr.composebitmap.ui.theme.ComposeBitmapTheme
import kotlinx.coroutines.delay
class MainActivity : ComponentActivity() {
private val bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launchWhenCreated {
for (y in 0 until bitmap.width) {
for (x in 0 until bitmap.height) {
bitmap.setPixel(x, y, Color.RED)
}
delay(10)
}
}
setContent {
ComposeBitmapTheme {
Surface(color = MaterialTheme.colors.background) {
Image(
bitmap = bitmap.asImageBitmap(),
modifier = Modifier.wrapContentSize(),
contentDescription = ""
)
Button(onClick = { /*no op*/ }) {
// click me to "wake up" the animation
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment