3

I want to compare current imageView with image from R.drawable. I guess I tried everything but I can't solve this one. I tried everything form 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" />

Android:

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();
          }
     }
});
e-shfiyut
  • 3,538
  • 2
  • 30
  • 31
Michael
  • 81
  • 1
  • 1
  • 9
  • 2
    Welcome to StackOverflow. You may want to explain why do you want to compare images, as 'comparing images' is generally computer vision problem. – Morrison Chang Jun 01 '16 at 18:11
  • I just want to compare what resource is use in current ImageView. In example above i attribute some image(R.drawable.red) to imageview. Then i want to compare reosurces from imageview with R.drawable.red – Michael Jun 01 '16 at 18:16
  • try this answer on stack overflow....http://stackoverflow.com/a/14909358/3678308 – Muhammad Waleed Jun 01 '16 at 18:26
  • But its not it. Is it possible to "pull" what image is used in ImageView (or some id of it) and compare it with R.drawable ? – Michael Jun 01 '16 at 18:30
  • 1
    No as the Android resources is just an index into static data such as strings, bitmaps, and styles which populates the appropriate data structure. In your case I think you would have to extract out the bitmap of imageview1, create a bitmap of R.drawable.red and then do the comparison between bitmaps. Drawables can be created from lots of different things other than a Android Resource file: https://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromPath(java.lang.String) – Morrison Chang Jun 01 '16 at 18:36

3 Answers3

6

If you want to save some information to view, you can use tag.

    <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" />

and now you can compare

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

you can set this tag from code

image.setTag("not work");
Vivart
  • 14,900
  • 6
  • 36
  • 74
4

Thanks Morrison, that was it.

First

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();

Next

if(bmap.sameAs(myLogo))
{
do sthng
}
Michael
  • 81
  • 1
  • 1
  • 9
0

First of all, this:

.getResources().getDrawable(imageResource)

is deprecated in API21 and above.

Here is one example of code that you can use to compare two images:

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