mirror of
https://github.com/Steffo99/twom.git
synced 2024-11-21 23:54:26 +00:00
Improve avatar image cropping
This commit is contained in:
parent
63bf438b1b
commit
22b06ab30f
3 changed files with 15 additions and 14 deletions
|
@ -15,10 +15,10 @@ import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import eu.steffo.twom.matrix.avatar.AvatarFromBitmap
|
import eu.steffo.twom.matrix.avatar.AvatarFromImageBitmap
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@Preview
|
@Preview(widthDp = 40, heightDp = 40)
|
||||||
fun AvatarSelector(
|
fun AvatarSelector(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
onSelectAvatar: (bitmap: Bitmap) -> Unit = {},
|
onSelectAvatar: (bitmap: Bitmap) -> Unit = {},
|
||||||
|
@ -47,7 +47,7 @@ fun AvatarSelector(
|
||||||
launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
AvatarFromBitmap(
|
AvatarFromImageBitmap(
|
||||||
bitmap = selection?.asImageBitmap(),
|
bitmap = selection?.asImageBitmap(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,15 @@ package eu.steffo.twom.matrix.avatar
|
||||||
|
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.ImageBitmap
|
import androidx.compose.ui.graphics.ImageBitmap
|
||||||
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
@Preview(widthDp = 40, heightDp = 40)
|
@Preview(widthDp = 40, heightDp = 40)
|
||||||
fun AvatarFromBitmap(
|
fun AvatarFromImageBitmap(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
bitmap: ImageBitmap? = null,
|
bitmap: ImageBitmap? = null,
|
||||||
fallbackText: String = "?",
|
fallbackText: String = "?",
|
||||||
|
@ -25,6 +27,8 @@ fun AvatarFromBitmap(
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
bitmap = bitmap,
|
bitmap = bitmap,
|
||||||
contentDescription = contentDescription,
|
contentDescription = contentDescription,
|
||||||
|
contentScale = ContentScale.Crop,
|
||||||
|
alignment = Alignment.Center,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,16 +1,15 @@
|
||||||
package eu.steffo.twom.matrix.avatar
|
package eu.steffo.twom.matrix.avatar
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.compose.foundation.Image
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.ImageBitmap
|
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import eu.steffo.twom.matrix.LocalSession
|
import eu.steffo.twom.matrix.LocalSession
|
||||||
|
@ -24,7 +23,7 @@ fun AvatarFromURL(
|
||||||
contentDescription: String = "",
|
contentDescription: String = "",
|
||||||
) {
|
) {
|
||||||
val session = LocalSession.current
|
val session = LocalSession.current
|
||||||
var avatar by remember { mutableStateOf<ImageBitmap?>(null) }
|
var bitmap by rememberSaveable { mutableStateOf<Bitmap?>(null) }
|
||||||
|
|
||||||
LaunchedEffect(session, url) GetAvatar@{
|
LaunchedEffect(session, url) GetAvatar@{
|
||||||
if (session == null) {
|
if (session == null) {
|
||||||
|
@ -48,21 +47,19 @@ fun AvatarFromURL(
|
||||||
)
|
)
|
||||||
// TODO: Should I check the MIME type? And the size of the image?
|
// TODO: Should I check the MIME type? And the size of the image?
|
||||||
Log.d("Avatar", "File for $url is: $avatarFile")
|
Log.d("Avatar", "File for $url is: $avatarFile")
|
||||||
val avatarBitmap = BitmapFactory.decodeFile(avatarFile.absolutePath)
|
bitmap = BitmapFactory.decodeFile(avatarFile.absolutePath)
|
||||||
Log.d("Avatar", "Bitmap for $url is: $avatarBitmap")
|
|
||||||
avatar = avatarBitmap.asImageBitmap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (session == null || url == null || avatar == null) {
|
if (session == null || url == null || bitmap == null) {
|
||||||
AvatarFromDefault(
|
AvatarFromDefault(
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
fallbackText = fallbackText,
|
fallbackText = fallbackText,
|
||||||
contentDescription = contentDescription
|
contentDescription = contentDescription
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
Image(
|
AvatarFromImageBitmap(
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
bitmap = avatar!!,
|
bitmap = bitmap!!.asImageBitmap(),
|
||||||
contentDescription = contentDescription,
|
contentDescription = contentDescription,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue