0

the question is about imageview in android:

There is an imageview, after I call method like

imageview.setImageDrawable(drawable) 

or

imageview.setImageBitmap(bitmap)

then a drawable will be drawned on the imageview, I want to know that before I call another setXXXDrawable/Bitmap method to the same imageview , the drawable that had been drawned on this imageview has or has not been changed by other operation? or the image showned on the imageview has been changed since my last call?

Any help will be appreciated~ thks!

Suraj Makhija
  • 1,376
  • 8
  • 16
  • try using imageview.getDrawable(); and compare – Nabil Mar 24 '17 at 06:34
  • Try customizing ImageView to detect image changes , Customized ImageView class posted as an answer , please chekc and let me know if it is working for you , consider adding setImageBitmap and setImageDrawable methods as you are using them . – Shishupal Shakya Mar 24 '17 at 06:42

3 Answers3

0

You can create your own ImageView to detect image changes , Here is the class from Sandy's answer , try with it , setImageChangeListner() to detect image changes .

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

Reference Link : ImageView onImageChangedListener Android

Community
  • 1
  • 1
Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41
0

First get the drawable from imageView.getDrawable() method and get bimap from that drawable that you just got. Now compare these both bitmaps to identify that you have both same image or image has been changed. Both will have same bytes if they will be same.

Awais
  • 1,157
  • 9
  • 15
-1

You can check whether an image has been assigned a drawable or not as follows :

if(imageView.getDrawable() != null) {
    // you can get the drawable that has been assigned to it
} else {
   // assign the desired drawable
   imageview.setImageDrawable(drawable)
}
Suraj Makhija
  • 1,376
  • 8
  • 16
  • What you have answered is not the matching the requirement , Alex is asking to detect Image Changes , but what your answer will do -> Detect existing drawable , If it is then ok else assign new drawable . – Shishupal Shakya Mar 24 '17 at 06:35