图片加载框架Glide和Picasso的简单使用

本文介绍如何在Android应用中使用Glide与Picasso进行图片加载,并提供依赖引入方式及简单使用示例。

图片加载框架Glide和Picasso的简单使用

Picasso

引入依赖:

//Picasso
implementation 'com.squareup.picasso:picasso:2.71828'

简单使用:[说明:2.71828这一新版不再使用with(Context)改成get()]

String url="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524215585000&di=bfce834d92cbdd3ded74d695cf5c8638&imgtype=0&src=http%3A%2F%2Fimage5.tuku.cn%2Fpic%2Fwallpaper%2Fmeinv%2Frenbihuajiaominv%2F010.jpg";

Picasso.get().load(url)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .resize(300,200)
                .into(img);//ImageView

混淆配置

#okhttp3
# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform

或者

#针对3.0以下
-dontwarn com.squareup.okhttp.**

Glide

引入依赖(默认使用HttpUrlConnection的网络协议,我这边使用okhttp3)

 //Glide by google
 implementation 'com.github.bumptech.glide:glide:4.7.1'
 annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
 // Glide's OkHttp Integration
 compile "com.github.bumptech.glide:okhttp3-integration:4.7.1"
 compile 'com.squareup.okhttp3:okhttp:3.1.2'

简单使用 [新版4.7.1中设置placeholder改为在RequestOption中设置]

 RequestOptions options=new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .override(300,200)
                .error(R.mipmap.ic_launcher);
//一般设置为Activity或者Fragment,这样的生命周期保持一致
Glide.with(this)
      .load(url)
      .apply(options)
      .into(img);//ImageView

混淆配置

#Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}
# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule

后续更新更加强大的使用,例如Glide中设置TargetView用自定义视图显示图片等

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值