安卓中获取ImageView等控件的宽高

时间:2022-02-05 22:41:58

1.获取ImageView控件的实际长度和宽度:

mSuperMan = ((ImageView) findViewById(R.id.superman));

int i = View.MeasureSpec.makeMeasureSpec(0, 0);
int j = View.MeasureSpec.makeMeasureSpec(0, 0);
mSuperMan.measure(i, j);
mSuperManWidth = mSuperMan.getMeasuredWidth();//图片的实际大小
mSuperManHeight = mSuperMan.getMeasuredHeight();


2.获取ImageView控件的设置后布局的长度和宽度:

LayoutParams para;
para = mSuperMan.getLayoutParams();

mSuperManWidth = para.width;

mSuperManHeight =para.height;