From b74b0cd1030ddeceb0a4fb0d67adbcd2b79c4a0f Mon Sep 17 00:00:00 2001 From: yimiao Date: Mon, 26 Jul 2021 10:50:24 +0800 Subject: [PATCH] =?UTF-8?q?author:wangyimiao=20desc:=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=9C=86=E8=A7=92=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/commonlibtest/MainActivity.java | 4 +- .../image/config/GlideTransitionConfig.kt | 1 + .../commonlib/image/loader/ConfigHelper.kt | 47 +++++++++++++++++ .../common/commonlib/image/loader/ILoader.kt | 9 ++++ .../commonlib/image/loader/ImageLoader.kt | 50 +++++++++++-------- 5 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 commonLib/src/main/java/com/common/commonlib/image/loader/ConfigHelper.kt diff --git a/app/src/main/java/com/common/commonlibtest/MainActivity.java b/app/src/main/java/com/common/commonlibtest/MainActivity.java index cecc44d..f39b57e 100644 --- a/app/src/main/java/com/common/commonlibtest/MainActivity.java +++ b/app/src/main/java/com/common/commonlibtest/MainActivity.java @@ -45,9 +45,9 @@ public class MainActivity extends AppCompatActivity { GlideHolderConfig config = new GlideHolderConfig(); config.setHolderId(R.drawable.ic_record_delete); ImageLoader.INSTANCE.setGlobalConfig(config); - ImageLoader.INSTANCE.setTransition(new GlideTransitionConfig(1000)); + ImageLoader.INSTANCE.setGlobalTransition(new GlideTransitionConfig(1000)); - ImageLoader.INSTANCE.load(this, url_1, binding.iv); + ImageLoader.INSTANCE.loadRound(this, url_1, binding.iv, 8); binding.tv.setOnClickListener(l -> { InternetLoader loader = new InternetLoader(); diff --git a/commonLib/src/main/java/com/common/commonlib/image/config/GlideTransitionConfig.kt b/commonLib/src/main/java/com/common/commonlib/image/config/GlideTransitionConfig.kt index a1bfd2c..3e77c55 100644 --- a/commonLib/src/main/java/com/common/commonlib/image/config/GlideTransitionConfig.kt +++ b/commonLib/src/main/java/com/common/commonlib/image/config/GlideTransitionConfig.kt @@ -4,6 +4,7 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions /** * 转换配置 + * @param time 动画时长 */ class GlideTransitionConfig(time: Int?) { val transition: DrawableTransitionOptions = if (time != null) { diff --git a/commonLib/src/main/java/com/common/commonlib/image/loader/ConfigHelper.kt b/commonLib/src/main/java/com/common/commonlib/image/loader/ConfigHelper.kt new file mode 100644 index 0000000..bd70fbf --- /dev/null +++ b/commonLib/src/main/java/com/common/commonlib/image/loader/ConfigHelper.kt @@ -0,0 +1,47 @@ +package com.common.commonlib.image.loader + +import android.graphics.drawable.Drawable +import com.bumptech.glide.RequestBuilder +import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.bumptech.glide.load.resource.bitmap.BitmapTransformation +import com.common.commonlib.image.config.GlideBaseConfig + +/** + * 加载配置辅助类 + */ +object ConfigHelper { + + fun loadWithTransformation( + requestBuilder: RequestBuilder, + transformations: List + ): RequestBuilder { + var temp = requestBuilder + for (item in transformations) { + temp = temp.transform(item) + } + return temp + } + + fun loadWithMod( + requestBuilder: RequestBuilder, + loadingMode: GlideBaseConfig.LoadMode + ): RequestBuilder { + var request = requestBuilder + request = when (loadingMode) { + GlideBaseConfig.LoadMode.DEFAULT -> + request + GlideBaseConfig.LoadMode.ALL -> + request.diskCacheStrategy(DiskCacheStrategy.ALL) + GlideBaseConfig.LoadMode.SKIP_CACHE -> + request.skipMemoryCache(true) + .diskCacheStrategy(DiskCacheStrategy.NONE) + GlideBaseConfig.LoadMode.SKIP_DISK -> + request.diskCacheStrategy(DiskCacheStrategy.NONE) + GlideBaseConfig.LoadMode.SKIP_MEM -> + request.skipMemoryCache(true) + GlideBaseConfig.LoadMode.SKIP_NET -> + request.onlyRetrieveFromCache(true) + } + 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 index a379281..d90d9ce 100644 --- a/commonLib/src/main/java/com/common/commonlib/image/loader/ILoader.kt +++ b/commonLib/src/main/java/com/common/commonlib/image/loader/ILoader.kt @@ -25,6 +25,15 @@ interface ILoader { */ fun load(context: Context, url: String?, view: ImageView) + /** + * 加载圆角图片 + * @param context 上下文 + * @param url 图片链接 + * @param view 图片控件 + * @param radius 圆角半径 + */ + fun loadRound(context: Context, url: String?, view: ImageView, radius: Float) + /** * 加载图片 * @param context 上下文 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 index fd44290..64bb3da 100644 --- a/commonLib/src/main/java/com/common/commonlib/image/loader/ImageLoader.kt +++ b/commonLib/src/main/java/com/common/commonlib/image/loader/ImageLoader.kt @@ -1,10 +1,12 @@ package com.common.commonlib.image.loader import android.content.Context +import android.graphics.drawable.Drawable import android.widget.ImageView import com.bumptech.glide.Glide -import com.bumptech.glide.load.engine.DiskCacheStrategy -import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions +import com.bumptech.glide.RequestBuilder +import com.bumptech.glide.load.resource.bitmap.BitmapTransformation +import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners import com.common.commonlib.image.config.GlideBaseConfig import com.common.commonlib.image.config.GlideHolderConfig import com.common.commonlib.image.config.GlideTransitionConfig @@ -27,7 +29,7 @@ object ImageLoader : ILoader { GlobalConfigHelper.globalHolderConfig = globalHolderConfig } - fun setTransition(transitionConfig: GlideTransitionConfig) { + fun setGlobalTransition(transitionConfig: GlideTransitionConfig) { GlobalConfigHelper.globalTransition = transitionConfig.transition } @@ -35,29 +37,25 @@ object ImageLoader : ILoader { loadWithMode(context, url, view, GlideBaseConfig.LoadMode.DEFAULT) } + override fun loadRound(context: Context, url: String?, view: ImageView, radius: Float) { + var request = GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url)) + request = ConfigHelper.loadWithMod(request, GlideBaseConfig.LoadMode.DEFAULT) + + val transformations = ArrayList() + transformations.add(GranularRoundedCorners(radius, radius, radius, radius)) + request = ConfigHelper.loadWithTransformation(request, transformations) + loadIntoView(request, view) + } + 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) - } + var request = GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url)) + request = ConfigHelper.loadWithMod(request, loadingMode) + loadIntoView(request, view) } override fun loadWithPlaceHolder( @@ -66,8 +64,16 @@ object ImageLoader : ILoader { view: ImageView, placeGlideHolder: GlideHolderConfig ) { - GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url), placeGlideHolder) - .into(view) + loadIntoView( + GlobalConfigHelper.loadWithGlobalConfig( + Glide.with(context).load(url), + placeGlideHolder + ), view + ) + } + + private fun loadIntoView(requestBuilder: RequestBuilder, view: ImageView) { + requestBuilder.into(view) } override fun clearMemoryCache(context: Context) = runBlocking {