Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将图像动态添加到ImageView_Android - Fatal编程技术网

Android 如何将图像动态添加到ImageView

Android 如何将图像动态添加到ImageView,android,Android,我正在写一个益智游戏来学习开发Android应用程序。但现在我一直在研究如何将图像动态添加到布局文件中的ImageView(见下文)。我试着做一个循环,在其中我向每个ImageView添加一个随机图像。但我找不到任何关于如何做到这一点的例子。我的图像与ImageView的名称相同,仅以小写字母命名。 还是有其他更好的方法来解决这个问题 到目前为止,我的代码是: // Puzzle of 2x2 tiles String[] sTiles = {"A0","A1","B0","B1

我正在写一个益智游戏来学习开发Android应用程序。但现在我一直在研究如何将图像动态添加到布局文件中的ImageView(见下文)。我试着做一个循环,在其中我向每个ImageView添加一个随机图像。但我找不到任何关于如何做到这一点的例子。我的图像与ImageView的名称相同,仅以小写字母命名。 还是有其他更好的方法来解决这个问题

到目前为止,我的代码是:

    // Puzzle of 2x2 tiles
    String[] sTiles = {"A0","A1","B0","B1"};
    final Random myRandom = new Random();

    // Random tiles
    String tmp = null;
    for (int i = 0; i < sTiles.length; ++i) {
        tmp = sTiles[i];
        int r = myRandom.nextInt(sTiles.length);
        sTiles[i] = sTiles[r];
        sTiles[r] = tmp;
    }

    // Lopp to add images randomly to the screen (layout/main.xml)
    //for(i=0; i < sTiles.length; ++i) {
         ImageView image = (ImageView) findViewById(R.id.B0);
         image.setImageResource(R.drawable.a0);
    //}
//2x2瓷砖拼图
字符串[]阶梯={“A0”、“A1”、“B0”、“B1”};
最终随机数myRandom=新随机数();
//随机瓷砖
字符串tmp=null;
对于(int i=0;i
---------------layout/main.xml------------



谢谢,西格德

您必须制作某种容器(可能是线性或相对布局),并将图像添加到视图中。例如:

   RelativeLayout cbrl = new RelativeLayout(this);
            RelativeLayout.LayoutParams cbp = new RelativeLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                cbVal.setLayoutParams(cbp);

    for(i=0; i < sTiles.length; ++i) {
         ImageView image = (ImageView) findViewById(this);
         image.setImageResource(R.drawable.xX);
cbrl.addView(iv);
    }
RelativeLayout cbrl=新的RelativeLayout(本);
RelativeLayout.LayoutParams cbp=新的RelativeLayout.LayoutParams(
LayoutParams.WRAP_内容,LayoutParams.WRAP_内容);
cbVal.setLayoutParams(cbp);
对于(i=0;i
从R.drawable中读取反射值,并通过获取R中字段的值将其加载为位图

有点像:

    public static void example() throws IllegalArgumentException, IllegalAccessException{
        Class<R.drawable> clazz = R.drawable.class;
        for(Field field : clazz.getDeclaredFields()){
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), (Integer) field.get(null));
            //bmp is your image
        }
    }
public static void example()抛出IllegalArgumentException,IllegalAccessException{
clazz类=R.drawable.Class;
for(字段:clazz.getDeclaredFields()){
位图bmp=BitmapFactory.decodeResource(getResources(),(Integer)field.get(null));
//bmp是你的图像
}
}

尝试设置一些int数组来存储可绘制和小部件ID。它比使用反射按名称查找它们要快。像这样的事情应该可以做到:

int[] imageViews = {
    R.id.A0, R.id.A1,
    R.id.B0, R.id.B1,
    R.id.C0, R.id.C1 //...
    };

int[] images = {
    R.drawable.a0, R.drawable.a1,
    R.drawable.b0, R.drawable.b1,
    R.drawable.c0, R.drawable.c1 //...
    };

Random random = new Random(System.currentTimeMillis());

for(int v : imageViews) {
    ImageView iv = (ImageView)findViewById(v);
    iv.setImageResource(images[random.nextInt(images.length - 1)]);
}

如果要确保所有ImageView都获得唯一的图像,您可能需要添加一些特殊处理,但这应该按原样工作。

刚刚意识到我的代码中有sTiles.length;应该是图像。长度:)已更改。我将此代码用于我的应用程序,随机图像正在更改,但一些图像重复。我想要一个imageview的图像。。请帮帮我。。
int[] imageViews = {
    R.id.A0, R.id.A1,
    R.id.B0, R.id.B1,
    R.id.C0, R.id.C1 //...
    };

int[] images = {
    R.drawable.a0, R.drawable.a1,
    R.drawable.b0, R.drawable.b1,
    R.drawable.c0, R.drawable.c1 //...
    };

Random random = new Random(System.currentTimeMillis());

for(int v : imageViews) {
    ImageView iv = (ImageView)findViewById(v);
    iv.setImageResource(images[random.nextInt(images.length - 1)]);
}