How can i find which image is set in imageview from my drawable?

此生再无相见时 提交于 2019-12-04 01:46:38

问题


I need to find which image is set in the imageview (onClick of imageview) from my drawble folder. For that I need to compare drawable of imageview's background and image from my drawable folder. I found some of methods for that but none of that works in my case.

While debugging this problem I found one property of the imageview (on hover of imageview) named "mBackgroundResource" holds same Integer value of my image in the drawable folder.

int i = R.drawable.skype; (2130837526)

Imageview's mBackgroundResource= 2130837526

So is there any way to get mBackgroundResource value? So I can try to compare it. Can anyone help to solve this?

Here is my code.

I am in adapter so,

Context mContext;
Drawable skype;

skype = mContext.getResources().getDrawable(R.drawable.skype); // in the constructor of the adapter.

public void onClick(View v)
    {   
        ImageView img = (ImageView)v.findViewById(R.id.img_call_icon);
        Drawable img_id = img.getBackground();
    }

Now I've tried following..(Where am I missing ?)

/***********************************************************************************/

    if(img_id == skype)
    {
        // Not working...
    }

/***********************************************************************************/
Bitmap bitmap = ((BitmapDrawable)img_id).getBitmap();
Bitmap bitmap1 = ((BitmapDrawable)skype).getBitmap();

if(bitmap == bitmap1)
{
    // Not working...
}
/***********************************************************************************/
Object tag = img.getTag();
int i = R.drawable.skype;

if(tag != null && ((Integer)tag).intValue() == i)
{
   // Not working...
}
/***********************************************************************************/
int j = ((Integer)tag).intValue() ; // always return 0..
int i = R.drawable.skype;

if(i == j)
{
   // Not working...
}
/***********************************************************************************/
Boolean b = img_id.equals(skype); // return FALSE, Not working...
/***********************************************************************************/
ImageView imageView = (ImageView)v.findViewById(R.id.img_call_icon);
assert(i == imageView.getId());
Integer integer = (Integer) imageView.getTag(); // always fill with null...
integer = integer == null ? 0 : integer;
   switch(integer) 
   {
   case R.drawable.skype:
       Toast.makeText(mContext, "skype", Toast.LENGTH_SHORT).show();
       break;

   case R.drawable.call:
       Toast.makeText(mContext, "call", Toast.LENGTH_SHORT).show();
       break;
   } // Not working...

回答1:


Are you settings the ImageView's drawable in the adapter?

If so, you could just save the drawable id in the View's tag, like so:

ImageView imageView = (ImageView) v.findViewById(R.id.img_call_icon);
imageView.setTag(R.drawable.a_drawable);
imageView.setDrawableResource(R.drawable.a_drawable);

and to check which drawable is in the ImageView:

ImageView imageView = (ImageView) v.findViewById(R.id.img_call_icon);
Object tag = imageView.getTag();
int id = tag == null ? -1 : (int) tag;
switch(id)
{
case R.drawable.skype:
    Toast.makeText(mContext, "skype", Toast.LENGTH_SHORT).show();
    break;

case R.drawable.call:
    Toast.makeText(mContext, "call", Toast.LENGTH_SHORT).show();
    break;
}


来源:https://stackoverflow.com/questions/9922345/how-can-i-find-which-image-is-set-in-imageview-from-my-drawable

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