Picture loading using analytical framework Glide and advanced usage

Use analytical framework Glide image loading and advanced usage

1.Glide Project

2. The general picture loading method and Glide contrast image loading

3.Glide the advanced use of (a)

4.Glide advanced use (b)

5.Glide using common methods

6.RequestOptions common configuration

.
.
1.Glide Project

Glide Andrews in not a new technology, but it is relatively hot image loading frame used. On GitHuB have 28.2K stars also reflects the love of the majority of developers we want to use Glide Glide, then need to add the following code in the dependent library:

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

Here Insert Picture Description

2. The general picture loading method and Glide contrast image loading

2.1 First, write a layout with only one button and a ImageView and then bound controls in Activity, the opening of the basic operation of the network permissions.
2.2 Load images on the Web with an ordinary method

  /*
    用普通方法加载网络中的图片给共分三步
    1.找到图片地址
    2.根据图片地址转化为可被加载的对象
    3.通过imageView来加载图片
     */
    private void loadImage(final String img)
    {
        //加载网络图片是耗时操作,需要新开线程
        new Thread()
        {
            @Override
            public void run() {
                super.run();
                Message message = Message.obtain();//从消息池取出的message
                try {
                    //使用HttpURLConnection进行网络请求
                    URL url = new URL(img);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    int code = httpURLConnection.getResponseCode();
                    //如果请求成功
                    if (code == 200)
                    {
                        InputStream inputStream = httpURLConnection.getInputStream();
                        //将inputstream转换为可被加载的BitMap对象
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        message.what = 200;
                        message.obj = bitmap;

                    }
                    else
                    {
                        message.what = code;
                    }
                    
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                finally {
                    handler.sendMessage(message);//使用Handler将message发送出去
                }
            }
        }.start();

    }

//在Handlerzhong 


 private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 200:
                {
                    //请求成功
                    Bitmap bitmap = (Bitmap) msg.obj;
                    imageView1.setImageBitmap(bitmap);
                    break;
                }
                default:
                {
                    break;
                }
            }
        }
    };

2.3 Glide load images on the Web

    /*
    1.调用.with方法来创建图片加载实例
    2.调用.load方法选择图片来源
    3.调用.into方法来配置图片载体
     */
    private void glideLoadImage(String url)
    {
    //这段代码对图片进行了处理,可以不用使用
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)//加载网络图片是显示的图片
                .error(R.mipmap.ic_launcher)//请求出错的时候显示的图片
                .circleCrop();//将图片变为圆形

//一般情况下只用下方的代码即可完成加载图片的需求
        Glide.with(this)
                .load(url)
               .apply(options)
               .into(imageView1);
    }

By comparing the two pieces of code we can see at a glance the use of a code of Glide becomes more simple and concise.

3.Glide advanced use (a)
In the above code we use to configure the RequestOptions loaded picture. But if each picture loaded once wrote a RequestOptions too much trouble, so we need to RequestOptions encapsulation.

public class GlideOptionsUtils
{
    //生成一个基本的RequestOptions
    public static RequestOptions baseOptionss()
    {
        return new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher);

    }
    //对基本的RequestOptions进行个性化处理
    public static RequestOptions circleOptions()
    {
        return baseOptionss().circleCrop();
    }
}

Then we can be slightly modified in the same place Glide used:

    private void glideLoadImage(String url)
    {
        //这段代码对图片进行了处理,可以不用使用
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)//加载网络图片是显示的图片
                .error(R.mipmap.ic_launcher)//请求出错的时候显示的图片
                .circleCrop();//将图片变为圆形

//一般情况下只用下方的代码即可完成加载图片的需求
        Glide.with(this)
                .load(url)
     ****           .apply(GlideOptionsUtils.circleOptions())
                .into(imageView1);
    }

If there is new demand after another as long as baseOptions processing, can generate a new approach.

Advanced use 4.Glide (b)
Glide gives us the Generated API to process the images.
4.1
write a class inherits from AppGlideModule, and notes with @GlideModule

/*
生成GlideApp对象
 */
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
}

4.2

@GlideExtension
public class MyGlideEctension
{
    private MyGlideEctension(){}


    /**
     * 全局统一配置
     * @param options
     */
    @GlideOption
    public static BaseRequestOptions<?> miniThumb(BaseRequestOptions<?> options) {
        return options .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .circleCrop();
    }
}

4.3 Use

 /**
     * 通过GlideAPP加载网络图片
     */

    private void glideAppLoadUrlImage(String img)
    {
        GlideApp.with(this)
                .load(img)
                .miniThumb()
                .into(imageView1);
    }

5.Glide common method

  Glide.with(this)//此方法返回RequestManager对象
        .load(url)//返回RequestBuilder对象
        .apply(options)//设置Reauestion
        .thumbnail(Glide.with(this)//加载缩略图,如果目标图片先于缩略图加载,则缩略图停止加载
                        .load(Thumbnailurl))
        .error(Glide.with(this)//加载缩略图,如果目标图片先于缩略图加载,则缩略图停止加载
                        .load(errorImageUrl))//加载失败时,执行新的加载
        .transition(GenericTransitionOptions.<Drawable>with(R.anim.fade))//占位图到主图的的动画效果,比较损耗性能
        .listener(new RequestListener<Drawable>() {
        //监听图片加载
        返回fals表示没有被处理,会向下传递。
        返回true表示已经被处理,不会向下传递 
            @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(imageView1);//设置目标容器

6.RequestOptions common configuration

   RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_launcher)//加载网络图片显示的占位图
                .error(R.mipmap.ic_launcher)//请求出错的时候显示的图片
                .circleCrop();//将图片变为圆形
                .timeout(5*1000)//图片加载超时时间
                .override(width,height);

Glide is probably about the use of these, I will upload the follow-up Demo for your reference.

Demo in here

Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41525021/article/details/104187279