diff --git a/app/src/main/java/com/common/commonlibtest/MainActivity.java b/app/src/main/java/com/common/commonlibtest/MainActivity.java index c71ad34..bd0e243 100644 --- a/app/src/main/java/com/common/commonlibtest/MainActivity.java +++ b/app/src/main/java/com/common/commonlibtest/MainActivity.java @@ -5,6 +5,8 @@ import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; 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.interceptor.RequestHeadInterceptor; 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.InternetLoader; import com.common.commonlibtest.manager.LoginLoader; +import com.yinuo.commonlibtest.R; import com.yinuo.commonlibtest.databinding.ActivityMainBinding; import org.jetbrains.annotations.Nullable; @@ -36,6 +39,13 @@ public class MainActivity extends AppCompatActivity { } 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 -> { InternetLoader loader = new InternetLoader(); loader.getHomeArticles(0, new RequestCallBack() { diff --git a/app/src/main/java/com/common/commonlibtest/MyApplication.kt b/app/src/main/java/com/common/commonlibtest/MyApplication.kt index 4273915..a0a8f4e 100644 --- a/app/src/main/java/com/common/commonlibtest/MyApplication.kt +++ b/app/src/main/java/com/common/commonlibtest/MyApplication.kt @@ -2,11 +2,14 @@ package com.common.commonlibtest import android.app.Application import com.common.commonlib.CommonApplication +import com.common.commonlib.image.config.GlideBaseConfig +import com.common.commonlib.image.loader.ImageLoader class MyApplication : Application() { override fun onCreate() { super.onCreate() CommonApplication.intLibs(this) + ImageLoader.init(this, GlideBaseConfig.MemoryType.NORMAL) } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fad0dab..ff5ad2f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,6 +5,16 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + ): RequestBuilder { + return loadWithGlobalConfig(requestBuilder, globalHolderConfig) + } + + fun loadWithGlobalConfig( + requestBuilder: RequestBuilder, + holderConfig: GlideHolderConfig? + ): RequestBuilder { + 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 + } +} \ No newline at end of file diff --git a/commonLib/src/main/java/com/common/commonlib/image/loader/ILoader.kt b/commonLib/src/main/java/com/common/commonlib/image/loader/ILoader.kt new file mode 100644 index 0000000..a379281 --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/image/loader/ILoader.kt @@ -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) +} \ No newline at end of file diff --git a/commonLib/src/main/java/com/common/commonlib/image/loader/ImageLoader.kt b/commonLib/src/main/java/com/common/commonlib/image/loader/ImageLoader.kt new file mode 100644 index 0000000..2a57775 --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/image/loader/ImageLoader.kt @@ -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 { + launch(Dispatchers.Main) { + Glide.get(context).clearMemory() + } + } + + override fun clearDiskCache(context: Context) = runBlocking { + launch(Dispatchers.IO) { + Glide.get(context).clearDiskCache() + } + } + + override fun clearAllCache(context: Context) = runBlocking { + launch(Dispatchers.Main) { + Glide.get(context).clearMemory() + } + launch(Dispatchers.IO) { + Glide.get(context).clearDiskCache() + } + } +} \ No newline at end of file diff --git a/commonLib/src/main/java/com/common/commonlib/image/OkHttpLibGlideModule.kt b/commonLib/src/main/java/com/common/commonlib/image/module/OkHttpLibraryGlideModule.kt similarity index 60% rename from commonLib/src/main/java/com/common/commonlib/image/OkHttpLibGlideModule.kt rename to commonLib/src/main/java/com/common/commonlib/image/module/OkHttpLibraryGlideModule.kt index cb1ec7b..f7fffd5 100644 --- a/commonLib/src/main/java/com/common/commonlib/image/OkHttpLibGlideModule.kt +++ b/commonLib/src/main/java/com/common/commonlib/image/module/OkHttpLibraryGlideModule.kt @@ -1,4 +1,4 @@ -package com.common.commonlib.image +package com.common.commonlib.image.module import android.content.Context import com.bumptech.glide.Glide @@ -9,14 +9,14 @@ import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.module.LibraryGlideModule import java.io.InputStream +/** + * okhttp配置 + */ @GlideModule -class OkHttpLibGlideModule : LibraryGlideModule() { +class OkHttpLibraryGlideModule : LibraryGlideModule() { + override fun registerComponents(context: Context, glide: Glide, registry: Registry) { - registry.replace( - GlideUrl::class.java, - InputStream::class.java, - OkHttpUrlLoader.Factory() - ) - super.registerComponents(context, glide, registry) + // 替换网路请求使用okhttp + registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory()) } } \ No newline at end of file