Skip to content

Instantly share code, notes, and snippets.

@ademkocamaz
Forked from tkuenneth/MainActivity.kt
Created June 17, 2024 14:39
Show Gist options
  • Save ademkocamaz/06031593c964dc027ff34853eca5f839 to your computer and use it in GitHub Desktop.
Save ademkocamaz/06031593c964dc027ff34853eca5f839 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")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment