Android比较imageView与image

3
我想将当前imageView和来自R.drawable的图像进行比较。我猜我尝试了所有的方法,但我无法解决这个问题。我从stack overflow尝试了所有的方法。
XML:
<ImageView
        android:layout_width="match_parent"
        android:src="@drawable/red"
        android:id="@+id/imageview1"
        android:clickable="true"
        android:layout_height="match_parent" />

安卓:
final ImageView test = (ImageView) findViewById(R.id.imageview1);

test.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          //if(test.getResources().equals(R.drawable.red))
          //if(test.getDrawable().equals(R.drawable.red))
          if(test.getDrawable().getConstantState().equals(getResources().getDrawable(R.drawable.red).getConstantState()))
          {
               Toast.makeText(getApplicationContext(), "work", Toast.LENGTH_SHORT).show();
          }
          else
          {
               Toast.makeText(getApplicationContext(), "not work", Toast.LENGTH_SHORT).show();
          }
     }
});

2
欢迎来到 StackOverflow。您可能想解释一下为什么要比较图像,因为“比较图像”通常是计算机视觉问题。 - Morrison Chang
我只想比较当前ImageView使用了哪些资源。在上面的例子中,我将一些图像(R.drawable.red)分配给imageview。然后,我想将imageview中的资源与R.drawable.red进行比较。 - Michael
尝试在Stack Overflow上查看这个答案...https://dev59.com/nWUq5IYBdhLWcg3wBL7j#14909358 - Muhammad Waleed
但这还不够。是否有可能“提取”ImageView中使用的图像(或其某个ID),并将其与R.drawable进行比较? - Michael
1
不是Android资源,而只是字符串,位图和样式等静态数据的索引,它们填充适当的数据结构。在您的情况下,我认为您需要提取出imageview1的位图,创建一个R.drawable.red的位图,然后在位图之间进行比较。可绘制对象可以从许多不同的东西中创建,而不仅仅是Android资源文件:https://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromPath(java.lang.String) - Morrison Chang
3个回答

6
如果您想保存一些信息以便查看,可以使用标签。
    <ImageView
    android:layout_width="match_parent"
    android:id="@+id/imageview1"
    android:src="@drawable/red"
    android:tag="work"
    android:clickable="true"
    android:layout_height="match_parent" />

现在你可以进行比较了

        ImageView image = (ImageView) findViewById(R.id.imageview1);
    if ("work".equals(image.getTag())){
        Toast.makeText(getApplicationContext(), "work", Toast.LENGTH_SHORT).show();
    }

您可以通过代码设置此标签。
image.setTag("not work");

5
谢谢,Morrison,就是这样。
首先
final ImageView test = (ImageView) findViewById(R.id.imageview1);
final Bitmap bmap = ((BitmapDrawable)test.getDrawable()).getBitmap();
Drawable myDrawable = getResources().getDrawable(R.drawable.red);
final Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();

下一步
if(bmap.sameAs(myLogo))
{
do sthng
}

0
首先,这个:
.getResources().getDrawable(imageResource)

在API21及以上版本中已被弃用。

以下是一个比较两张图片的代码示例:

imageview.setImageResource(R.drawable.image_name);
int id= getResources().getIdentifier("@drawable/image_name", null, this.getPackageName());

Drawable.ConstantState constantState;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        constantState = mContext.getResources().getDrawable(id,mContext.getTheme()).getConstantState();
    } else {
        constantState = mContext.getResources().getDrawable(id).getConstantState();
    }

    if (imageview.getDrawable().getConstantState() == constantState) {
        Log.d(TAG,"Success");
    } else {
        Log.d(TAG,"Not possible");
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接