【问题标题】:how to get the source of ImageView in order to change it?如何获取 ImageView 的来源以便更改它?
【发布时间】:2012-05-23 19:24:09
【问题描述】:

我知道仅使用myImageView.setImageResource(mynewImageDrawable) 更改 ImageView 资源并不是什么大问题

但我想做的是在更改之前检查当前的 ImageSource。

基本上,我想使用 imageViews 实现我自己的组单选按钮。所以每次点击任何imageView时,oncliked事件方法都会改变我组的Image Resources。

对不起,我的英语很差。

问候, 红海

【问题讨论】:

    标签: android imageview drawable


    【解决方案1】:

    没有getDrawableId 函数,因此您需要在更改其可绘制对象时为ImageView 设置标签。例如,将drawable id设置为ImageView的标签,这样你就可以从标签中获取drawable id。

    怎么做?

    我会说 90% 的时间,你的视图上不会有任何标签,所以最简单的方法是假设你的标签是唯一的标签:

    myImageView.setTag(R.drawable.currentImage);    //When you change the drawable
    int drawableId = (Integer)myImageView.getTag(); //When you fetch the drawable id
    

    如果我的视图中已经有标签怎么办

    Android 视图可以同时托管多个标签,只要它们具有唯一标识符即可。您需要 create a unique id 资源并将其作为第一个参数添加到 setTag 方法调用。留下这样的代码:

    myImageView.setTag(R.id.myTagId, R.drawable.currentImage); //Set
    int drawableId = (Integer)myImageView.getTag(R.id.myTagId);
    

    【讨论】:

    • 如果我不知道要在 setTag 中的第二个参数上分配哪个当前图像怎么办?
    【解决方案2】:

    要获取图片来源,您可以使用Drawable.ConstantState。 例如,在我的问题中,我正在实现一个按钮单击来更改 ImageView。 我创建了两个Drawable.ConstantState 对象并进行了比较。 代码如下:

    ImageView myImg=(ImageView) findViewById(R.id.myImg);
            Drawable.ConstantState imgID = myImg.getDrawable().getConstantState();
            Drawable.ConstantState imgID2 = getDrawable(R.drawable.otherImage).getConstantState();
            if(imgID!=imgID2)
            {
                // Block of Code
            }
            else // Other Block of Code 
    

    【讨论】:

      【解决方案3】:

      您是说您打算检查资源 ID 以确定是否选择了单选按钮?虽然这可能可行,但它不是一个很好的设计。一般来说,您希望将模型与视图分开(也称为Model-View-Controller 范例)。为此,您可以让单选按钮组维护所选项目的索引,并将每个项目存储在列表中。选择项目后,您可以确定其索引、更新组的模型,然后更新 UI 以反映该更改。

      这并不是您提出的行不通,它只是不是好的设计,并导致可维护性差(例如,如果资源名称发生更改)。

      【讨论】:

        【解决方案4】:

        如果你想获得src drawable,请使用ImageView的以下方法: http://developer.android.com/reference/android/widget/ImageView.html#getDrawable()

        【讨论】:

          【解决方案5】:

          实际上经过我的尝试,我发现有一种方法可以在不需要标签属性的情况下获取 imageView 的来源。

          Integer src = attrs.getAttributeResourceValue(
                              "http://schemas.android.com/apk/res/android", "src", -1);
          

          第一个参数是android的命名空间,src是属性名。 该方法如果找到则返回图片资源id,否则返回-1;

          【讨论】:

          • 你没有提到两个非常重要的事情:如何获取 attrs 以及如何选择你想要获取 src 的确切 ImageView?
          【解决方案6】:

          你可以使用它。

           private Drawable colocaImgen(String nombreFile) {
              Drawable res1 = null;
              String uri1 = null;
              try {
                  //First image
                  uri1 = "@drawable/" + nombreFile;
                  int imageResource1 = getResources().getIdentifier(uri1, null,getApplicationContext().getPackageName());
                  res1 = getResources().getDrawable(imageResource1);
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  //int imageResource1 = context.getResources().getIdentifier(uri1, null, context.getApplicationContext().getPackageName());
                  res1 = getResources().getDrawable(R.drawable.globo2);// Default image
              }
              return res1;
          }
          

          然后

          ((ImageView) findViewById(R.id.youNameFile)).setImageDrawable(colocaImgen("NameFile"));
          

          【讨论】:

            【解决方案7】:

            我遇到了同样的问题,这是我的工作解决方案:

            public void engLayoutExpand(View view) {
                ImageView iv = (ImageView) view;
            
                // One way:
                Bitmap bm1 = ((BitmapDrawable) iv.getDrawable()).getBitmap();
                Bitmap bm2 = ((BitmapDrawable) getResources().getDrawable(R.drawable.baseline_expand_less_black_36)).getBitmap();
            
                if (bm1 == bm2) {
                    iv.setImageResource(R.drawable.baseline_expand_more_black_36);
                }
            
                // Other way
                Drawable.ConstantState cs1 = iv.getDrawable().getConstantState();
                Drawable.ConstantState cs2 = getResources().getDrawable(R.drawable.baseline_expand_less_black_36).getConstantState();
            
                if ( cs1 == cs2) {
                    iv.setImageResource(R.drawable.baseline_expand_more_black_36);
                }
            
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-02-27
              • 1970-01-01
              • 2011-01-05
              • 2013-11-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多