Android compare imageView with image

本秂侑毒 提交于 2019-12-11 01:36:07

问题


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

回答1:


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



回答2:


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
}



回答3:


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


来源:https://stackoverflow.com/questions/37575857/android-compare-imageview-with-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!