Glide显示图片高度自适应

首先,定义ImageView,在该ImageView中,我们需要设置属性android:adjustViewBounds="true",他的意思图片是否保持宽高比。切记的一点是该属性需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果

<ImageView  
       android:id="@+id/img_list"  
       android:layout_width="fill_parent"  
       android:layout_height="wrap_content"  
       android:scaleType="fitXY"  
       android:adjustViewBounds="true"  
       android:src="@drawable/load_default_img" />  

剩下最主要的功能就是动态设置 ImageView 容器的高度

 Glide.with(mContext).load(item.getCoverfile_name()).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();
                    ViewGroup.LayoutParams para = photo.getLayoutParams();


                    int maxHeight = XiurenUtils.dip2px(mContext, 400);
                    int height = (int) ((float) photo.getWidth()/imageWidth * imageHeight);
                    if (height > maxHeight) height = maxHeight;
                    para.height = height;
                    photo.setLayoutParams(para);
                    Glide.with(mContext).load(item.getCoverfile_name()).asBitmap().into(photo);
                }
            });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容