您的位置:首页 > 其它

如何让ImageView高度和宽度保持一致?

2016-05-20 09:34 627 查看

问题描述

不设置具体数字的高宽,如何让宽高值保持一致?

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"/>


也就是说:

竖屏下,height=width;

宽屏下,width=height;

解决方案

扩展ImageView控件,重写ImageView的onMeasure方法

int squareDim = 1000000000;
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int h = this.getMeasuredHeight();
int w = this.getMeasuredWidth();
int curSquareDim = Math.min(w,h);
if(curSquareDim < squareDim)
{
squareDim = curSquareDim;
}
Log.d("MyApp", "h "+h+"w "+w+"squareDim "+squareDim);
setMeasuredDimension(squareDim, squareDim);
}


参考:http://stackoverflow.com/questions/9798392/imageview-have-height-match-width
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: