0

私のrawフォルダーには、1.jpgから100.jpgの範囲の名前の画像がたくさんあります。1〜100の乱数を生成し、生成された数に基づいて、対応する画像を画像ビューに表示します。たとえば、乱数5なので、5.jpgを表示します。

これどうやってするの?ありがとう!:-)

4

3 に答える 3

1
  Random random = new Random();

  // The +1 because nextInt(100) delivers between 0-99
  int drawableNumber = random.nextInt(100) +1;

  String drawableName = recourceNum + ".jpg";

  yourImageView.setImageDrawable(getRecource().getDrawable(getRecources().getIdentifier(drawableName, "res/drawable", yourPackageName);
于 2012-07-16T09:02:14.283 に答える
0

これを試して:

oncreate(){

1- initialize your imageView say imgView.
2- generate a random number between 1 to 100, say random.
3- make sure you have all those 100 images in your res/drawable folder.

4- get the res ID related to the random by using following code:

    int id = context.getResources().getIdentifier(String.valueOf(random), "drawable", context.getPackageName());

5- imgView.setImageResource(id);

}

これがお役に立てば幸いです!!

于 2012-07-16T09:02:02.650 に答える
-1
private static int getRandomInteger(int aStart, int aEnd) {
    long range = (long) aEnd - (long) aStart + 1;
    long fraction = (long) (range * new Random().nextDouble());
    int randomNumber = (int) (fraction + aStart);
    return randomNumber;
}
于 2012-07-16T09:02:58.167 に答える