Created
June 1, 2021 18:10
-
-
Save feresr/4ba8ff1338fe71325bd6fc8bb2290534 to your computer and use it in GitHub Desktop.
Android update underlying bitmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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