ImageView.getWidth() returns 0

前端 未结 4 1153
误落风尘
误落风尘 2020-12-08 21:28

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

xml file :



        
相关标签:
4条回答
  • 2020-12-08 21:34

    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.

    call on onWindowFocusChanged like this way

    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());
         }
    
        }
    
    0 讨论(0)
  • 2020-12-08 21:40

    You need to wait for ImageView inflated. To try OnLayoutChangeListener.

            imageView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
                //
                if (imageView.getWidth() > 0 && imageView.getHeight() > 0) {
                    // put your code here...
                }
            });
    
    0 讨论(0)
  • 2020-12-08 21:46

    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 ImageView and added a new callback OnSizeChangedCallback that is being called whenever the size of the view is changed to non zero values.

    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
    0 讨论(0)
  • 2020-12-08 22:01

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

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

    and then simply

    int width = (int) getResources().getDimension(R.dimen.image_Width)
    
    0 讨论(0)
提交回复
热议问题