diff --git a/banner/src/main/java/com/zj/banner/ui/BannerCard.kt b/banner/src/main/java/com/zj/banner/ui/BannerCard.kt index 2f4687f..7cad5e2 100644 --- a/banner/src/main/java/com/zj/banner/ui/BannerCard.kt +++ b/banner/src/main/java/com/zj/banner/ui/BannerCard.kt @@ -1,23 +1,15 @@ package com.zj.banner.ui -import android.graphics.BitmapFactory -import android.util.Log -import androidx.compose.foundation.Image import androidx.compose.foundation.clickable import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.Card import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Shape -import androidx.compose.ui.graphics.asImageBitmap -import androidx.compose.ui.graphics.painter.BitmapPainter import androidx.compose.ui.layout.ContentScale -import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp -import com.google.accompanist.coil.rememberCoilPainter import com.zj.banner.model.BaseBannerBean - -private const val TAG = "BannerCard" +import com.zj.banner.utils.ImageLoader /** * Banner 图片展示卡片 @@ -45,36 +37,6 @@ fun BannerCard( modifier = modifier ) { val imgModifier = Modifier.clickable(onClick = onBannerClick) - when (bean.data) { - is String -> { - val img = bean.data as String - val painter = if (img.contains("https://") || img.contains("http://")) { - Log.d(TAG, "PostCardPopular: 加载网络图片") - rememberCoilPainter(img) - } else { - Log.d(TAG, "PostCardPopular: 加载本地图片") - val bitmap = BitmapFactory.decodeFile(img) - BitmapPainter(bitmap.asImageBitmap()) - } - Image( - modifier = imgModifier, - painter = painter, - contentDescription = "", - contentScale = contentScale - ) - } - is Int -> { - Log.d(TAG, "PostCardPopular: 加载本地资源图片") - Image( - modifier = imgModifier, - painter = painterResource(bean.data as Int), - contentDescription = "", - contentScale = contentScale - ) - } - else -> { - throw IllegalArgumentException("参数类型不符合要求,只能是:url、文件路径或者是 drawable id") - } - } + ImageLoader(bean.data, imgModifier, contentScale) } } diff --git a/banner/src/main/java/com/zj/banner/utils/ImageLoader.kt b/banner/src/main/java/com/zj/banner/utils/ImageLoader.kt new file mode 100644 index 0000000..1a8ae1e --- /dev/null +++ b/banner/src/main/java/com/zj/banner/utils/ImageLoader.kt @@ -0,0 +1,59 @@ +package com.zj.banner.utils + +import android.graphics.BitmapFactory +import android.util.Log +import androidx.compose.foundation.Image +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.asImageBitmap +import androidx.compose.ui.graphics.painter.BitmapPainter +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.res.painterResource +import com.google.accompanist.coil.rememberCoilPainter + +private const val TAG = "ImageLoader" + +/** + * 图片加载工具 + * + * @param data 图片数据 可以为图片网址、本地地址或资源id + * @param modifier 修饰符 + * @param contentScale 使用可选的scale参数来确定要使用的纵横比缩放 + */ +@Composable +fun ImageLoader( + data: Any?, + modifier: Modifier = Modifier, + contentScale: ContentScale = ContentScale.Crop, +) { + when (data) { + is String -> { + val painter = if (data.contains("https://") || data.contains("http://")) { + Log.d(TAG, "PostCardPopular: 加载网络图片") + rememberCoilPainter(data) + } else { + Log.d(TAG, "PostCardPopular: 加载本地图片") + val bitmap = BitmapFactory.decodeFile(data) + BitmapPainter(bitmap.asImageBitmap()) + } + Image( + modifier = modifier, + painter = painter, + contentDescription = "", + contentScale = contentScale + ) + } + is Int -> { + Log.d(TAG, "PostCardPopular: 加载本地资源图片") + Image( + modifier = modifier, + painter = painterResource(data), + contentDescription = "", + contentScale = contentScale + ) + } + else -> { + throw IllegalArgumentException("参数类型不符合要求,只能是:url、文件路径或者是 drawable id") + } + } +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index db5c911..a19be6c 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { maven { url 'https://jitpack.io' } } dependencies { - classpath 'com.android.tools.build:gradle:7.0.0-beta02' + classpath 'com.android.tools.build:gradle:7.0.0-beta03' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32" // NOTE: Do not place your application dependencies here; they belong