android layout的高度自适应,简单实现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">

android:id="@+id/image"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:src="@drawable/test"

android:background="#FF0"/>

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);

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值