Java ImageView.getWidth() 返回 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19271609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:36:27  来源:igfitidea点击:

ImageView.getWidth() returns 0

javaandroidandroid-activityandroid-imageview

提问by Geek

I get imageView's width 0. Below is code.

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

xml file :

xml文件:

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

Activity :

活动 :

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

I don't want to use below code as I need to place this code in so many places.

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

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;
        }
    });

My workaround :I actually later did not need to add this code in an activity. I added it to a non-activity class and instead of ImageView's width I used Bitmap image's width and now does not have this problem.

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

采纳答案by Sanket Kachhela

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.

你在哪里打电话getWidth()getHeight()在 ImageView 上?如果您从onCreate()活动中调用,它将不起作用。您需要等待活动窗口附加,然后调用getWidth() and getHeight()ImageView。您可以尝试getWidth() and getHeight()onWindowFocusChanged()活动的方法中调用。

call on onWindowFocusChanged like this way

像这样调用 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());
     }

    }

回答by linakis

Since you already have a "fixed" width imageView of 200dp why don't you just put it in dimens.xml

既然你已经有一个 200dp 的“固定”宽度 imageView 为什么不把它放在 dimens.xml 中

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

and then simply

然后简单地

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

回答by M.ElSaka

well i found many many questions related that are duplicate to this question, and most of the answers were not fit for my case.

好吧,我发现许多与此问题相关的问题与此问题重复,并且大多数答案都不适合我的情况。

I have come up with a solution that may fit for yours as it does for me. Simply, i have created a custom ImageViewand added a new callback OnSizeChangedCallbackthat is being called whenever the size of the view is changed to non zero values.

我想出了一个可能适合你和我一样的解决方案。简单地说,我创建了一个自定义ImageView并添加了一个新的回调OnSizeChangedCallback,每当视图的大小更改为非零值时都会调用该回调。

Here's a code sample of my own:

这是我自己的代码示例:

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);
    }
}

and you can simply use it as follows:

你可以简单地使用它如下:

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 ...
    }
});

related questions:

相关问题:

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