2015年1月14日 星期三

How to get height and width value of ImageView

Where you calling getWidth() and getHeight() on ImageView?

If you calling from onCreate() in activity, it won't work. 
You need to wait for activity window to attached and then call getWidth() and getHeight() on ImageView.
You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.


@Override
public void onWindowFocusChanged(boolean hasFocus)
{
 // TODO Auto-generated method stub
 super.onWindowFocusChanged(hasFocus);
 initUI(); 
}
 
private void initUI()
{
 m_imgView = (ImageView) findViewById(R.id.idImgLoginPic);
 textMobile = (TextView) findViewById(R.id.idMobileText);
 textMobile2 = (TextView) findViewById(R.id.idMobileText2);
 tmLinearLayout = (LinearLayout) findViewById(R.id.idLinearLayout1);
 tmLinearLayout2 = (LinearLayout) findViewById(R.id.idLinearLayout2);
  
 WIDTH_HEIGHT[0] = m_imgView.getWidth();
 WIDTH_HEIGHT[1] = m_imgView.getHeight();
 WIDTH_HEIGHT[2] = tmLinearLayout.getWidth();
 WIDTH_HEIGHT[3] = tmLinearLayout.getHeight();
}

ref : Here

沒有留言:

張貼留言