Android实现图片宽度全屏,高度随图片大小动态适配

xiaoxiao2021-02-28  112

1.layout中设置图片宽度match_parent 高度wrap_content 2.获得图片的高度,可以让后台直接传过来,也可以拿到图片后在移动端自行获得 3.计算图片宽高比(注意应为float型),以及屏幕的宽度 用屏幕宽度/宽高比得到图片应设置的高度 4.得到ImageView控件的LayoutParams,重新设置params int imgWidth=Integer.valueOf(jsonArray.getJSONObject(0).getString("w")); int imgHeight=Integer.valueOf(jsonArray.getJSONObject(0).getString("h")); screenWidth=ScreenUtils.getScreenWidth(mContext); float ratio=(float) imgWidth/(float) imgHeight; height=(int)(screenWidth/ratio); ViewGroup.LayoutParams params=photo.getLayoutParams(); params.height=height; params.width=screenWidth; photo.setLayoutParams(params); 获取屏幕宽度: /** * 获取屏幕宽度(px) */ public static int getScreenWidth(Context context) { return context.getResources().getDisplayMetrics().widthPixels; }
转载请注明原文地址: https://www.6miu.com/read-49317.html

最新回复(0)