Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing resources within two drawables

Tags:

java

android

I am trying to compare two drawables but without success. I did some research, there is even a similar question but did not help.

In my app, I use getCompoundDrawablesWithIntrinsicBounds to get the ImageView in the right position of a EditText. Then I need to check which image resource is alocated there.

This small sample should work, shouldn't it? It returns "not equal", though.

Drawable drawable1 = ContextCompat.getDrawable(getApplicationContext(),R.drawable.cor);

Drawable drawable2 = ContextCompat.getDrawable(getApplicationContext(),R.drawable.cor);


if(drawable1 == drawable2){
     System.out.println("equal");
}else{
     System.out.println("not equal");
 }
like image 404
AndroidDev Avatar asked Apr 02 '16 12:04

AndroidDev


1 Answers

getConstantState doesn't work well

If you do this: if(drawable1 == drawable2){

you are comparing the reference of the objects and it not correct...

use instead equals with the getConstantState() method...

Update Try to compare with bytes or pixel is the only way that generally works.

 // Usage: 
 drawable1.bytesEqualTo(drawable2) 
 drawable1.pixelsEqualTo(drawable2) 
 bitmap1.bytesEqualTo(bitmap1) 
 bitmap1.pixelsEqualTo(bitmap2) 

https://gist.github.com/XinyueZ/3cca89416a1e443f914ed37f80ed59f2

like image 197
ΦXocę 웃 Пepeúpa ツ Avatar answered Nov 09 '22 09:11

ΦXocę 웃 Пepeúpa ツ