ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何从drawable中找到imageview中设置的图像?

2019-10-02 05:25:32  阅读:149  来源: 互联网

标签:android android-imageview background-image


我需要在drawble文件夹中查找imageview(onview of imageview)中设置的图像.为此,我需要从drawable文件夹中比较drawview的imageview的背景和图像.我找到了一些方法,但在我的情况下,这些都不起作用.

在调试这个问题时,我发现一个名为“mBackgroundResource”的imageview(在imageview上悬停)的一个属性在drawable文件夹中保存了我的图像的相同Integer值.

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

Imageview的mBackgroundResource = 2130837526

那么有没有办法获得mBackgroundResource值?所以我可以尝试比较它.谁能帮忙解决这个问题?

这是我的代码.

我在适配器中,所以,

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

现在我试过了……(我错过了哪里?)

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

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

解决方法:

您是否在适配器中设置了ImageView的drawable?

如果是这样,您可以将可绘制ID保存在View的标记中,如下所示:

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

并检查ImageView中哪个drawable:

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

标签:android,android-imageview,background-image
来源: https://codeday.me/bug/20191002/1841259.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有