当前位置: 首页 > news >正文

信融网站建设网站开发产品营销方案

信融网站建设网站开发,产品营销方案,域名注册美橙,东莞网站建设曼哈顿信科文章目录 Android 优雅封装Glide核心思想定义策略接口定义图片选项实现Glide策略图片管理类使用 Android 优雅封装Glide 核心思想 使用策略模式实现不同图片加载框架的切换,使用建造者设计模式处理不同参数,最后通过 ImageLoader 进行管理。 定义策略…

文章目录

  • Android 优雅封装Glide
    • 核心思想
    • 定义策略接口
    • 定义图片选项
    • 实现Glide策略
    • 图片管理类
    • 使用

Android 优雅封装Glide

核心思想

使用策略模式实现不同图片加载框架的切换,使用建造者设计模式处理不同参数,最后通过 ImageLoader 进行管理。

定义策略接口

interface ILoaderStrategy {fun loadImage(configs: ImageOptions)fun clearDiskCache(context: Context)fun clearMemoryCache(context: Context)fun clearAll(context: Context) {clearDiskCache(context)clearMemoryCache(context)}fun clear(imageView: ImageView)
}

定义图片选项

class ImageOptions private constructor() {var targetObj: Any? = null // 生命周期对象var targetView: ImageView? = null // 目标ImageViewvar resource: Any? = null // 加载资源var width = -1 // 指定宽度var height = -1 // 指定高度var isDiskCache = true // 磁盘缓存var isMemoryCache = true // 内存缓存@DrawableResvar placeholder: Int = -1 // 占位图资源@DrawableResvar error: Int = -1 // 失败图资源var listener: Listener? = nullcompanion object {fun create(): ImageOptions {return ImageOptions()}}fun with(targetObj: Any): ImageOptions {this.targetObj = targetObjreturn this}fun loadResource(resource: Any): ImageOptions {this.resource = resourcereturn this}fun size(size: Int): ImageOptions {return size(width, height)}fun size(width: Int, height: Int): ImageOptions {this.width = widththis.height = heightreturn this}fun placeholder(@DrawableRes placeholder: Int): ImageOptions {this.placeholder = placeholderreturn this}fun error(@DrawableRes error: Int): ImageOptions {this.error = errorreturn this}fun setDiskCache(isCache: Boolean): ImageOptions {isDiskCache = isCachereturn this}fun setMemoryCache(isCache: Boolean): ImageOptions {isMemoryCache = isCachereturn this}fun setListener(listener: Listener): ImageOptions {this.listener = listenerreturn this}fun into(imageView: ImageView) {this.targetView = imageViewImageLoader.loadOptions(this)}interface Listener {fun onSuccess(model: Any?)fun onFail(model: Any?)}
}

实现Glide策略

class GlideLoader : ILoaderStrategy {private lateinit var requestManager: RequestManageroverride fun loadImage(options: ImageOptions) {requestManager = getRequestManager(options.targetObj)var requestBuilder: RequestBuilder<Drawable>? = nulloptions.resource?.let {requestBuilder = generateRequestBuilder(it)}requestBuilder?.let {if (options.placeholder != -1) {it.placeholder(options.placeholder)}if (options.error != -1) {it.error(options.error)}if (options.width != -1 || options.height != -1) {it.override(options.width, options.height)}it.skipMemoryCache(options.isMemoryCache)it.diskCacheStrategy(if (options.isDiskCache) DiskCacheStrategy.AUTOMATIC else DiskCacheStrategy.NONE)options.listener?.let { listener ->it.addListener(object : RequestListener<Drawable> {override fun onLoadFailed(e: GlideException?,model: Any?,target: Target<Drawable>?,isFirstResource: Boolean): Boolean {listener.onFail(model)return false}override fun onResourceReady(resource: Drawable?,model: Any?,target: Target<Drawable>?,dataSource: DataSource?,isFirstResource: Boolean): Boolean {listener.onSuccess(model)return false}})}if (options.targetView == null) {throw IllegalArgumentException("targetView cannot be null");}it.into(options.targetView!!)}}private fun getRequestManager(targetObj: Any?): RequestManager {return if (targetObj is FragmentActivity) {Glide.with(targetObj)} else if (targetObj is Context) {Glide.with(targetObj)} else if (targetObj is View) {Glide.with(targetObj)} else if (targetObj is Fragment) {Glide.with(targetObj)} else {throw IllegalArgumentException("You cannot start a load on a null Context");}}private fun generateRequestBuilder(res: Any): RequestBuilder<Drawable>? {return if (res is String) {requestManager.load(res)} else if (res is File) {requestManager.load(res)} else {return null}}override fun clearDiskCache(context: Context) {thread {Glide.get(context).clearDiskCache()}}override fun clearMemoryCache(context: Context) {Glide.get(context).clearMemory()}override fun clear(imageView: ImageView) {Glide.with(imageView.context).clear(imageView)}
}

图片管理类

object ImageLoader {private var imageLoader: ILoaderStrategy? = nullfun setImageLoader(imageLoader: ILoaderStrategy) {this.imageLoader = imageLoader}fun with(targetObj: Any): ImageOptions {val options = ImageOptions.create()options.with(targetObj)return options}fun loadOptions(options: ImageOptions) {imageLoader!!.loadImage(options)}fun clearDiskCache(context: Context){imageLoader!!.clearDiskCache(context)}fun clearMemoryCache(context: Context){imageLoader!!.clearMemoryCache(context)}fun clearAll(context:Context){imageLoader!!.clearAll(context)}fun clear(imageView: ImageView){imageLoader!!.clear(imageView)}}

使用

override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_image_loader)imageView0 = findViewById(R.id.imageView0)imageView1 = findViewById(R.id.imageView1)imageView2 = findViewById(R.id.imageView2)ImageLoader.setImageLoader(GlideLoader())val url = "https://i-blog.csdnimg.cn/blog_migrate/de6e3262387d57977e53af596a87f582.png"ImageLoader.with(this).loadResource(url).size(100).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView1)ImageLoader.with(this).loadResource(File(cacheDir, "aaa.png")).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView2)
} 
ImageLoader.clear(imageView1)
ImageLoader.clearAll(this)
ImageLoader.clearMemoryCache(this)
ImageLoader.clearDiskCache(this)
http://www.15wanjia.com/news/50098.html

相关文章:

  • 20亿做网站百度推广新手入门
  • 网站优化推广方案网站seo李守洪排名大师
  • 申请域网站网站关键词优化技巧
  • 南宁彩票网站开发域名查询平台
  • 网站建设怎样接业务百度游戏app下载
  • jpress wordpress对比网络优化有前途吗
  • 内蒙古知名网站建设百度快照是干嘛的
  • wordpress百度分享插件广州seo网络培训课程
  • wordpress 怎么重新安装宁波seo推广
  • 网站开发工程师职责网络营销和推广做什么
  • 优秀网站参考青岛seo关键字排名
  • 微趋道官网手机小程序制作优化seo软件
  • 怎样把广告放到百度优就业seo
  • 网站添加锚点百度上做广告怎么收费
  • 营销型网站建设菲凡网北京网络网站推广
  • 怎么在一个网站做编辑苏州疫情最新情况
  • 网站建设做微营销免费网络推广公司
  • 网站报价关键词怎么做快速的有排名
  • 潍坊市城市建设官网站seo优化广告
  • 寿县网站建设怎样创建一个网站
  • 邯郸网站建设的地方中国今天刚刚发生的新闻
  • 微网站在线制作青岛网站快速排名提升
  • 网站长图怎么做广州seo网站多少钱
  • 青岛做网站推广公司哪家好百度快照查询入口
  • ui培训多少钱上海seo服务
  • wordpress 汉化包seo职业发展
  • 做一个静态网站多少钱备案查询
  • 织梦系统做的网站打开慢网站推广的常用方法
  • 做seo网站不用域名100个成功营销策划案例
  • 从做系统后以前的网站打不开了怎么办理网站软件下载大全