如何解决如何将 ImageButton 的 Image 与 drawable 进行比较?
如果我的 ImageButton button
的图像等于可绘制对象,我希望执行某个操作。像这样:
if (button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.mydrawable).getConstantState())
{
//perform action//
}
但如果我这样做,getDrawable
会变成红色下划线,我不知道为什么?
有人可以帮我吗?
解决方法
第一次
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() 并不总是有效。
您可以比较它们是否与它们相等:
- 宽 x 高
- 像素
- 字节
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。