Comparing two drawings in android

How to compare two blueprints, I am doing this but not succeeding

public void MyClick(View view) { Drawable fDraw = view.getBackground(); Drawable sDraw = getResources().getDrawable(R.drawable.twt_hover); if(fDraw.equals(sDraw)) { //Not coming } } 
+74
android
Feb 03 2018-12-12T00:
source share
11 answers

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 are using TextView , use getBackground().getConstantState instead.

+144
Dec 20
source share

Using only getConstantState() can lead to false negatives .

The approach I took is to try to compare ConstantState in the first instance, but to drop the Bitmap comparison if this check fails.

This should work in all cases (including images that are not resources), but note that he is 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 - eg 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; } 
+24
Jul 22 '15 at 11:41
source share

My question was that we were simply comparing two drawings, but I could not get any method that would directly compare the two drawings, however, for my solution, I changed drawable to bitmap and then compared two raster images and this works.

 Bitmap bitmap = ((BitmapDrawable)fDraw).getBitmap(); Bitmap bitmap2 = ((BitmapDrawable)sDraw).getBitmap(); if(bitmap == bitmap2) { //Code blcok } 
+8
Feb 03 2018-12-12T00:
source share

Solution for Android 5:

  if(image.getDrawable().getConstantState().equals(image.getContext().getDrawable(R.drawable.something).getConstantState())) 
+4
Apr 08 '15 at 2:56
source share

maybe try this as follows:

 public void MyClick(View view) { Drawable fDraw = view.getBackground(); Drawable sDraw = getResources().getDrawable(R.drawable.twt_hover); if(fDraw.hashCode() == sDraw.hashCode()) { //Not coming } } 

or prepare a method that takes two valid arguments and returns boolean. In this method, you can convert drawable to bytes and compare,

 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; } 
+3
Feb 03 2018-12-12T00:
source share

for SDK 21+

it works in SDK -21

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

for SDK +21 android 5. set reverse identifier in image with tag

 img.setTag(R.drawable.xxx); 

and compare like this

 if ((Integer) img.getTag() == R.drawable.xxx) { ....your code } 

this solution is for those who want to compare drawable id imageview with drawable id.

+2
May 13, '15 at 15:22
source share

Well, I think I have found the final solution for you. Due to AppCompat and friends, the popped code is sometimes inflated in different forms, so getResources().getBitmap(R.drawable.my_awesome_drawable) not enough.

So, to get an approximate instance of the same type and form as the view, you can do the following:

 public static Drawable drawableFrom(View view, @DrawableRes int drawableId) { Context context = view.getContext(); try { View dummyView = view.getClass().getConstructor(Context.class).newInstance(context); dummyView.setBackgroundResource(drawableId); return dummyView.getBackground(); } catch (Exception e) { return ResourcesCompat.getDrawable(context.getResources(), drawableId, null); } } 

This is useful when running tests. However, I would not recommend doing this in production. If you need additional caching, it would be advisable to avoid too much reflection.

For express tests, you can use this pretty well:

 onView(withDrawable(R.drawable.awesome_drawable)) .check(matches(isDisplayed())); 

or

 onView(withId(R.id.view_id)) .check(matches(withDrawable(R.drawable.awesome_drawable))); 

Before you have to declare this helper class:

 public class CustomMatchers { public static Matcher<View> withDrawable(@DrawableRes final int drawableId) { return new DrawableViewMatcher(drawableId); } private static class DrawableViewMatcher extends TypeSafeMatcher<View> { private final int expectedId; private String resourceName; private enum DrawableExtractionPolicy { IMAGE_VIEW { @Override Drawable findDrawable(View view) { return view instanceof ImageView ? ((ImageView) view).getDrawable() : null; } }, TEXT_VIEW_COMPOUND { @Override Drawable findDrawable(View view) { return view instanceof TextView ? findFirstCompoundDrawable((TextView) view) : null; } }, BACKGROUND { @Override Drawable findDrawable(View view) { return view.getBackground(); } }; @Nullable private static Drawable findFirstCompoundDrawable(TextView view) { for (Drawable drawable : view.getCompoundDrawables()) { if (drawable != null) { return drawable; } } return null; } abstract Drawable findDrawable(View view); } private DrawableViewMatcher(@DrawableRes int expectedId) { this.expectedId = expectedId; } @Override protected boolean matchesSafely(View view) { resourceName = resources(view).getResourceName(expectedId); return haveSameState(actualDrawable(view), expectedDrawable(view)); } private boolean haveSameState(Drawable actual, Drawable expected) { return actual != null && expected != null && areEqual(expected.getConstantState(), actual.getConstantState()); } private Drawable actualDrawable(View view) { for (DrawableExtractionPolicy policy : DrawableExtractionPolicy.values()) { Drawable drawable = policy.findDrawable(view); if (drawable != null) { return drawable; } } return null; } private boolean areEqual(Object first, Object second) { return first == null ? second == null : first.equals(second); } private Drawable expectedDrawable(View view) { return drawableFrom(view, expectedId); } private static Drawable drawableFrom(View view, @DrawableRes int drawableId) { Context context = view.getContext(); try { View dummyView = view.getClass().getConstructor(Context.class).newInstance(context); dummyView.setBackgroundResource(drawableId); return dummyView.getBackground(); } catch (Exception e) { return ResourcesCompat.getDrawable(context.getResources(), drawableId, null); } } @NonNull private Resources resources(View view) { return view.getContext().getResources(); } @Override public void describeTo(Description description) { description.appendText("with drawable from resource id: "); description.appendValue(expectedId); if (resourceName != null) { description.appendValueList("[", "", "]", resourceName); } } } } 
+2
Jun 19 '17 at 14:33
source share

Use getTag () and setTag () to compare

+1
Apr 24 2018-12-12T00:
source share

getDrawable (int) is now deprecated. Use getDrawable (context, R.drawable.yourimageid)

Compare two flags

 Boolean Condition1=v.getBackground().getConstantState().equals( ContextCompat.getDrawable(getApplicationContext(),R.drawable.***).getConstantState()); 
0
Mar 31 '17 at 4:00
source share

if you want to directly compare the two extracted codes use the following code

Drawable fDraw = getResources (). getDrawable (R.drawable.twt_hover);

Drawable sDraw = getResources (). getDrawable (R.drawable.twt_hover);

 if (fDraw.getConstantState().equals(sDraw.getConstantState())) { //write your code. } else { //write your code. } 
-one
Feb 20 '14 at 11:19
source share

When you use the equals() method, it is used to compare contents. you should try == to compare two objects.

 public void MyClick(View view) { Drawable fDraw = view.getBackground(); Drawable sDraw = getResources().getDrawable(R.drawable.twt_hover); if( fDraw == sDraw ) { // Coming } } 
-2
Feb 03 2018-12-12T00:
source share



All Articles