Image Compare and change image in Android

Create xml Step 1

Step 2: 

Src imageView Match image

if (compareTwoDrawable(holder.binding.RecyclerviewAssignCourseSelected.getDrawable(), context.getResources().getDrawable(R.drawable.close_square_icon))) {
// Change the background to a different image
holder.binding.RecyclerviewAssignCourseSelected.setImageResource(R.drawable.yes_icon);
}
else {
// Set the background to R.drawable.yes_icon
holder.binding.RecyclerviewAssignCourseSelected.setImageResource(R.drawable.close_square_icon);
}

 

Step 2: 

match Drawable image function

public static boolean compareTwoDrawable(Drawable drawable1, Drawable drawable2) {
int h=drawable1.getIntrinsicHeight();
int w=drawable1.getIntrinsicWidth();
Bitmap map= Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
int h2=drawable2.getIntrinsicHeight();
int w2=drawable2.getIntrinsicWidth();
Bitmap map2= Bitmap.createBitmap(w2,h2, Bitmap.Config.ARGB_8888);
//binary code match
// ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
// map.compress(Bitmap.CompressFormat.PNG, 100, stream1);
// byte[] bytes1 = stream1.toByteArray();
//
// ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
// map2.compress(Bitmap.CompressFormat.PNG, 100, stream2);
// byte[] bytes2 = stream2.toByteArray();
//
// boolean same = Arrays.equals(bytes1, bytes2);

boolean same = map.sameAs(map2);
return same;
}

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.