Skip to content

Commit

Permalink
1、升级Gradle版本
Browse files Browse the repository at this point in the history
2、将图片加载抽取出来,可提供给外部进行使用
  • Loading branch information
zhujiang2 committed May 29, 2021
1 parent 6357153 commit d41df9a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 41 deletions.
42 changes: 2 additions & 40 deletions banner/src/main/java/com/zj/banner/ui/BannerCard.kt
Original file line number Diff line number Diff line change
@@ -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 图片展示卡片
Expand Down Expand Up @@ -45,36 +37,6 @@ fun <T : BaseBannerBean> 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)
}
}
59 changes: 59 additions & 0 deletions banner/src/main/java/com/zj/banner/utils/ImageLoader.kt
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d41df9a

Please sign in to comment.