比较两个drawable中的资源 [英] Comparing resources within two drawables

查看:103
本文介绍了比较两个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");
 }


推荐答案

getConstantState doesn'运作良好

如果你这样做:
if(drawable1 == drawable2){

您正在比较对象的参考但不正确...

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

使用而不是等于 getConstantState()方法......

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆