ImageView onImageChangedListener Android

Deadly 提交于 2019-11-28 10:05:38

Check the imageview code in grepcode. You don't know when it is changed or redrawn. It is because after you setImageDrawable(), imageview will invalidate. At this time, the image IS NOT CHANGED correctly until ondraw is called.

Anyway, why do you want to know the onimagechangedlistener?

There is no default listener in Android .. but we can create the imagechange listiner .. copy the class and instead of using ImageView use MyImageView..

 public class MyImageView extends ImageView {

        private OnImageChangeListiner onImageChangeListiner;


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

        public MyImageView(Context context, AttributeSet attributeSet) {         
            super(context, attributeSet); 
        }


        public void setImageChangeListiner(
                OnImageChangeListiner onImageChangeListiner) {
            this.onImageChangeListiner = onImageChangeListiner;
        }

        @Override
        public void setBackgroundResource(int resid) {
            super.setBackgroundResource(resid);
            if (onImageChangeListiner != null)
                onImageChangeListiner.imageChangedinView(this);
        }


        @Override
        public void setBackgroundDrawable(Drawable background) {
            super.setBackgroundDrawable(background);
            if (onImageChangeListiner != null)
                onImageChangeListiner.imageChangedinView(this);
        }


        public static interface OnImageChangeListiner {
            public void imageChangedinView(ImageView mImageView);
        }
    }

If you want to load the image from network and check change in imageview you can use imageView.isAttachedToWindow(). I have tried downloading the image from network and disabled the progressbar after image downloaded and attached to window. Use,

         if(imageView.isAttachedToWindow()){ 
            //your code here
          }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!