This commit is contained in:
weber
2025-08-25 18:35:06 +08:00
parent 77033854f0
commit df75c710e5
20 changed files with 353 additions and 181 deletions

View File

@@ -59,16 +59,15 @@ fun AgentCard(
Row(
modifier = Modifier
) {
// 使用remember基于agentEntity.id来缓存图片避免滑动时重复加载
Box(
modifier = Modifier
.size(40.dp)
.clip(RoundedCornerShape(40.dp))
) {
CustomAsyncImage(
LocalContext.current,
context,
agentEntity.avatar,
contentDescription = "",
contentDescription = agentEntity.openId,
modifier = Modifier.size(40.dp),
contentScale = ContentScale.Crop
)

View File

@@ -23,6 +23,28 @@ import com.aiosman.ravenow.utils.Utils.getImageLoader
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/**
* CustomAsyncImage 组件使用说明:
*
* @param context 上下文,可选
* @param imageUrl 图片URL或Bitmap对象
* @param contentDescription 图片描述
* @param modifier 修饰符
* @param blurHash 模糊哈希值(暂未使用)
* @param placeholderRes 加载时显示的占位符图片资源ID
* @param errorRes 加载失败时显示的错误图片资源ID
* @param defaultRes 当imageUrl为空或null时显示的默认图片资源ID优先级最高
* @param contentScale 图片缩放模式
*
* 使用示例:
* CustomAsyncImage(
* imageUrl = "https://example.com/image.jpg",
* contentDescription = "用户头像",
* defaultRes = R.mipmap.default_avatar,
* placeholderRes = R.mipmap.loading_placeholder,
* errorRes = R.mipmap.error_image
* )
*/
@Composable
fun rememberImageBitmap(imageUrl: String, imageLoader: ImageLoader): Bitmap? {
val context = LocalContext.current
@@ -53,6 +75,10 @@ fun CustomAsyncImage(
blurHash: String? = null,
@DrawableRes
placeholderRes: Int? = null,
@DrawableRes
errorRes: Int? = null,
@DrawableRes
defaultRes: Int? = null,
contentScale: ContentScale = ContentScale.Crop
) {
val localContext = LocalContext.current
@@ -62,10 +88,11 @@ fun CustomAsyncImage(
// 处理 imageUrl 为 null 或空字符串的情况
if (imageUrl == null || imageUrl == "") {
// 如果 imageUrl 为 null 且有占位符,则直接显示占位符
if (placeholderRes != null) {
// 优先使用 defaultRes然后是 placeholderRes
val fallbackRes = defaultRes ?: placeholderRes
if (fallbackRes != null) {
Image(
painter = androidx.compose.ui.res.painterResource(placeholderRes),
painter = androidx.compose.ui.res.painterResource(fallbackRes),
contentDescription = contentDescription,
modifier = modifier,
contentScale = contentScale
@@ -97,6 +124,10 @@ fun CustomAsyncImage(
if (placeholderRes != null) {
placeholder(placeholderRes)
}
// 设置错误时显示的图片
if (errorRes != null) {
error(errorRes)
}
}
.build(),
contentDescription = contentDescription,
@@ -104,4 +135,38 @@ fun CustomAsyncImage(
contentScale = contentScale,
imageLoader = imageLoader
)
}
}
/*
使用示例:
1. 基本使用(带默认图片):
CustomAsyncImage(
imageUrl = user.avatar,
contentDescription = "用户头像",
defaultRes = R.mipmap.default_avatar
)
2. 完整配置:
CustomAsyncImage(
imageUrl = "https://example.com/image.jpg",
contentDescription = "产品图片",
defaultRes = R.mipmap.default_product,
placeholderRes = R.mipmap.loading_placeholder,
errorRes = R.mipmap.error_image,
contentScale = ContentScale.Crop
)
3. 处理空URL
CustomAsyncImage(
imageUrl = "", // 空字符串会显示默认图片
contentDescription = "头像",
defaultRes = R.mipmap.default_avatar
)
4. 处理Bitmap
CustomAsyncImage(
imageUrl = bitmapObject, // Bitmap对象会直接显示
contentDescription = "裁剪后的图片"
)
*/