找到动态创建的Imageview的高度和宽度?

时间:2014-11-05 13:28:05

标签: android android-imageview android-image

  1. LayoutParams params = new FrameLayout.LayoutParams( LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
  2. ImageView的     mainimage = new ImageView(HLActivity.this);     mainimage.setLayoutParams(PARAMS);

    loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
    

    我已经尝试过mainimage.getwidth()和mainimage.getHeight();

    现在我想知道之后ImageView的orignalwidth和高度     将图像放在imageview中。能帮帮我吗。


1 个答案:

答案 0 :(得分:0)

getWidth()getHeight()确实是您正在寻找的,但是当您还没有测量视图时,您正试图询问宽度和高度。为了解决这个问题,我们向ImageView发布了一条消息,该消息将在测量完成后执行。

LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// mark it as final, so it can be accessed in the runnable.
final imageView mainimage = new ImageView(HLActivity.this);
mainimage.setLayoutParams(params);
mainimage.post(new Runnable() {

    @Override
    public void run() {
       // get the width and the height
       int width = mainimage.getWidth();
       int height = mainimage.getHeight();
       // do what you want to do with the sizes.
       loader.DisplayImage(_pageslist.get(j).getUrl(), mainimage);
    }
});