Created
February 15, 2017 21:42
-
-
Save kostovtd/8eb46fed1be6dfb096cb977a65987279 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment