简体   繁体   中英

How can I compare the Image of an ImageButton to a drawable?

I want a certain action to be done if the image of my ImageButton button is equal to a drawable. Like this:

if (button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.mydrawable).getConstantState())
{
     //perform action//
}

But if I do so, getDrawable gets red underline and I don't know why?

Can anyone help me?

First

ImageButton temp =  findViewById(R.id.myImagebtn);
Bitmap imageBtm = ((BitmapDrawable)temp.getDrawable()).getBitmap();
Drawable myDrawable = getResources().getDrawable(R.drawable.apple);
Bitmap appleBtm = ((BitmapDrawable) myDrawable).getBitmap();

second

if(imageBtm.sameAs(appleBtm))
{
Log.i("Bitmap compare", "equal");
}

getConstantState() doesn't always work.

You could compare if they are equal with their:

  • width x height
  • pixels
  • bytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM