//获取图片的url
            String url = resultsEntity.getUrl();
            //判断获取的图片是否存在
            if (resultsEntity.getItemHeight() > 0) {
                ViewGroup.LayoutParams layoutParams = viewHolder.rlRoot.getLayoutParams();
                layoutParams.height = resultsEntity.getItemHeight();
            }
            //设置默认的图片    
            viewHolder.image.setImageResource(R.drawable.pic_gray_bg);
            RequestOptions options = new RequestOptions();
            //设置图片的展示样式
            options.fitCenter();
            //设置备用图片的展示
            options.placeholder(R.drawable.pic_gray_bg);
            //设置缓存
            options.diskCacheStrategy(DiskCacheStrategy.ALL);
            //Glide加载图片
            Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .apply(options)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                            int width = resource.getWidth();
                            int height = resource.getHeight();
                            //计算高宽比
                            int finalHeight = (screenWidth / 2) * height / width;
                            if (resultsEntity.getItemHeight() <= 0) {
                                resultsEntity.setItemHeight(finalHeight);
                                ViewGroup.LayoutParams layoutParams = viewHolder.rlRoot.getLayoutParams();
                                layoutParams.height = resultsEntity.getItemHeight();
                            }

                            viewHolder.image.setImageBitmap(resource);
                        }
                    });

 

posted on 2017-09-25 19:02  从前有個人  阅读(142)  评论(0编辑  收藏  举报