问题
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