-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2、将图片加载抽取出来,可提供给外部进行使用
- Loading branch information
zhujiang2
committed
May 29, 2021
1 parent
6357153
commit d41df9a
Showing
3 changed files
with
62 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters