Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

java - Comparing resources within two drawables

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");
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...