【问题标题】:Adding random and unique images to imageviews android向imageviews android添加随机和唯一的图像
【发布时间】:2012-06-04 17:28:27
【问题描述】:

我在一项活动中有 16 个图像浏览......名为 img1->img16。我想要每个图像视图上的差异图像,这不能硬编码,取决于作为额外传递的数字,例如1、2、3……添加到这些视图中的图像必须不同。

我目前有添加 16 张图像的代码,但这些不是唯一的......它们是重复的......

        int[] imageViews = {
            R.id.img1, R.id.img2,
            R.id.img3, R.id.img4,
            R.id.img5, R.id.img6,
            R.id.img7, R.id.img8,
            R.id.img9, R.id.img10,
            R.id.img11, R.id.img12,
            R.id.img13, R.id.img14,
            R.id.img15, R.id.img16
            };

    int[] images = {
            R.drawable.img1, R.drawable.img2,
            R.drawable.img3, R.drawable.img4,
            R.drawable.img5, R.drawable.img6,
            R.drawable.img7, R.drawable.img8,
            R.drawable.img9, R.drawable.img10,
            R.drawable.img11, R.drawable.img12,
            R.drawable.img13, R.drawable.img14,
            R.drawable.img15, R.drawable.img16
            };


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

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

而且,如果可能的话,我需要它以便图像不会硬编码在图像数组中,因为它们会根据传递的数字而有所不同。

所以这些图像是针对数字 1 的,但是如果将数字 2 传递给 Activity,则图像会有所不同。

所有细节都保存在本地数据库中,图像都在可绘制文件夹中,并且都将遵循相同的命名样式,例如img1 一路说 img100....

非常感谢。

/更新/

您好,我已使用以下代码进行了尝试:

Random random = new Random(System.currentTimeMillis());
    List<Integer> generated = new ArrayList<Integer>();
        for(int v : imageViews) 
        {
             int next = random.nextInt(15) + 1;
             if (!generated.contains(next))
             {
                generated.add(next);
                ImageView iv = (ImageView)findViewById(v);
                iv.setImageResource(images[next]);
             }
        }

我猜我可能做错了....但到目前为止我还没有找到重复的,但对差距没有帮助,好像它可能会生成 16 个随机整数,但只会添加那些是唯一的,而不是生成直到达到 16 个唯一的图像,如果您了解的话?

在这方面有什么想法吗?

【问题讨论】:

  • 那么您遇到的唯一问题是您当前的方法允许重复吗?
  • 是的,有时当您访问活动时,会丢失一些图像,图像视图仍然存在但可能存在图像应该存在的间隙

标签: android image random imageview


【解决方案1】:

每当您获得下一个随机整数时,将该值保存在一个数组中。然后下次你得到一个随机整数时,检查这个数字是否已经被选中。如果有,请重复该过程,直到选择了未选择的 int。

编辑:试试这个代码:

Random random = new Random( System.currentTimeMillis() );
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < imageViews.length; i++) {

    int v = imageViews[i];
    int next = random.nextInt( 15 ) + 1;
    if ( !generated.contains( next ) ) {
        generated.add( next );
        ImageView iv = (ImageView) findViewById( v );
        iv.setImageResource( images[next] );
    }
    else {
        i--;
    }
}

【讨论】:

  • 我已经更新了我的帖子......我已经尝试了一些代码,但如果你现在看到我的原始帖子,我会解释它的问题
  • 刚刚尝试了与没有 i-- 类似的操作,并且只是尝试了您的操作,但都导致了同样的问题,一旦您加载活动,它会在黑屏上挂起几秒钟,然后才会要求强制关闭.......
  • @Dean'Amstrad'Woodford 我认为它必须是random.nextInt(16) + 1,因为该方法会在 [0,n) 范围内找到一个随机数。
  • 我不确定问题出在哪里,但是一旦我设置为 16,它就会立即导致异常 java.lang.ArrayIndexOutOfBoundsException: length=16;索引=16
  • 好的解决了这个问题,设置为16但是去掉+1......谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多