Android宽度填满,高度自适应

网上说的设置fitxy center啥都不适用了,我已经尝试全。只能用最原始的办法来实现这个功能。

首先布局文件就写个imageview,宽高都设置成wrap_content即可,别的属性不需要,接着就是测量绘制工作。

int screenWidth =getScreenWidth(mContext) - ViewUtils.dip2px(mContext, 30);  //顶格的宽度

Glide.with(mContext).asBitmap().load(bean).into(new BitmapImageViewTarget(picHolder.image){

@Override

    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition transition) {

super.onResourceReady(resource, transition);

       //1、使用Glide下载图片转成Bitmap,并获取原始图片的宽高:

        width = resource.getWidth();

        height = resource.getHeight();

      //2、修改宽度为屏幕宽度(一般要减去两边空白距离,此案例左右留白15dp,然后高度等比缩放)

        ViewGroup.LayoutParams lp =imageView.getLayoutParams();

        lp.height =height *screenWidth/width;   //高度等比缩放

        lp.width =screenWidth;

        imageView.setLayoutParams(lp);

    }

});

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容