Try to Transform the drawable
in Bitmap
and compare using the method sameAs.
Follow an example:
final ImageButton buttonImage = ImageButton.class.cast(findViewById(R.id.facebookLogin));
if(null != buttonImage.getDrawable()){
final Drawable drawable = getResources().getDrawable(R.drawable.ic_facebook, null);
if( getBitmap(buttonImage.getDrawable()).sameAs(getBitmap(drawable)) ){
Toast.makeText(getApplicationContext(), "São iguais", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Não são iguais", Toast.LENGTH_SHORT).show();
}
}
method to transform into Bitmap:
public static Bitmap getBitmap(Drawable drawable) {
Bitmap result;
if (drawable instanceof BitmapDrawable) {
result = ((BitmapDrawable) drawable).getBitmap();
} else {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// Some drawables have no intrinsic width - e.g. solid colours.
if (width <= 0) {
width = 1;
}
if (height <= 0) {
height = 1;
}
result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
}
return result;
}
Hello Leandro, could you add the error that occurs? Thanks in advance!
– Thiago Luiz Domacoski
I edited the question, basically wanted to know how I can compare the
Background
of aImageButton
with a diedrawable
?– Leandro Almeida
You can try in a more "shallow" way, without the need to make image conversions and risk generating false-true. I used in past projects the property "getTag()" and "setTag()", which helped me quickly and simply.
– Carlos Bridi
how do I do that?
getTag()
andsetTag()
– Leandro Almeida
ImageView imageView = (ImageView) view.findViewById(R.id.imageInteresse);

imageView.setTag("1");

iamgeView.getTag();
– Carlos Bridi