动态更改图片视图背景

4

我有一个包含10个ImageView的布局。我已经给它们顺序编号,如下所示:

android:id="@+id/pb1"

android:id="@+id/pb2"

现在,我想动态地更改背景。
    int totalPoi = listOfPOI.size();
    int currentPoi = (j/totalPoi)*10;
    for (i=1;i<=currentPoi;i++) {
          imageview.setBackgroundResource(R.drawable.progressgreen);
}

现在,在for循环内部,我想动态设置图像视图的背景。即,如果currentpoi值为3,则应更改3个图像视图的背景。无论for循环迭代多少次,都应更改相应数量的图像视图的背景。希望问题现在清楚了。

注意:我只有一个名为progressgreen的图像,需要设置到10个图像视图中。


你能展示完整的代码并清晰地解释吗?你有多少个drawable?请列出所有的drawable。 - ilango j
6个回答

7
最终,我是按照以下方式完成的:
我将所有的id都放在数组中,如下:
int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10};

现在:

int pindex = 0;

for (pindex; pindex <currentPoi; pindex++) {

    ImageView img = (ImageView) findViewById(imageViews[pindex]) ;
    img.setImageResource(R.drawable.progressgreen);
}

现在,我可以动态地更改图片。

@goto10。谢谢你的帮助。我会调试你的点以查看我的问题出在哪里。


3
创建一个 ImageView 数组:
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.pb1);
...
views[9] = (ImageView)findViewById(R.id.pb10);

现在遍历循环来设置图片的背景,如下所示:
for (i=1;i<=currentPoi;i++) 
{
    views[i-1].setBackgroundResource(R.drawable.progressgreen);
}

2

您可以通过将Drawable的名称设置为img_1、img_2、img_3等来实现此操作。

for (i=1;i<=currentPoi;i++)
{
ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName()));
imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable",  getPackageName()));
}

这并不会在多个ImageView上设置背景。它会在同一个ImageView上多次设置背景。 - goto10

1

你需要给你的ImageViews分配顺序id,例如"@+id/pb1"和"@+id/pb2"等。然后你可以在循环中像这样获取它们:

for (i=1;i<=currentPoi;i++) {
    // Find the image view based on it's name. We know it's pbx where 'x' is a number
    // so we concatenate "pb" with the value of i in our loop to get the name
    // of the identifier we're looking for. getResources.getIdentifier() is able to use
    // this string value to find the ID of the imageView
    int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name");

    // Use the ID retrieved in the previous line to look up the ImageView object
    ImageView imageView = (ImageView) findViewById(imageViewId);

    // Set the background for the ImageView
    imageView.setBackgroundResource(R.drawable.progressgreen);
}

com.your.package.name替换为您的应用程序包。


你能告诉我在 imageview.setBackgroundResource(R.drawable.progressgreen); 中的 imageview 是什么吗? - tejas
是的,我明白了,但我有一个愚蠢的疑问,你所指的pb是指图像视图对象,对吗? - tejas
这很奇怪。imageViewId的值是否与你的R.java文件中的任何ID值匹配? - goto10
请确保包名正确。它应该与您清单文件中的包属性匹配。 - goto10
我已经没有更多的想法了。那行代码是否在这个静态类中:public static final class id {?当imageViewId为0时,pindex的值是多少? - goto10
显示剩余14条评论

1

尝试这段代码...... 创建图像数组..

private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2,
        R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 };

然后修改你的代码

int totalPoi = listOfPOI.size();
int currentPoi = (j/totalPoi)*10;
for (i=1;i<=currentPoi;i++) {
      imageview.setBackgroundResource(mThumbIds[i]);}

那将会多次更改同一图像的背景。我认为这不是所期望的。 - goto10
他想根据currentPoi的值更改多个ImageView的背景。如果它是3,则前三个(共10个)图像应更改其背景。您的代码有5个背景,如果currentPoi为3,则会在一个图像上更改3次背景。 - goto10
这就好像我让你把一间房间的墙涂成红色、绿色、蓝色和橙色,但你最终只把一面墙先涂成了红色,然后又在同一面墙上涂上了绿色和蓝色,最后再涂上了橙色。这样我最终只会得到一面橙色的墙,而不是我要求的四面涂色的墙。 - goto10
@goto10,是的,正是这个问题。 - tejas
我的回答告诉你如何做到这一点。 - goto10

1
你可以创建一个 ImageView 数组,然后在循环中修改它们。
ImageView views[] = new ImageView[10];
views[0] = (ImageView)findViewById(R.id.imageView0);
...
views[9] = (ImageView)findViewById(R.id.imageView9);

然后将您的for循环更改为:

for (i=1;i<=currentPoi;i++) {
   views[currentPoi].setBackgroundResource(R.drawable.progressgreen);
}

数组从索引0开始,因此请确保这里没有偏移一个的错误。


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