ImageView.getWidth()返回0

时间:2013-10-09 12:09:29

标签: java android android-activity android-imageview

我得到imageView的宽度0.下面是代码。

xml文件:

<ImageView
        android:id="@+id/img"
        android:layout_width="200dp"
        android:layout_height="200dp" />

活动:

@Override
protected void onResume() {
    super.onResume();
    ImageView img = (ImageView) findViewById(R.id.img);
    Log.d(TAG, "width : " + img.getWidth());
}

我不想使用下面的代码,因为我需要将这些代码放在很多地方。

ViewTreeObserver vto = img.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            ImageView img = (ImageView) findViewById(R.id.img);
                    Log.d(TAG, "width : " + img.getWidth());
            return true;
        }
    });

我的解决方法: 我实际上后来不需要在活动中添加此代码。我将它添加到非活动类,而不是ImageView的宽度,我使用了Bitmap图像的宽度,现在没有这个问题。

5 个答案:

答案 0 :(得分:38)

您在ImageView上调用getWidth()getHeight()的位置?如果您在活动中致电onCreate(),则无法使用。您需要等待附加活动窗口,然后在ImageView上调用getWidth() and getHeight()。您可以尝试从活动的getWidth() and getHeight()方法中调用onWindowFocusChanged()

以这种方式调用onWindowFocusChanged

public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
     if (hasFocus) {
        ImageView img = (ImageView) findViewById(R.id.img);
        Log.d(TAG, "width : " + img.getWidth());
     }

    }

答案 1 :(得分:9)

由于你已经拥有200dp的“固定”宽度imageView,为什么不把它放在dimens.xml中

<dimen name="image_width">200dp</dimen>

然后简单地

int width = (int) getResources().getDimension(R.dimen.image_Width)

答案 2 :(得分:3)

我发现许多相关的问题都与这个问题重复,而且大部分答案都不适合我的案例。

我已经提出了一个适合你的解决方案,就像它对我一样。简单地说,我创建了一个自定义ImageView并添加了一个新的回调OnSizeChangedCallback,只要视图的大小更改为非零值,就会调用它。

以下是我自己的代码示例:

public class AvatarImageView extends ImageView{

    private OnImageViewSizeChanged sizeCallback = null;

    public AvatarImageView(Context context) {
        super(context);
    }

    public AvatarImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AvatarImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (w == 0 || h == 0) {
            return;
        }
        else{
            if(sizeCallback != null)
                sizeCallback.invoke(this, w, h);
        }

    }

    public void setOnImageViewSizeChanged(OnImageViewSizeChanged _callback){
        this.sizeCallback = _callback;

        if (getWidth() != 0 && getHeight() != 0) {
            _callback.invoke(this, getWidth(), getHeight());
        }
    }

    public interface OnImageViewSizeChanged{
        public void invoke(ImageView v, int w, int h);
    }
}

您可以按如下方式使用它:

final AvatarImageView avatarIcon = (AvatarImageView) findViewById(R.id.avatarImg);
avatarIcon.setOnImageViewSizeChanged(new AvatarImageView.OnImageViewSizeChanged() {
    @Override
    public void invoke(ImageView v, final int w, final int h) {
            // Do whatever you want with w and h which are non zero values ...
    }
});

相关问题:

  1. views-getwidth-and-getheight-returning-0
  2. android-imageview-return-getwidth-getheight-zero
  3. android-get-width-returns-0

答案 3 :(得分:1)

您需要等待ImageView膨胀。尝试OnLayoutChangeListener

        imageView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
            //
            if (imageView.getWidth() > 0 && imageView.getHeight() > 0) {
                // put your code here...
            }
        });

答案 4 :(得分:0)

如果xml中提到了imageview的维度,那么可以通过编程方式找到

imageview.getLayoutParams().width

如果xml中提到的imageview宽度是包裹内容并且直到在UI上没有膨胀那么它返回0