2

this is my question:

At the first click on button I set the background of view

public void clickOnButton(View v){
      if(compareDrawable(getResources().getDrawable(R.drawable.green_button), v.getBackground())) {
          v.setBackgroundResource(R.drawable.red_button);
      }else{
          v.setBackgroundResource(R.drawable.green_button);
      }
}

Where the compare function has this code:

    public boolean compareDrawable(Drawable d1, Drawable d2){
    try{
        Bitmap bitmap1 = ((BitmapDrawable)d1).getBitmap();
        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
        stream1.flush();
        byte[] bitmapdata1 = stream1.toByteArray();
        stream1.close();

        Bitmap bitmap2 = ((BitmapDrawable)d2).getBitmap();
        ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
        bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream2);
        stream2.flush();
        byte[] bitmapdata2 = stream2.toByteArray();
        stream2.close();

        return bitmapdata1.equals(bitmapdata2);
    }
    catch (Exception e) {
        // TODO: handle exception
    }
    return false;
}

I've already try to use some of these comparations:

    if(v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.green_button).getConstantState()))

or

    if(getResources().getDrawable(R.drawable.green_button).hashCode() == v.getBackground().hashCode())

and the code of xml files is like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#70c656" />
        <stroke
            android:width="1dp"
            android:color="#53933f" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>
<item>
    <shape>
        <gradient
            android:startColor="#70c656"
            android:endColor="#53933f"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#53933f" />
        <corners
            android:radius="4dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

but they doesn't work, they return false every time. How can I compare these object?

DuffMck
  • 81
  • 7
  • this question was already discussed here http://stackoverflow.com/questions/6120439/comparing-bitmap-images-in-android – Desert Jun 24 '13 at 21:23

1 Answers1

0

Why would you need to compare Drawables just to change the image back and forth?

Instead use CheckBox or other CompoundButton and use selector xml with 4 states:

  • checked and pressed
  • checked
  • pressed
  • default
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94