author:wangyimiao

desc:添加图片加载库
master
yimiao 3 years ago
parent 6a5bed626a
commit 5028f60325

@ -5,6 +5,8 @@ import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import com.common.commonlib.image.config.GlideHolderConfig;
import com.common.commonlib.image.loader.ImageLoader;
import com.common.commonlib.net.RequestCallBack; import com.common.commonlib.net.RequestCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor; import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
import com.common.commonlib.net.interceptor.ResponseHeadInterceptor; import com.common.commonlib.net.interceptor.ResponseHeadInterceptor;
@ -14,6 +16,7 @@ import com.common.commonlibtest.bean.LoginResponse;
import com.common.commonlibtest.manager.CollectionLoader; import com.common.commonlibtest.manager.CollectionLoader;
import com.common.commonlibtest.manager.InternetLoader; import com.common.commonlibtest.manager.InternetLoader;
import com.common.commonlibtest.manager.LoginLoader; import com.common.commonlibtest.manager.LoginLoader;
import com.yinuo.commonlibtest.R;
import com.yinuo.commonlibtest.databinding.ActivityMainBinding; import com.yinuo.commonlibtest.databinding.ActivityMainBinding;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -36,6 +39,13 @@ public class MainActivity extends AppCompatActivity {
} }
private void initView() { private void initView() {
String url = "https://pics1.baidu.com/feed/48540923dd54564eb4cf53d7fabb2e84d0584f25.jpeg?token=d52b80fe96b3211270261b6c71b9efe2";
String url_1 = "https://pics6.baidu.com/feed/574e9258d109b3decb67a7bc7b814e87820a4cca.jpeg?token=56f8cb89982903d054e94bb89e34f84c&s=0D84C41006A072B8D38C054D030030E2";
GlideHolderConfig config = new GlideHolderConfig();
config.setErrorId(R.drawable.ic_record_delete);
ImageLoader.INSTANCE.setGlobalConfig(config);
ImageLoader.INSTANCE.load(this, url_1, binding.iv);
binding.tv.setOnClickListener(l -> { binding.tv.setOnClickListener(l -> {
InternetLoader loader = new InternetLoader(); InternetLoader loader = new InternetLoader();
loader.getHomeArticles(0, new RequestCallBack<ArticlesResponse>() { loader.getHomeArticles(0, new RequestCallBack<ArticlesResponse>() {

@ -2,11 +2,14 @@ package com.common.commonlibtest
import android.app.Application import android.app.Application
import com.common.commonlib.CommonApplication import com.common.commonlib.CommonApplication
import com.common.commonlib.image.config.GlideBaseConfig
import com.common.commonlib.image.loader.ImageLoader
class MyApplication : Application() { class MyApplication : Application() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
CommonApplication.intLibs(this) CommonApplication.intLibs(this)
ImageLoader.init(this, GlideBaseConfig.MemoryType.NORMAL)
} }
} }

@ -5,6 +5,16 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.2" />
<TextView <TextView
android:id="@+id/tv" android:id="@+id/tv"
android:layout_width="wrap_content" android:layout_width="wrap_content"

@ -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,27 @@
package com.common.commonlib.image.config
import android.graphics.drawable.Drawable
/**
* 占位符配置
*/
class GlideHolderConfig {
/**
* holder占位图
*/
var holderDrawable: Drawable? = null
var holderId: Int = -1
/**
* 加载失败时且没有配置fallback占位图时展示
*/
var errorDrawable: Drawable? = null
var errorId: Int = -1
var errorBuilder: Any? = null
/**
* 加载失败时是否允许失败作为正常流程展示
*/
var fallBackDrawable: Drawable? = null
var fallBackId: Int = -1
}

@ -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()
}
}
}

@ -1,4 +1,4 @@
package com.common.commonlib.image package com.common.commonlib.image.module
import android.content.Context import android.content.Context
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
@ -9,14 +9,14 @@ import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.module.LibraryGlideModule import com.bumptech.glide.module.LibraryGlideModule
import java.io.InputStream import java.io.InputStream
/**
* okhttp配置
*/
@GlideModule @GlideModule
class OkHttpLibGlideModule : LibraryGlideModule() { class OkHttpLibraryGlideModule : LibraryGlideModule() {
override fun registerComponents(context: Context, glide: Glide, registry: Registry) { override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
registry.replace( // 替换网路请求使用okhttp
GlideUrl::class.java, registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory())
InputStream::class.java,
OkHttpUrlLoader.Factory()
)
super.registerComponents(context, glide, registry)
} }
} }
Loading…
Cancel
Save