最近项目开发一个图片下载功能,因为需要考虑到在点开图片后,进行第一次请求之后,后面若要反复请求,这样就浪费了很多的流量,这样对用户的体验感不好,后来大佬说可以用Glide,因为这个框架自带了三级缓存机制,当地缓存和内存缓存都没有的时候,才会去进行网络请求,所以就研究下这个框架
为了更好对比这个框架确实好用,先看下原生代码是怎么加载网络图片的
class MainActivity : AppCompatActivity() {
//图片的控件
lateinit var ivPic:ImageView
private val handler=object :Handler(){
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
var bitmap:Bitmap=msg.obj as Bitmap
when(msg.what){
200->ivPic.setImageBitmap(bitmap)
-1->ivPic.setImageResource(R.drawable.lose)
else->ivPic.setImageResource(R.drawable.lose)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ivPic=findViewById(R.id.gilde_pic)
}
fun onClick(view: View){
//这个是原生代码
loadGildePic("https://www.imooc.com/static/img/index/logo-recommended.png")
//这个是使用Glide的
// gildeloadPic("https://www.imooc.com/static/img/index/logo-recommended.png")
}
fun loadGildePic(msg:String){
//默认的背景图片
ivPic.setImageResource(R.drawable.icon_follow_bg_red_xxh)
val message=Message()
Thread(){
kotlin.run {
try {
val url=URL(msg)
//获取httpURLConnection对象
val httpURLConnection=url.openConnection() as HttpURLConnection
//设置请求模式
httpURLConnection.requestMethod="GET"
//获取请求码
var code=httpURLConnection.responseCode
//200为成功
if (code==200){
val input=httpURLConnection.inputStream
val bitmap=BitmapFactory.decodeStream(input)
message.obj=bitmap
message.what=200
}else{
message.what=code
}
}catch (e:MalformedURLException){
e.printStackTrace()
message.what=-1
}catch (e:IOException){
e.printStackTrace()
message.what=-1
}finally {
handler.sendMessage(message)
}
}
}.start()
}
使用原生代码的话,比较麻烦,又要开新的线程,又要使用handle去传递请求结果,对比Glide的话确实很麻烦,然后下面就说下Glide神器了
先把github地址送上:
https://muyangmin.github.io/glide-docs-cn/doc/download-setup.html
添加依赖如下:
#### Gradle[](https://muyangmin.github.io/glide-docs-cn/doc/download-setup.html#gradle)
如果使用 Gradle,可从 Maven Central 或 JCenter 中添加对 Glide 的依赖。同样,你还需要添加 Android 支持库的依赖。
repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}
dependencies {
compile 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
}
注意:如果可能,请尽量在你的依赖中避免使用 `@aar` 。如果你必须这么做,请添加 `transitive=true` 以确保所有必要的类都被包含到你的 API 中:
dependencies {
implementation ("com.github.bumptech.glide:glide:4.11.0@aar") {
transitive = true
}
}
代码如下:
fun Glidetext(img: String){
//with传入activity
Glide.with(this)
//load加载网络请求或者本地资源也行
.load(img)
//指定展示图片的控件
.into(ivPic)
}
就这一句代码就已经完成了,上面一大串的原生代码的功能了,是不是很简单,而且Glide框架还提供了各式各样的配置,让我们可以按照项目的实际需求来进行使用,真的不要太贴心
配置代码如下(这个只是配置方法的展示):
fun gildeloadPic(img:String){
//build出RequestOptionsd对象,通过构造option去配置Glide的效果,placeholderOf在加载时默认的图片资源,error加载失败时调用的图片资源
//circleCrop使加载的图片呈现圆形
val option=RequestOptions.placeholderOf(R.drawable.icon_follow_bg_red_xxh)
.error(R.drawable.lose)
.circleCrop()
Glide.with(this)
.load(img)
//通过apply去应用上面的配置
.apply(option)
.into(ivPic)
}
但是实际开发的时候,Glide用到的地方都比较多,所以建议直接创建一个工具类,方便全局使用,代码如下:
class GlideUtils {
val baseoptions=RequestOptions.placeholderOf(R.drawable.icon_follow_bg_red_xxh)
.error(R.drawable.lose)
//添加了圆形的属性
val circleoptions=baseoptions.circleCrop()
}
然后在需要调用的的位置使用的代码如下:
fun gildeloadPic(img:String){
//build出RequestOptionsd对象,通过构造option去配置Glide的效果,placeholderOf在加载时默认的图片资源,error加载失败时调用的图片资源
//circleCrop使加载的图片呈现圆形
// val option=RequestOptions.placeholderOf(R.drawable.icon_follow_bg_red_xxh)
// .error(R.drawable.lose)
// .circleCrop()
Glide.with(this)
.load(img)
// .apply(option)
.apply(GlideUtils().circleoptions)
.into(ivPic)
}
然后还有里面各种各样配置接口可以参考下大神的文章:
https://juejin.im/post/6869937984960593934
想了解缓存的话,可以参考下手撕系列的:
https://www.jianshu.com/p/b85f89fce019
好了,今天说到这里了,如果有疑问的,可以在下面留言哦,米花你懂得!!
网友评论