gpt4 book ai didi

android - 我如何从我的可绘制对象中找到在 imageview 中设置的图像?

转载 作者:太空狗 更新时间:2023-10-29 16:07:47 25 4
gpt4 key购买 nike

我需要从我的 drawble 文件夹中找到 ImageView 中设置的图像( ImageView 的 onClick)。为此,我需要比较 imageview 背景的可绘制对象和我的可绘制文件夹中的图像。我为此找到了一些方法,但在我的情况下都不起作用。

在调试这个问题时,我发现名为“mBackgroundResource”的 imageview(悬停在 imageview 上)的一个属性与我在可绘制文件夹中的图像的整数值相同。

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 的可绘制对象?

如果是这样,您可以将可绘制对象的 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 中有哪个可绘制对象:

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 - 我如何从我的可绘制对象中找到在 imageview 中设置的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9922345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com