1
Fork 0
mirror of https://github.com/Steffo99/twom.git synced 2024-11-21 15:44:26 +00:00

Improve avatar image cropping

This commit is contained in:
Steffo 2023-12-07 01:48:10 +01:00
parent 63bf438b1b
commit 22b06ab30f
Signed by: steffo
GPG key ID: 2A24051445686895
3 changed files with 15 additions and 14 deletions

View file

@ -15,10 +15,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.matrix.avatar.AvatarFromBitmap
import eu.steffo.twom.matrix.avatar.AvatarFromImageBitmap
@Composable
@Preview
@Preview(widthDp = 40, heightDp = 40)
fun AvatarSelector(
modifier: Modifier = Modifier,
onSelectAvatar: (bitmap: Bitmap) -> Unit = {},
@ -47,7 +47,7 @@ fun AvatarSelector(
launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
}
) {
AvatarFromBitmap(
AvatarFromImageBitmap(
bitmap = selection?.asImageBitmap(),
)
}

View file

@ -2,13 +2,15 @@ package eu.steffo.twom.matrix.avatar
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.tooling.preview.Preview
@Composable
@Preview(widthDp = 40, heightDp = 40)
fun AvatarFromBitmap(
fun AvatarFromImageBitmap(
modifier: Modifier = Modifier,
bitmap: ImageBitmap? = null,
fallbackText: String = "?",
@ -25,6 +27,8 @@ fun AvatarFromBitmap(
modifier = modifier,
bitmap = bitmap,
contentDescription = contentDescription,
contentScale = ContentScale.Crop,
alignment = Alignment.Center,
)
}
}

View file

@ -1,16 +1,15 @@
package eu.steffo.twom.matrix.avatar
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.tooling.preview.Preview
import eu.steffo.twom.matrix.LocalSession
@ -24,7 +23,7 @@ fun AvatarFromURL(
contentDescription: String = "",
) {
val session = LocalSession.current
var avatar by remember { mutableStateOf<ImageBitmap?>(null) }
var bitmap by rememberSaveable { mutableStateOf<Bitmap?>(null) }
LaunchedEffect(session, url) GetAvatar@{
if (session == null) {
@ -48,21 +47,19 @@ fun AvatarFromURL(
)
// TODO: Should I check the MIME type? And the size of the image?
Log.d("Avatar", "File for $url is: $avatarFile")
val avatarBitmap = BitmapFactory.decodeFile(avatarFile.absolutePath)
Log.d("Avatar", "Bitmap for $url is: $avatarBitmap")
avatar = avatarBitmap.asImageBitmap()
bitmap = BitmapFactory.decodeFile(avatarFile.absolutePath)
}
if (session == null || url == null || avatar == null) {
if (session == null || url == null || bitmap == null) {
AvatarFromDefault(
modifier = modifier,
fallbackText = fallbackText,
contentDescription = contentDescription
)
} else {
Image(
AvatarFromImageBitmap(
modifier = modifier,
bitmap = avatar!!,
bitmap = bitmap!!.asImageBitmap(),
contentDescription = contentDescription,
)
}