简单实现ImageView宽度填满屏幕,高度自适应的两种方式

方法一:
android:id="@+id/zb_piclist_item_iamge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"
android:visibility="gone" />

//获取屏幕宽度
WindowManager m = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
m.getDefaultDisplay().getMetrics(outMetrics);

        int width = outMetrics.widthPixels - dp2px(mContext, 10f) * 2; 

//乘以2是因为左右两侧的宽度
//bobaoArcWidth bobaoArcHeight 原始图片宽高
int height = (int) (width / (Float.parseFloat(bobaoArcWidth)) * (Float.parseFloat(bobaoArcHeight)));
//设置图片参数
ViewGroup.LayoutParams layoutParams = viewHolder.zbPiclistItemIamge.getLayoutParams();
// layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.width = width;
layoutParams.height = height;
viewHolder.zbPiclistItemIamge.setLayoutParams(layoutParams);

        GlideUtils.glideFragment(mContext, bobaoArcPic, viewHolder.zbPiclistItemIamge);

方发二

public class ResizableImageView extends ImageView {

public ResizableImageView(Context context) {  
    super(context);  
}  

public ResizableImageView(Context context, AttributeSet attrs) {  
    super(context, attrs);  
}  

@Override  
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){  
    Drawable d = getDrawable();  

    if(d!=null){  
        // ceil not round - avoid thin vertical gaps along the left/right edges  
        int width = MeasureSpec.getSize(widthMeasureSpec);  
        //高度根据使得图片的宽度充满屏幕计算而得  
        int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());  
        setMeasuredDimension(width, height);  
    }else{  
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
    }  
}  

}

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

  

Three

根据图片的宽度,高度,来按比例显示

final String app_arc_pic = list.get(position).ad_pic;
String adPicHeight = list.get(position).ad_pic_height;
String adPicWidth = list.get(position).ad_pic_width;
int adPicH = Integer.parseInt(adPicHeight);
int adPicW = Integer.parseInt(adPicWidth);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();

    int height = width * adPicH / adPicW;
    ViewGroup.LayoutParams para = imageView.getLayoutParams();
    para.height = height;
    imageView.setLayoutParams(para);

    LogUtils.e(TAG , "============拿到的app_arc_pic==========" +app_arc_pic);
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Glide.with(activity)
                    .load(app_arc_pic)
                    .placeholder(R.mipmap.zhan_message_tozhi_new)
                    .error(R.mipmap.zhan_message_tozhi_new)
                    .into(imageView);
        }
    });

你可能感兴趣的:(简单实现ImageView宽度填满屏幕,高度自适应的两种方式)