Compare Background with Drawable

Asked

Viewed 86 times

1

I have a problem comparing the Background of a ImageButton with a drawable

IB6 is my ImageButton and wanted to see if you had the drawable def, when it comes to this if application to and error in equals

if (IB6.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.def).getConstantState()))

The program goes down because I’m miscomparing the Background of ImageButton with the drawable.

How do I make a if to verify whether the Background of IB6 is equal to drawable def??

  • Hello Leandro, could you add the error that occurs? Thanks in advance!

  • 1

    I edited the question, basically wanted to know how I can compare the Backgroundof a ImageButton with a die drawable?

  • 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.

  • how do I do that? getTag() and setTag()

  • ImageView imageView = (ImageView) view.findViewById(R.id.imageInteresse);

imageView.setTag("1");

iamgeView.getTag();

2 answers

4

getDrawable() does not return the background but rather the image assigned by android:src.

Instead of getDrawable() use getBackground()

if (IB6.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.def).getConstantState()))

0

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;
}
  • I was close to solving but there’s a mistake is that mine IB6.getDrawable() is always null and it begins with an image in Background, because it is always the null? Even changing the image gives null

  • The @ramaral response should suit you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.