Skip to content

Instantly share code, notes, and snippets.

@tkuenneth
Created May 29, 2021 10:07
Show Gist options
  • Save tkuenneth/ddf598663f041dc79960cda503d14448 to your computer and use it in GitHub Desktop.
Save tkuenneth/ddf598663f041dc79960cda503d14448 to your computer and use it in GitHub Desktop.
How to display an adaptive icon in Jetpack Compose
package com.thomaskuenneth.sandbox
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.content.res.ResourcesCompat
import com.thomaskuenneth.sandbox.ui.theme.SandboxTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SandboxTheme {
Surface(color = MaterialTheme.colors.background) {
Sandbox()
}
}
}
}
}
@Composable
fun Sandbox() {
ResourcesCompat.getDrawable(
LocalContext.current.resources,
R.mipmap.ic_launcher, LocalContext.current.theme
)?.let { drawable ->
val bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth, drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
Column(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
// painter = painterResource(R.mipmap.ic_launcher),
bitmap = bitmap.asImageBitmap(),
"An image",
modifier = Modifier.requiredSize(96.dp)
)
Text("Hello Image")
}
}
}
@ALizarazoTellez
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment