如何获取ImageView的源以便更改它?

23

我知道改变ImageView的资源不是什么大问题,只需要使用myImageView.setImageResource(mynewImageDrawable)

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

基本上,我想使用ImageView实现自己的单选按钮组。所以每次点击任何一个ImageView时,onclick事件方法将更改我的组的图像资源。

对于我的糟糕英语,我深感抱歉。

问候, redsea

7个回答

34

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

如何做到这一点?

我认为90%的情况下,您的视图上都没有任何标记,因此最简单的方法是假设您的标记是唯一的标记:

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

如果我的视图上已经有一个标签怎么办

Android 视图可以同时承载多个标签,只要它们具有唯一的标识符。您需要创建一个唯一的 ID 资源,并将其作为 setTag 方法调用的第一个参数添加进去。保留代码如下:

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

如果我不知道在setTag的第二个参数中分配哪个当前图像怎么办? - jorge saraiva

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 

1

实际上,在我的尝试之后,我发现可以通过其他方式获取imageView的源,而不需要使用tag属性。

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

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

你没有提到两个非常重要的事情:如何获取 attrs 和如何选择您想获取 src 的确切 ImageView? - dragi

1

1

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

并不是说你提出的方案行不通,只是它不是很好的设计,会导致维护性较差(例如如果资源名称更改)。


0

我曾经遇到过同样的问题,这是我的解决方案:

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

}

0

你可以使用这个。

 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"));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接