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

2016-01-16  本文已影响17248人  WiiHuu

两种方式

1.重写View的onMeasure方法参考这里easion_zms的专栏

核心代码

protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){

Drawable d = getDrawable();if(d!=null){// ceil not round - avoid thin vertical gaps along the left/right edgesintwidth = MeasureSpec.getSize(widthMeasureSpec);//高度根据使得图片的宽度充满屏幕计算而得intheight = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());

setMeasuredDimension(width, height);

}else{super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

2.设置ImageView的属性:

//宽度填满屏幕

android:layout_width=”match_parent”

android:scaleType=”fitXY”

android:layout_height=”wrap_content”

//保持比例,一定要设置

android:adjustViewBounds=”true”

上一篇 下一篇

猜你喜欢

热点阅读