author:wangyimiao

desc:添加图片加载库过渡动画
master
yimiao 3 years ago
parent 5028f60325
commit 588fbd918a

@ -6,6 +6,7 @@ import android.os.Bundle;
import android.util.Log;
import com.common.commonlib.image.config.GlideHolderConfig;
import com.common.commonlib.image.config.GlideTransitionConfig;
import com.common.commonlib.image.loader.ImageLoader;
import com.common.commonlib.net.RequestCallBack;
import com.common.commonlib.net.interceptor.RequestHeadInterceptor;
@ -42,8 +43,10 @@ public class MainActivity extends AppCompatActivity {
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);
config.setHolderId(R.drawable.ic_record_delete);
ImageLoader.INSTANCE.setGlobalConfig(config);
ImageLoader.INSTANCE.setTransition(new GlideTransitionConfig(1000));
ImageLoader.INSTANCE.load(this, url_1, binding.iv);
binding.tv.setOnClickListener(l -> {

@ -7,8 +7,8 @@
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="240dp"
android:layout_height="240dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

@ -132,4 +132,4 @@
# 保留glide的配置类
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl
-keep class com.common.commonlib.image.module.OkHttpLibraryGlideModule

@ -0,0 +1,14 @@
package com.common.commonlib.image.config
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
/**
* 转换配置
*/
class GlideTransitionConfig(time: Int?) {
val transition: DrawableTransitionOptions = if (time != null) {
DrawableTransitionOptions.withCrossFade(time)
} else {
DrawableTransitionOptions.withCrossFade()
}
}

@ -2,6 +2,7 @@ package com.common.commonlib.image.loader
import android.graphics.drawable.Drawable
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.common.commonlib.image.config.GlideHolderConfig
/**
@ -9,14 +10,23 @@ import com.common.commonlib.image.config.GlideHolderConfig
*/
object GlobalConfigHelper {
var globalHolderConfig: GlideHolderConfig? = null
var globalTransition: DrawableTransitionOptions? = null
fun loadWithGlobalConfig(requestBuilder: RequestBuilder<Drawable>): RequestBuilder<Drawable> {
return loadWithGlobalConfig(requestBuilder, globalHolderConfig)
return loadWithGlobalConfig(requestBuilder, globalHolderConfig, globalTransition)
}
fun loadWithGlobalConfig(
requestBuilder: RequestBuilder<Drawable>,
holderConfig: GlideHolderConfig?
): RequestBuilder<Drawable> {
return loadWithGlobalConfig(requestBuilder, holderConfig, globalTransition)
}
private fun loadWithGlobalConfig(
requestBuilder: RequestBuilder<Drawable>,
holderConfig: GlideHolderConfig?,
transition: DrawableTransitionOptions?
): RequestBuilder<Drawable> {
var request = requestBuilder
if (holderConfig != null) {
@ -42,6 +52,10 @@ object GlobalConfigHelper {
request = request.fallback(holderConfig.fallBackId)
}
}
if (transition != null) {
request = request.transition(transition)
}
return request
}
}

@ -4,8 +4,10 @@ import android.content.Context
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.common.commonlib.image.config.GlideBaseConfig
import com.common.commonlib.image.config.GlideHolderConfig
import com.common.commonlib.image.config.GlideTransitionConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
@ -25,6 +27,10 @@ object ImageLoader : ILoader {
GlobalConfigHelper.globalHolderConfig = globalHolderConfig
}
fun setTransition(transitionConfig: GlideTransitionConfig) {
GlobalConfigHelper.globalTransition = transitionConfig.transition
}
override fun load(context: Context, url: String?, view: ImageView) {
loadWithMode(context, url, view, GlideBaseConfig.LoadMode.DEFAULT)
}

Loading…
Cancel
Save