author:wangyimiao

desc:添加图片圆角能力
master
yimiao 3 years ago
parent 588fbd918a
commit b74b0cd103

@ -45,9 +45,9 @@ public class MainActivity extends AppCompatActivity {
GlideHolderConfig config = new GlideHolderConfig(); GlideHolderConfig config = new GlideHolderConfig();
config.setHolderId(R.drawable.ic_record_delete); config.setHolderId(R.drawable.ic_record_delete);
ImageLoader.INSTANCE.setGlobalConfig(config); 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 -> { binding.tv.setOnClickListener(l -> {
InternetLoader loader = new InternetLoader(); InternetLoader loader = new InternetLoader();

@ -4,6 +4,7 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
/** /**
* 转换配置 * 转换配置
* @param time 动画时长
*/ */
class GlideTransitionConfig(time: Int?) { class GlideTransitionConfig(time: Int?) {
val transition: DrawableTransitionOptions = if (time != null) { val transition: DrawableTransitionOptions = if (time != null) {

@ -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<Drawable>,
transformations: List<BitmapTransformation>
): RequestBuilder<Drawable> {
var temp = requestBuilder
for (item in transformations) {
temp = temp.transform(item)
}
return temp
}
fun loadWithMod(
requestBuilder: RequestBuilder<Drawable>,
loadingMode: GlideBaseConfig.LoadMode
): RequestBuilder<Drawable> {
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
}
}

@ -25,6 +25,15 @@ interface ILoader {
*/ */
fun load(context: Context, url: String?, view: ImageView) 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 上下文 * @param context 上下文

@ -1,10 +1,12 @@
package com.common.commonlib.image.loader package com.common.commonlib.image.loader
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView import android.widget.ImageView
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions 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.GlideBaseConfig
import com.common.commonlib.image.config.GlideHolderConfig import com.common.commonlib.image.config.GlideHolderConfig
import com.common.commonlib.image.config.GlideTransitionConfig import com.common.commonlib.image.config.GlideTransitionConfig
@ -27,7 +29,7 @@ object ImageLoader : ILoader {
GlobalConfigHelper.globalHolderConfig = globalHolderConfig GlobalConfigHelper.globalHolderConfig = globalHolderConfig
} }
fun setTransition(transitionConfig: GlideTransitionConfig) { fun setGlobalTransition(transitionConfig: GlideTransitionConfig) {
GlobalConfigHelper.globalTransition = transitionConfig.transition GlobalConfigHelper.globalTransition = transitionConfig.transition
} }
@ -35,29 +37,25 @@ object ImageLoader : ILoader {
loadWithMode(context, url, view, GlideBaseConfig.LoadMode.DEFAULT) 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<BitmapTransformation>()
transformations.add(GranularRoundedCorners(radius, radius, radius, radius))
request = ConfigHelper.loadWithTransformation(request, transformations)
loadIntoView(request, view)
}
override fun loadWithMode( override fun loadWithMode(
context: Context, context: Context,
url: String?, url: String?,
view: ImageView, view: ImageView,
loadingMode: GlideBaseConfig.LoadMode loadingMode: GlideBaseConfig.LoadMode
) { ) {
val request = GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url)) var request = GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url))
when (loadingMode) { request = ConfigHelper.loadWithMod(request, loadingMode)
GlideBaseConfig.LoadMode.DEFAULT -> loadIntoView(request, view)
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( override fun loadWithPlaceHolder(
@ -66,8 +64,16 @@ object ImageLoader : ILoader {
view: ImageView, view: ImageView,
placeGlideHolder: GlideHolderConfig placeGlideHolder: GlideHolderConfig
) { ) {
GlobalConfigHelper.loadWithGlobalConfig(Glide.with(context).load(url), placeGlideHolder) loadIntoView(
.into(view) GlobalConfigHelper.loadWithGlobalConfig(
Glide.with(context).load(url),
placeGlideHolder
), view
)
}
private fun loadIntoView(requestBuilder: RequestBuilder<Drawable>, view: ImageView) {
requestBuilder.into(view)
} }
override fun clearMemoryCache(context: Context) = runBlocking<Unit> { override fun clearMemoryCache(context: Context) = runBlocking<Unit> {

Loading…
Cancel
Save