Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two drawables in android

Tags:

android

How to compare two drawables, I am doing like this but not having any success

public void MyClick(View view) {  Drawable fDraw = view.getBackground();  Drawable sDraw = getResources().getDrawable(R.drawable.twt_hover);    if(fDraw.equals(sDraw))   {    //Not coming   } } 
like image 506
Roshan Jha Avatar asked Feb 03 '12 07:02

Roshan Jha


People also ask

What are Drawables in Android?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What is the preferred image format for the Drawables?

Supported file types are PNG (preferred), JPG (acceptable), and GIF (discouraged). App icons, logos, and other graphics, such as those used in games, are well suited for this technique.


Video Answer


2 Answers

Update https://stackoverflow.com/a/36373569/1835650

getConstantState() works not well

There is another way to compare:

mRememberPwd.getDrawable().getConstantState().equals             (getResources().getDrawable(R.drawable.login_checked).getConstantState()); 

mRemeberPwd is an ImageView in this example. If you're using a TextView, use getBackground().getConstantState instead.

like image 117
Mejonzhan Avatar answered Oct 06 '22 18:10

Mejonzhan


Relying on getConstantState() alone can result in false negatives.

The approach I've taken is to try comparing the ConstantState in the first instance, but fall back on a Bitmap comparison if that check fails.

This should work in all cases (including images which aren't resources) but note that it is memory hungry.

public static boolean areDrawablesIdentical(Drawable drawableA, Drawable drawableB) {     Drawable.ConstantState stateA = drawableA.getConstantState();     Drawable.ConstantState stateB = drawableB.getConstantState();     // If the constant state is identical, they are using the same drawable resource.     // However, the opposite is not necessarily true.     return (stateA != null && stateB != null && stateA.equals(stateB))             || getBitmap(drawableA).sameAs(getBitmap(drawableB)); }  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; } 
like image 20
vaughandroid Avatar answered Oct 06 '22 16:10

vaughandroid