设为首页 加入收藏

TOP

Android 图片加载框架 Glide4.x(一)
2019-09-01 23:13:57 】 浏览:62
Tags:Android 图片 加载 框架 Glide4.x

概述

Glide是一个图片加载框架,使得我们可以轻松的加载和展示图片

Glide4.x新增apply()来进行设置,apply可以调用多次,但是如果两次apply存在冲突的设置,会以最后一次为准

新增RequestOptions对象,用来存放设置

添加依赖

 implementation 'com.github.bumptech.glide:glide:4.4.0' //添加支持
 annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0' //Glide4.x以上需要添加的支持

//如果需要加载网络图片,需要网络权限
<uses-permission android:name="android.permission.INTERNET" />

Gilde的图片加载

基本实例

Glide.with(this).load(url).into(imageView);

with() 是初始化Glide的实例

load() 是加载URL地址的图片

into() 是指定显示的控件

加载资源

Glide.with(this).load("https://www.baid.com/a.jpg").into(imageView);//加载网络图片

Glide.with(this).load(uri).into(imageView);//加载URL图片

Glide.with(this).load(R.mipmap.ic_launcher).into(imageView); //加载Resources图片

Glide.with(this).load(context.getResources().getAssets().open("a.png")).into(imageView); //加载assets图片

Glide.with(this).load(file).into(imageView);//加载File图片

Glide.with(this).load(byte[]).into(imageView); //加载二进制流图片

指定加载静态/动态图片

Gilde默认支持gif图片,但也可以选择指定加载gif动态图片或是静态图片

//asBitmap 指定加载静态图片,如果加载gif图片将会显示第一帧
Glide.with(this).asBitmap().load(imageUrl).into(imageView);

//asGif 指定加载动态图片,如果加载静态图片将会显示加载失败或者显示异常时设定的占位图片
Glide.with(this).asGif().load(imageUrl).into(imageView);

//Glide4.x以上新增.asFile()和.asDrawable(),强制使用文件格式和Drawable格式的加载,与3.x不同,4.x需要先设置加载格式,再load,不然会报错!

//不允许加载网络视频,但是可以加载本地视频
Glide.with(context).load(Uri.fromFile(new File( filePath))).into(imageView);

设置图片大小&使用RequestOptions对象存放设置

Glide不会直接将图片的完整尺寸全部加载到内存中,而是用多少加载多少,所以Glide会自动根据ImageView的大小来决定图片的大小,但是也可以指定加载图片的大小(view的宽高设定为wrap_content才可以指定尺寸):

RequestOptions options = new RequestOptions();
options1.override(100, 100); //设置加载的图片大小

Glide.with(this)
        .load(url)
        .apply(options)
        .into(imageView);

设置占位图

占位图是在加载未完成或者加载失败,为避免空白而设置的图片

RequestOptions options = new RequestOptions();
options.placeholder(R.drawable.ic_launcher_background); //设置加载未完成时的占位图
options.error(R.mipmap.load_error); //设置加载异常时的占位图

Glide.with(this) .load(url) .apply(options) .into(imageView);

Glide加载监听

Glide.with(this).load(url)
        .listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                //图片加载失败调用
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                //图片加载完成时调用
                return false;
            }
        })
        .into(iv);

Glide的回调方法

SimpleTarget对象

SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {
    @Override
    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
        //这里可以做复杂的图片变换处理,如下只是简单的显示在imageView上
        imageView.setImageDrawable(resource);
    }
};

public void loadImage(View view) {
    Glide.with(this)
         .load(url)
         .into(simpleTarget);
}

preload预加载

提前对图片进行一个预加载,等真正需要加载图片的时候就直接从缓存中读取

Glide.with(this)
     .load("http://www.baidu.com/a.png")
     .preload(); //预加载图片

Glide.with(this)
     .load("http://www.baidu.com/a.png")
     .into(i
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android使用Glide加载https链接的.. 下一篇Login case

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目