Glide 加载图片 宽度固定 等比缩放

来源:互联网 发布:淘宝天下 邀请 小二 编辑:程序博客网 时间:2024/06/05 09:03

Glide 加载图片 宽度固定 等比缩放

问题描述,由于图片来自第三方,尺寸很是不确定,而且为了美观要求宽度固定(屏幕宽度).
因此要求图片能够等比例缩放,知道宽度满足调节为止

解决办法

 Glide.with(context).load(imageUrls.get(position).getImgurl()).asBitmap()                .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {                    @Override                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {                        int imageWidth = resource.getWidth();                        int imageHeight = resource.getHeight();                        int width = ScreentUtils.getScreenWidth(context);//固定宽度                        //宽度固定,然后根据原始宽高比得到此固定宽度需要的高度                        int height = width * imageHeight / imageWidth;                        ViewGroup.LayoutParams para = holder.image.getLayoutParams();                        para.height = height;                        para.width = width;                        holder.image.setImageBitmap(resource);                    }                });

最终效果

如下

原创粉丝点击