美文网首页
Glide4.7.1的简单使用,注意事项

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

作者: jxtx | 来源:发表于2018-11-22 17:26 被阅读102次
    /**
     * 创建日期: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)方法。如果在使用的时候图片没有显示连设置的各种占位图都没有,很大可能就是调用的方法出错,因为之前版本的方法不一样。(我自己经历的血的教训)

    -----------------2018.11.27--------------------------
    问题:在glide配置中使用options.transforms(new GlideRoundedCornersTransform(context,5));来实现圆角以及centerCrop效果时,刷新界面会出现闪烁
    解决方法:由于glide默认采用了centerCrop方式显示图片,所以基本不需要再次设置,而圆角采用自定义控件的方式就可以实现其效果并不会出现闪烁问题

    public class GlideRoundTransform extends CenterCrop {
    
        private static float radius = 10f;
    
        public GlideRoundTransform(Context context) {
            this(context, 10);
        }
    
        public GlideRoundTransform(Context context, int dp) {
            super();
            this.radius = UnitConversionUtil.dip2px(context, dp);
        }
    
        @Override
        protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
            //glide4.0+
            Bitmap transform = super.transform(pool, toTransform, outWidth, outHeight);
            return roundCrop(pool, transform);
            //glide3.0
            //return roundCrop(pool, toTransform);
        }
    
        private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
            if (source == null) return null;
    
            Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            if (result == null) {
                result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }
    
        public String getId() {
            return getClass().getName() + Math.round(radius);
        }
    
        @Override
        public void updateDiskCacheKey(MessageDigest messageDigest) {
    
        }
    }
    

    相关文章

      网友评论

          本文标题:Glide4.7.1的简单使用,注意事项

          本文链接:https://www.haomeiwen.com/subject/vytrqqtx.html