我如何才能找到它的图像在ImageView的从我的绘制设置? [英] How can i find which image is set in imageview from my drawable?

查看:108
本文介绍了我如何才能找到它的图像在ImageView的从我的绘制设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我的drawble文件夹中找到该图像在ImageView的设置(ImageView的的的onClick)。为此我需要从我的文件夹绘制比较的ImageView的背景和图像绘制。我发现一些为方法,但这些方法都在我的情况的作品。

在调试这个问题,我发现的ImageView的一个属性(在ImageView的悬停)命名为 mBackgroundResource 持有绘制文件夹中我的形象的相同的整数值。

  INT I = R.drawable.skype; (2130837526)

的ImageView mBackgroundResource = 2130837526

那么,有没有办法让mBackgroundResource价值?所以,我可以尝试进行比较。任何人都可以帮助解决这个问题?

下面是我的code。

我在适配器,这样,

 上下文mContext;
可绘制Skype的;Skype的= mContext.getResources()getDrawable(R.drawable.skype)。 //在适配器的构造。公共无效的onClick(视图v)
    {
        ImageView的IMG =(ImageView的)v.findViewById(R.id.img_call_icon);
        可绘制img_id = img.getBackground();
    }

现在我已经试过以下。(我在哪里丢失的?)

  / ********* ********************************************** /    如果(img_id == Skype的)
    {
        //不工作...
    }/ ******************* ********************************** /
位图的位图=((BitmapDrawable)img_id).getBitmap();
位图bitmap1 =((BitmapDrawable)的Skype).getBitmap();如果(位图== bitmap1)
{
    //不工作...
}
/ ******************* ********************************** /
对象标记= img.getTag();
INT I = R.drawable.skype;如果(标记= NULL&放大器;!及((整数)标签).intValue()== I)
{
   //不工作...
}
/ ******************* ********************************** /
INT J =((整型)标签).intValue(); //总是返回0 ..
INT I = R.drawable.skype;如果(我== j)条
{
   //不工作...
}
/ ******************* ********************************** /
布尔B = img_id.equals软件(Skype); //返回FALSE,不工作...
/ ******************* ********************************** /
ImageView的ImageView的=(ImageView的)v.findViewById(R.id.img_call_icon);
断言(ⅰ== imageView.getId());
整数整数=(整数)imageView.getTag(); //总是空填...
整数=整数== NULL? 0:整数;
   开关(整数)
   {
   案例R.drawable.skype:
       Toast.makeText(mContext,Skype的,Toast.LENGTH_SHORT).show();
       打破;   案例R.drawable.call:
       Toast.makeText(mContext,呼,Toast.LENGTH_SHORT).show();
       打破;
   } //不工作...


解决方案

您设置了的ImageView绘制在适配器?

如果是这样,你可以只保存在查看的标签的ID绘制,就像这样:

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

和检查哪些可绘制在ImageView的:

  ImageView的ImageView的=(ImageView的)v.findViewById(R.id.img_call_icon);
对象标记= imageView.getTag();
INT ID =标签== NULL? -1:(INT)标签;
开关(ID)
{
案例R.drawable.skype:
    Toast.makeText(mContext,Skype的,Toast.LENGTH_SHORT).show();
    打破;案例R.drawable.call:
    Toast.makeText(mContext,呼,Toast.LENGTH_SHORT).show();
    打破;
}

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...

解决方案

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

这篇关于我如何才能找到它的图像在ImageView的从我的绘制设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆