Glide学习笔记(一)

Glide介绍

glide是一个被google推荐的图片加载框架,glide被广泛应用在google的开源框架中。目前glide的最新release版本为4.12.0,本文使用的是4.9.0的版本。

Glide的使用

使用glide加载图片

在模块的gradle文件中引入glide的依赖。

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.9.0'
}
MainActivity.java

使用glide加载图片非常简单,一般只需要一行代码。我们只需要给with方法传入上下文,给load方法传入图片的url,给into方法传入显示图片的控件,即可显示图片。

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.imageview);
        loadImageWithGlide("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.daimg.com%2Fuploads%2Fallimg%2F111111%2F3-1111111I623563.jpg&refer=http%3A%2F%2Fimg.daimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1634137192&t=dcc4de13dff2354143489110c40e34ea");

    }

    public void loadImageWithGlide(String url) {
        Glide.with(this)   //上下文
                .load(url)  //需要加载的图片的链接
                .into(mImageView);  //显示图片的控件
    }

}
布局文件

布局文件非常简单,只有一个用来显示图片的ImageView。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/imageview"/>

</RelativeLayout>
结果

在这里插入图片描述

glide的常用配置

如果想要在加载图片的过程中显示一张其它的图片或者在加载失败的时候显示加载失败的其它图片,我们该怎么做呢?别怕,glide已经帮我们准备好了,使用RequestOptions类即可完成这些功能。

MainActivity
public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.imageview);
		loadImageWithGlide("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.daimg.com%2Fuploads%2Fallimg%2F111111%2F3-1111111I623563.jpg&refer=http%3A%2F%2Fimg.daimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1634137192&t=dcc4de13dff2354143489110c40e34ea");

    }

    public void loadImageWithGlide(String url) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.drawable.ic_launcher_background)  //加载中显示的图片
                .error(R.drawable.ic_launcher_foreground)       //加载错误时显示的图片
                .fallback(R.drawable.ic_launcher_foreground)  //当请求的url为null时显示的图片
                .circleCrop();      //圆角图片

        Glide.with(this)
                .load(url)
                .apply(options)
                .into(mImageView);
       
       //这里要说明一点,其实不使用RequestOptions也可以完成这些功能,如下代码所示:
      /* Glide.with(this)
       .load(url)
       .placeholder(R.drawable.ic_launcher_background)
       .error(R.drawable.ic_launcher_foreground)
       .circleCrop()
       .into(mImageView); */
    }

}

布局文件

布局文件和之前的相同

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/imageview"/>

</RelativeLayout>
结果

在这里插入图片描述

glide的常用方法
preload()

此方法用于预加载图片,是一个重载方法,不带参数的表示加载图片的原始大小,带参数的表示加载指定大小的图片。

MainActivity
public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.imageview);

        loadImageWithGlide("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.daimg.com%2Fuploads%2Fallimg%2F111111%2F3-1111111I623563.jpg&refer=http%3A%2F%2Fimg.daimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1634137192&t=dcc4de13dff2354143489110c40e34ea");

    }

    public void loadImageWithGlide(String url) {
        Glide.with(this)
                .load(url)
               .listener(new RequestListener<Drawable>() {
                   @Override
                   public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                       Log.d(TAG, "onLoadFailed: ");
                       return false;
                   }

                   @Override
                   public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                       Log.d(TAG, "onResourceReady: ");
                       mImageView.setImageDrawable(resource);
                       return false;
                   }
               }).preload();
    }
}
submit()

用于下载图片,也是重载方法,不带参数的表示下载图片的原始大小,带参数的表示下载指定大小的图片。

MainActivity
public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.imageview);
     loadImageWithGlide("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.daimg.com%2Fuploads%2Fallimg%2F111111%2F3-1111111I623563.jpg&refer=http%3A%2F%2Fimg.daimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1634137192&t=dcc4de13dff2354143489110c40e34ea");

    }


    public void loadImageWithGlide(String url) {
        FutureTarget<File> submit = Glide.with(this)
                .asFile()      //指定加载格式
                .addListener(new RequestListener<File>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
                        Log.d(TAG, "onLoadFailed: ");
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
                        Log.d(TAG, "onResourceReady: " + resource.getAbsolutePath());
                        return false;
                    }
                })
                .load(url)
                .submit();


       new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    File file  = submit.get();
                    Log.d(TAG, "run: " + file.getAbsolutePath());
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
}
布局文件

布局文件和之前的相同

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/imageview"/>

</RelativeLayout>
结果

红色文件就是下载后的图片。
在这里插入图片描述

addListener

添加监听。

例子
MainActivity
public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = findViewById(R.id.imageview);
     loadImageWithGlide("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.daimg.com%2Fuploads%2Fallimg%2F111111%2F3-1111111I623563.jpg&refer=http%3A%2F%2Fimg.daimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1634137192&t=dcc4de13dff2354143489110c40e34ea");

    }


    public void loadImageWithGlide(String url) {
        FutureTarget<File> submit = Glide.with(this)
                .asFile()      //指定加载格式
                .addListener(new RequestListener<File>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
                        Log.d(TAG, "onLoadFailed: ");
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
                        Log.d(TAG, "onResourceReady: " + resource.getAbsolutePath());
                        return false;
                    }
                })
                .load(url)
                .submit();


       /* new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    File file  = submit.get();
                    Log.d(TAG, "run: " + file.getAbsolutePath());
                } catch (ExecutionException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();*/

    }
}
布局文件

布局文件和之前的相同

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/imageview"/>

</RelativeLayout>

参考

  1. Glide官网
  2. Glide使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值