1. 程式人生 > >安卓中獲取ImageView等控制元件的寬高

安卓中獲取ImageView等控制元件的寬高

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;