parent
6a5bed626a
commit
5028f60325
@ -0,0 +1,56 @@
|
||||
package com.common.commonlib.image.config
|
||||
|
||||
import com.bumptech.glide.MemoryCategory
|
||||
|
||||
object GlideBaseConfig {
|
||||
|
||||
enum class MemoryType(var memoryCategory: MemoryCategory) {
|
||||
/**
|
||||
* Tells Glide's memory cache and bitmap pool to use at most half of their initial maximum size.
|
||||
*/
|
||||
LOW(MemoryCategory.LOW),
|
||||
|
||||
/**
|
||||
* Tells Glide's memory cache and bitmap pool to use at most their initial maximum size.
|
||||
*/
|
||||
NORMAL(MemoryCategory.NORMAL),
|
||||
|
||||
/**
|
||||
* Tells Glide's memory cache and bitmap pool to use at most one and a half times their initial
|
||||
* maximum size.
|
||||
*/
|
||||
HIGH(MemoryCategory.HIGH)
|
||||
}
|
||||
|
||||
enum class LoadMode {
|
||||
/**
|
||||
* default loading type
|
||||
*/
|
||||
DEFAULT,
|
||||
|
||||
/**
|
||||
* cache all resource
|
||||
*/
|
||||
ALL,
|
||||
|
||||
/**
|
||||
* skip loading from disk
|
||||
*/
|
||||
SKIP_DISK,
|
||||
|
||||
/**
|
||||
* skip loading from memory cache
|
||||
*/
|
||||
SKIP_MEM,
|
||||
|
||||
/**
|
||||
* skip loading form all cache
|
||||
*/
|
||||
SKIP_CACHE,
|
||||
|
||||
/**
|
||||
* don't load form internet
|
||||
*/
|
||||
SKIP_NET
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.common.commonlib.image.loader
|
||||
|
||||
import android.graphics.drawable.Drawable
|
||||
import com.bumptech.glide.RequestBuilder
|
||||
import com.common.commonlib.image.config.GlideHolderConfig
|
||||
|
||||
/**
|
||||
* 全局配置适配器
|
||||
*/
|
||||
object GlobalConfigHelper {
|
||||
var globalHolderConfig: GlideHolderConfig? = null
|
||||
|
||||
fun loadWithGlobalConfig(requestBuilder: RequestBuilder<Drawable>): RequestBuilder<Drawable> {
|
||||
return loadWithGlobalConfig(requestBuilder, globalHolderConfig)
|
||||
}
|
||||
|
||||
fun loadWithGlobalConfig(
|
||||
requestBuilder: RequestBuilder<Drawable>,
|
||||
holderConfig: GlideHolderConfig?
|
||||
): RequestBuilder<Drawable> {
|
||||
var request = requestBuilder
|
||||
if (holderConfig != null) {
|
||||
if (holderConfig.holderDrawable != null) {
|
||||
request = request.placeholder(holderConfig.holderDrawable)
|
||||
}
|
||||
if (holderConfig.holderId != -1) {
|
||||
request = request.placeholder(holderConfig.holderId)
|
||||
}
|
||||
if (holderConfig.errorDrawable != null) {
|
||||
request = request.error(holderConfig.errorDrawable)
|
||||
}
|
||||
if (holderConfig.errorId != -1) {
|
||||
request = request.error(holderConfig.errorId)
|
||||
}
|
||||
if (holderConfig.errorBuilder != null) {
|
||||
request = request.error(holderConfig.errorBuilder)
|
||||
}
|
||||
if (holderConfig.fallBackDrawable != null) {
|
||||
request = request.fallback(holderConfig.fallBackDrawable)
|
||||
}
|
||||
if (holderConfig.fallBackId != -1) {
|
||||
request = request.fallback(holderConfig.fallBackId)
|
||||
}
|
||||
}
|
||||
return request
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.common.commonlib.image.loader
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.ImageView
|
||||
import com.common.commonlib.image.config.GlideBaseConfig
|
||||
import com.common.commonlib.image.config.GlideHolderConfig
|
||||
|
||||
/**
|
||||
* 加载器接口
|
||||
*/
|
||||
interface ILoader {
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param context 上下文
|
||||
* @param memoryCategory 缓存类型
|
||||
*/
|
||||
fun init(context: Context, memoryCategory: GlideBaseConfig.MemoryType)
|
||||
|
||||
/**
|
||||
* 加载图片
|
||||
* @param context 上下文
|
||||
* @param url 图片链接
|
||||
* @param view 图片控件
|
||||
*/
|
||||
fun load(context: Context, url: String?, view: ImageView)
|
||||
|
||||
/**
|
||||
* 加载图片
|
||||
* @param context 上下文
|
||||
* @param url 图片链接
|
||||
* @param view 图片控件
|
||||
* @param loadingMode 加载模式
|
||||
*/
|
||||
fun loadWithMode(
|
||||
context: Context,
|
||||
url: String?,
|
||||
view: ImageView,
|
||||
loadingMode: GlideBaseConfig.LoadMode
|
||||
)
|
||||
|
||||
/**
|
||||
* 加载图片
|
||||
* @param context 上下文
|
||||
* @param url 图片链接
|
||||
* @param view 图片控件
|
||||
* @param placeGlideHolder 占位图
|
||||
*/
|
||||
fun loadWithPlaceHolder(
|
||||
context: Context,
|
||||
url: String,
|
||||
view: ImageView,
|
||||
placeGlideHolder: GlideHolderConfig
|
||||
)
|
||||
|
||||
/**
|
||||
* 清理内存缓存
|
||||
*/
|
||||
fun clearMemoryCache(context: Context)
|
||||
|
||||
/**
|
||||
* 清理磁盘缓存
|
||||
*/
|
||||
fun clearDiskCache(context: Context)
|
||||
|
||||
/**
|
||||
* 清理所有缓存
|
||||
*/
|
||||
fun clearAllCache(context: Context)
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.common.commonlib.image.loader
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.ImageView
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
||||
import com.common.commonlib.image.config.GlideBaseConfig
|
||||
import com.common.commonlib.image.config.GlideHolderConfig
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
/**
|
||||
* 图片加载类
|
||||
*/
|
||||
object ImageLoader : ILoader {
|
||||
|
||||
override fun init(
|
||||
context: Context, memoryCategory: GlideBaseConfig.MemoryType
|
||||
) {
|
||||
Glide.get(context).setMemoryCategory(memoryCategory.memoryCategory)
|
||||
}
|
||||
|
||||
fun setGlobalConfig(globalHolderConfig: GlideHolderConfig) {
|
||||
GlobalConfigHelper.globalHolderConfig = globalHolderConfig
|
||||
}
|
||||
|
||||
override fun load(context: Context, url: String?, view: ImageView) {
|
||||
loadWithMode(context, url, view, GlideBaseConfig.LoadMode.DEFAULT)
|
||||
}
|
||||
|
||||
override fun loadWithMode(
|
||||
context: Context,
|
||||
url: String?,
|
||||
view: ImageView,
|
||||
loadingMode: GlideBaseConfig.LoadMode
|
||||
) {
|
||||
val request = GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url))
|
||||
when (loadingMode) {
|
||||
GlideBaseConfig.LoadMode.DEFAULT ->
|
||||
request.into(view)
|
||||
GlideBaseConfig.LoadMode.ALL ->
|
||||
request.diskCacheStrategy(DiskCacheStrategy.ALL).into(view)
|
||||
GlideBaseConfig.LoadMode.SKIP_CACHE ->
|
||||
request.skipMemoryCache(true)
|
||||
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
||||
.into(view)
|
||||
GlideBaseConfig.LoadMode.SKIP_DISK ->
|
||||
request.diskCacheStrategy(DiskCacheStrategy.NONE).into(view)
|
||||
GlideBaseConfig.LoadMode.SKIP_MEM ->
|
||||
request.skipMemoryCache(true).into(view)
|
||||
GlideBaseConfig.LoadMode.SKIP_NET ->
|
||||
request.onlyRetrieveFromCache(true).into(view)
|
||||
}
|
||||
}
|
||||
|
||||
override fun loadWithPlaceHolder(
|
||||
context: Context,
|
||||
url: String,
|
||||
view: ImageView,
|
||||
placeGlideHolder: GlideHolderConfig
|
||||
) {
|
||||
GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url), placeGlideHolder)
|
||||
.into(view)
|
||||
}
|
||||
|
||||
override fun clearMemoryCache(context: Context) = runBlocking<Unit> {
|
||||
launch(Dispatchers.Main) {
|
||||
Glide.get(context).clearMemory()
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearDiskCache(context: Context) = runBlocking<Unit> {
|
||||
launch(Dispatchers.IO) {
|
||||
Glide.get(context).clearDiskCache()
|
||||
}
|
||||
}
|
||||
|
||||
override fun clearAllCache(context: Context) = runBlocking<Unit> {
|
||||
launch(Dispatchers.Main) {
|
||||
Glide.get(context).clearMemory()
|
||||
}
|
||||
launch(Dispatchers.IO) {
|
||||
Glide.get(context).clearDiskCache()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue