最新消息:关注【已取消】微信公众号,可以获取全套资料,【全套Java基础27天】【JavaEE就业视频4个月】【Android就业视频4个月】

Android如何使用Glide4框架加载网络图片教程

Android 太平洋学习网 浏览 评论

Glide4是一个专门为Android开发的图片加载程序库,glide框架加载网络图片,本地图片,glide让显示圆角图片,圆形图片更加方便快捷,Google非常推荐Glide框架,它已经被用于许多Google开源程序,Glide已经添加了GIF支持以及图片的加载/缓存功能,下面来开始学习Glide吧!

一:添加Glide 4版本依赖

在app/build.gradle下添加Glide框架的依赖,代码如下:

implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

添加MyAppGlideModule.jar 配置文件,并继承于AppGlideModule 类,添加@GlideModule 注解,先不用添加任何内容,留一个空类,注意:Glide3版本不需要添加这种配置文件

import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;

// new since Glide v4
@GlideModule
public final class MyAppGlideModule extends AppGlideModule {
  // leave empty for now
}

编译一下我们的Android Studio之后,我们就可以使用GlideApp.with() 这种写法了,而在Glide3版本中,是这样写的:Glide.with() 

二:Glide4框架的基本使用

GlideApp.with(context)
    .load("http://via.placeholder.com/300.png")
    .into(imageView);

三:Glide框架的高级使用

1:重新设置图片大小,并加载图片

GlideApp.with(context)
    .load("http://via.placeholder.com/300.png")
    .override(300, 200)
    .into(imageView);

2:添加Placeholder占位图片和错误时显示的图片

GlideApp.with(context)
    .load("http://via.placeholder.com/300.png")
    .placeholder(R.drawable.placeholder)
    .error(R.drawable.imagenotfound)
    .into(imageView);

3:glide从中间裁剪图片

GlideApp.with(context)
    .load("http://via.placeholder.com/300.png")
    .centerCrop()
    .into(imageView);

四:配置Glide

MyAppGlideModule 中重写applyOptions方法

@GlideModule
public final class MyAppGlideModule extends AppGlideModule {
  @Override
  public void applyOptions(Context context, GlideBuilder builder) {
  
    // Glide default Bitmap Format is set to RGB_565 since it
    // consumed just 50% memory footprint compared to ARGB_8888.
    // Increase memory usage for quality with:
    // Glide默认是RGB_565图片格式,设置成ARGB_8888可以节约50%内存,提高内存使用效率
    builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888));
  }
}

五:Glide图片Transformations转换(此为扩展学习)

Glide图片转换需要使用第三方库,这个非常不错:glide-transformations,在app/build.gradle中加入此依赖,代码如下:

dependencies {
    implementation 'jp.wasabeef:glide-transformations:4.1.0'
    // If you want to use the GPU Filters
    implementation 'jp.co.cyberagent.android:gpuimage:2.0.4'
}

1:Glide设置圆角图片

int radius = 30; // corner radius, higher value = more rounded
int margin = 10; // crop margin, set to 0 for corners with no crop
GlideApp.with(this)
        .load("http://via.placeholder.com/300.png")
        .transform(new RoundedCornersTransformation(radius, margin))
        .into(ivImg);

2:Glide裁剪成圆形图片

GlideApp.with(this)
        .load("http://via.placeholder.com/300.png")
        .transform(new CircleCrop())
        .into(ivImg);

3:Glide添加模糊图片

GlideApp.with(this)
        .load("http://via.placeholder.com/300.png")
        .transform(new BlurTransformation())
        .into(ivImg);

Glide多重效果叠加,如Glide设置成模糊以及圆形图片

GlideApp.with(this)
        .load("http://via.placeholder.com/300.png")
        .transform(new MultiTransformation<Bitmap>(new BlurTransformation(25), new CircleCrop()))
        .into(ivImg);

六:动态调整ImageView图片大小

要ImageView在检索图像后重新调整大小,请首先定义一个SimpleTarget<Bitmap>对象以在Bitmap加载图像后对其进行拦截:

private SimpleTarget target = new SimpleTarget<Bitmap>() {  
    @Override
    public void onResourceReady(Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
        // do something with the bitmap
        // set it to an ImageView
        imageView.setImageBitmap(bitmap);
    }
};

通过 SimpleTarget 传给Glide的into()方法,代码如下:

GlideApp.with(context)
        .asBitmap()
        .load("http://via.placeholder.com/300.png")
        .into(target);

注意事项:

 SimpleTarget 对象必须作为一个方法或者属性,不能以匿名类的方式传入到into方法中,例如这样传入是错误的:GlideApp.with(this).load("url").into(new SimpleTarget<Bitmap>() { ... }) 

来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/android/1070.html
"文章很值,打赏犒劳作者一下"
微信号: Javaweb_engineer

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

与本文相关的文章

发表我的评论
取消评论

表情

您的回复是我们的动力!

  • 昵称 (必填)

网友最新评论