Glide4.7.1的简单使用,注意事项

/**
 * 创建日期:2018/11/7 on 11:58
 * 描述: glide使用工具类
 */
public class GlideUtil {

    public static void showImg2ImageView(final Context context, final String url, final ImageView imageView, final Handler handler){

        final RequestOptions options = new RequestOptions();
        options.skipMemoryCache(false);
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.priority(Priority.HIGH);
        options.error(R.mipmap.home_img_loading_pic1);
        options.placeholder(R.mipmap.home_img_loading_pic1);
        Glide.with(context).load(url)
                .apply(options)
                .thumbnail(0.1f)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        String message = e.getMessage();
                        LogUtil.i("e====="+e);
                        //有些图片加载失败时加载第二次
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Glide.with(context)
                                        .load(url)
                                        .apply(options)
                                        .into(imageView);
                            }
                        });


                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(imageView);
    }


    public static void showImg2ImageView(final Context context, final String url, final ImageView imageView, final Handler handler,int errorId,int placeholderId){

        final RequestOptions options = new RequestOptions();
        options.skipMemoryCache(false);
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.priority(Priority.HIGH);
        options.error(errorId);
        options.placeholder(placeholderId);
        Glide.with(context).load(url)
                .apply(options)
                .thumbnail(0.1f)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        String message = e.getMessage();
                        LogUtil.i("e====="+e);
                        //有些图片加载失败时加载第二次
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Glide.with(context)
                                        .load(url)
                                        .apply(options)
                                        .into(imageView);
                            }
                        });
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(imageView);
    }


    public static void showImg2ImageView(final Context context, final String url, final ImageView imageView){

        final RequestOptions options = new RequestOptions();
        options.skipMemoryCache(false);
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.priority(Priority.HIGH);
        options.error(R.mipmap.home_img_loading_pic1);
        options.placeholder(R.mipmap.home_img_loading_pic1);
        Glide.with(context).load(url)
                .apply(options)
                .into(imageView);
    }

    public static void getImageColor(final Context context, final String url, final ImageView imageView){

        Glide.with(context)
                .asBitmap().load(url)
                .into(new BitmapImageViewTarget(imageView){
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        PaletteUtil.getImageColor(resource,imageView);
                    }
                });

    }

}

注意事项:
1.设置一些配置的时候尽量使用

final RequestOptions options = new RequestOptions();
        options.skipMemoryCache(false);
        options.diskCacheStrategy(DiskCacheStrategy.ALL);
        options.priority(Priority.HIGH);
        options.error(R.mipmap.home_img_loading_pic1);
        options.placeholder(R.mipmap.home_img_loading_pic1);

但是在调用的使用一定要认清调用apply(options)方法。如果在使用的时候图片没有显示连设置的各种占位图都没有,很大可能就是调用的方法出错,因为之前版本的方法不一样。(我自己经历的血的教训)

猜你喜欢

转载自blog.csdn.net/baidu_21345205/article/details/84345611
今日推荐