图片加载框架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用自定义视图显示图片等

猜你喜欢

转载自blog.csdn.net/zb52588/article/details/81103049