本文介绍了比较两个drawable中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图比较两个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.

在我的应用程序中,我使用 getCompoundDrawablesWithIntrinsicBounds 将ImageView放在EditText的正确位置。
然后我需要检查哪个图像资源在那里。

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");
 }


推荐答案

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)

这篇关于比较两个drawable中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:58