如何比较ImageButton的图像和可绘制的图像?



如果ImageButtonbutton的图像等于一个可绘制的图像,我想要一个特定的动作。这样的:

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

但是如果我这样做,getDrawable得到红色下划线,我不知道为什么?

有人能帮我吗?

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

第二

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

getConstantState()并不总是有效。

你可以比较它们是否与它们相等:

  • 宽×高
  • <
  • 像素/gh><
  • 字节/gh>

最新更新