如何使图像数组随机化

时间:2016-01-05 00:03:12

标签: java android arrays

我有一个图像数组,在我从中选择带有ID编号的图像之前我想知道如果我可以将它们随机化,那么每次运行应用程序时相同的图像都可以有不同的ID编号?

我的图像数组:

<array name="PairImages">
    <item>@drawable/a</item>
    <item>@drawable/b</item>
    <item>@drawable/c</item>
    <item>@drawable/d</item>
    <item>@drawable/e</item>
    <item>@drawable/f</item>
    <item>@drawable/g</item>
    <item>@drawable/h</item>
    <item>@drawable/i</item>
    <item>@drawable/j</item>
</array>

代码我用来用数组中的图像填充我的图像视图:

ImageView FirstImageCard, SecondImageCard, ThirdImageCard, FourthImageCard, FifthImageCard, SixthImageCard, SeventhImageCard, EightImageCard;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.getWindow().getDecorView().setSystemUiVisibility(getSystemUiFlags());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirstImageCard = (ImageView)findViewById(R.id.imageView);
    SecondImageCard = (ImageView)findViewById(R.id.imageView2);
    ThirdImageCard = (ImageView)findViewById(R.id.imageView3);
    FourthImageCard = (ImageView)findViewById(R.id.imageView4);
    FifthImageCard = (ImageView)findViewById(R.id.imageView5);
    SixthImageCard = (ImageView)findViewById(R.id.imageView6);
    SeventhImageCard = (ImageView)findViewById(R.id.imageView7);
    EightImageCard = (ImageView)findViewById(R.id.imageView8);

    final TypedArray ImageArray = getResources().obtainTypedArray(R.array.PairImages);

    FirstImageCard.setImageResource(ImageArray.getResourceId(0, -1));
    SecondImageCard.setImageResource(ImageArray.getResourceId(1, -1));
    ThirdImageCard.setImageResource(ImageArray.getResourceId(2, -1));
    FourthImageCard.setImageResource(ImageArray.getResourceId(3, -1));
    FifthImageCard.setImageResource(ImageArray.getResourceId(4, -1));
    SixthImageCard.setImageResource(ImageArray.getResourceId(5, -1));
    SeventhImageCard.setImageResource(ImageArray.getResourceId(6, -1));
    EightImageCard.setImageResource(ImageArray.getResourceId(7, -1));
}

1 个答案:

答案 0 :(得分:1)

您已完成所有工作,只需要一个数组或列表来保存您的id值,然后将它们随机化。看看shuffle

您的代码将最终成为:

//...

final TypedArray ImageArray = getResources().obtainTypedArray(R.array.PairImages);

//Just a holder for random numbers. 
//If you add/remove an image make sure you change the 8...
ArrayList<Integer> randomNumbers = new ArrayList<>();
for (int i = 0; i < 8; i++){
    randomNumbers.add(i);
}
Collections.shuffle(randomNumbers);
int i = 0;


FirstImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
SecondImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
ThirdImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
FourthImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
FifthImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
SixthImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
SeventhImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
EightImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));

//...

你也可以尝试使用随机数字来构建你的数组,就像在question中那样,但我认为现在洗牌是正常的。