【问题标题】:Get the ID of a drawable in ImageView获取 ImageView 中可绘制对象的 ID
【发布时间】:2011-05-30 10:06:31
【问题描述】:

我有一个ImageView 并在其上设置了一个可绘制对象。现在我需要动态获取ImageView的点击事件的可绘制ID。我怎样才能得到它?

imgtopcolor = (ImageView) findViewById(R.id.topcolor); 
imgtopcolor.setImageResource(R.drawable.dr);  // How do I get this back?

现在在imgtopcolor 的触摸事件中,我需要drawable id,因为我每次都设置不同的drawable,并且想将drawable 与其他进行比较

【问题讨论】:

  • 请粘贴您的代码。

标签: android android-imageview android-drawable


【解决方案1】:

我想如果我理解正确的话,这就是你正在做的事情。

ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        ImageView imageView = (ImageView) view;
        assert(R.id.someImage == imageView.getId());

        switch(getDrawableId(imageView)) {
            case R.drawable.foo:
                imageView.setDrawableResource(R.drawable.bar);
                break;
            case R.drawable.bar:
            default:
                imageView.setDrawableResource(R.drawable.foo);
            break;
        }
    });

对吗?所以函数getDrawableId() 不存在。您无法获得实例化可绘制对象的 id,因为该 id 只是对设备上有关如何构造可绘制对象的数据位置的引用。一旦构建了drawable,它就没有办法取回用于创建它的resourceId。但是你可以使用 tags

让它像这样工作
ImageView view = (ImageView) findViewById(R.id.someImage);
view.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        ImageView imageView = (ImageView) view;
        assert(R.id.someImage == imageView.getId());

        // See here
        Integer integer = (Integer) imageView.getTag();
        integer = integer == null ? 0 : integer;

        switch(integer) {
        case R.drawable.foo:
            imageView.setDrawableResource(R.drawable.bar);
            imageView.setTag(R.drawable.bar);
            break;
        case R.drawable.bar:
        default:
            imageView.setDrawableResource(R.drawable.foo);
            imageView.setTag(R.drawable.foo);
            break;
        }
    });

【讨论】:

  • 感谢回复。但我认为没有像 getDrawableID() 这样的方法。
  • 是的,但我必须将标签对象转换为整数才能获得标签。我的工作已经完成
  • cool srry 我没有那个编译器。只要确保尝试使用 Integer.valueOf() 而不是使用 new Integer()。如果您将 new 用于应该回收的整数,它只会造成更多的浪费。希望少于 256。
  • 在xml文件的tag中设置什么?我做吗android:tag="@drawable/foo"
  • 如果您是在xml中静态定义它,那么您不妨只使用imageView id。据我所知,在 xml 中设置标签只会将标签设置为字符串。
【解决方案2】:

我已经在另一个问题中回答了类似的问题,但会为这个问题稍微改变一下。

很遗憾,没有getImageResource() 或getDrawableId()。但是,我使用 ImageView 标签创建了一个简单的解决方法。

在 onCreate() 中:

imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);

imageView0.setTag(R.drawable.apple);
imageView1.setTag(R.drawable.banana);
imageView2.setTag(R.drawable.cereal);

然后,如果您愿意,可以创建一个简单的函数来获取可绘制的 id:

private int getDrawableId(ImageView iv) {
    return (Integer) iv.getTag();
}

太容易了。

【讨论】:

  • 您可以将标签设置为常规的可绘制图像对象吗?
  • 标签可以设置为任意Object。
【解决方案3】:

截至今天,不支持此功能。但是,我发现了一个小技巧。

 imageView.setImageResource(R.drawable.ic_star_black_48dp);
 imageView.setTag(R.drawable.ic_star_black_48dp);

所以如果你想获取视图的ID,只需获取它的标签即可。

if (imageView.getTag() != null) {
   int resourceID = (int) imageView.getTag();

   //
   // drawable id.
   //   
}

【讨论】:

    【解决方案4】:

    一个简单的解决方案可能是将drawable id 存储在一个临时变量中。我不确定这对您的情况有多实用,但这绝对是一个快速解决方案。

    【讨论】:

      【解决方案5】:

      在 StackOverflow 上寻找类似问题的答案,我发现人们通常建议 2 种方法:

      • 将可绘制对象加载到内存中并将ConstantState 或位图本身与另一个进行比较。
      • 在视图中设置一个带有可绘制 id 的标签,并在需要时比较标签 那个。

      就个人而言,出于性能原因,我喜欢第二种方法,但是使用适当的标签标记一堆视图既痛苦又耗时。这在一个大项目中可能非常令人沮丧。就我而言,我需要编写很多 Espresso 测试,这些测试需要比较 TextView drawables、ImageView 资源、View 背景和前景。很多工作。

      所以我最终想出了一个解决方案,将“肮脏”的工作委托给自定义充气机。在每个膨胀的视图中,我都会搜索特定的属性,并为视图设置一个标签,如果找到的话,它会带有资源 id。这种方法与来自Calligraphy 的人几乎相同。我为此写了一个简单的库:TagView

      如果你使用它,你可以检索任何预定义的标签,包含在 xml 布局文件中设置的可绘制资源 id:

      TagViewUtils.getTag(view, ViewTag.IMAGEVIEW_SRC.id)
      TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_LEFT.id)
      TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_TOP.id)
      TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_RIGHT.id)
      TagViewUtils.getTag(view, ViewTag.TEXTVIEW_DRAWABLE_BOTTOM.id)
      TagViewUtils.getTag(view, ViewTag.VIEW_BACKGROUND.id)
      TagViewUtils.getTag(view, ViewTag.VIEW_FOREGROUND.id)
      

      该库实际上支持任何属性。您可以手动添加它们,只需查看 Github 上的 Custom attributes 部分。 如果您在运行时设置可绘制对象,您可以使用方便的库方法:

      setImageViewResource(ImageView view, int id)
      

      在这种情况下,标记是在内部为您完成的。如果你使用 Kotlin,你可以编写一个方便的扩展来调用 view 本身。像这样的:

      fun ImageView.setImageResourceWithTag(@DrawableRes int id) {
          TagViewUtils.setImageViewResource(this, id)
      }
      

      您可以在Tagging in runtime找到更多信息

      【讨论】:

        【解决方案6】:

        我最近遇到了同样的问题。我通过实现我自己的 ImageView 类解决了这个问题。

        这是我的 Kotlin 实现:

        class MyImageView(context: Context): ImageView(context) {
            private var currentDrawableId: Int? = null
        
            override fun setImageResource(resId: Int) {
                super.setImageResource(resId)
                currentDrawableId = resId
            }
        
            fun getDrawableId() {
                return currentDrawableId
            }
        
            fun compareCurrentDrawable(toDrawableId: Int?): Boolean {
                if (toDrawableId == null || currentDrawableId != toDrawableId) {
                    return false
                }
        
                return true
            }
        
        }
        

        【讨论】:

          【解决方案7】:

          更简单:只需将R.drawable id 存储在视图的id 中:使用v.setId()。然后用v.getId()取回。

          【讨论】:

          • 坏主意,视图 ID 用于其他事情,你不应该这样做,永远不要。例如:您为视图设置了一个随机 id,然后您想访问该视图……您如何识别它,因为您丢失了以前的布局 id? ://
          • 如果你想保存它以供以后使用,你可以将 Drawable Id 设置为视图标签
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多