Skip to content

Instantly share code, notes, and snippets.

@feresr
Created June 1, 2021 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feresr/4ba8ff1338fe71325bd6fc8bb2290534 to your computer and use it in GitHub Desktop.
Save feresr/4ba8ff1338fe71325bd6fc8bb2290534 to your computer and use it in GitHub Desktop.
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