簡體   English   中英

循環以隨機顯示數組中的圖像(Android)

[英]Loop to randomly display Images from Array (Android)

我有5個圖像的數組(R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four和R.drawable.five)。 以前,我的程序會從中選擇5張隨機圖像,並將它們隨機顯示在屏幕上,而不會出現任何錯誤。 然后,我添加了myImgCounttotalImgCount因為我想給每個圖像一個int值。 例如:R.drawable.one等於1,R.drawable.two等於2 ...等等。 然后,我希望我的for循環跟蹤使用myImgCount放置的圖像,然后運行直到totalImgCount等於50。添加myImgCounttotalImgCount ,現在出現錯誤:

“ 11-26 18:22:16.750:E / AndroidRuntime(13971):致命例外:Thread-33401 E / AndroidRuntime(13971):java.lang.NullPointerException”

Bitmap bmp = BitmapFactory.decodeResource(getResources(), array1[i]);

這是我到目前為止的代碼:

public void surfaceCreated(SurfaceHolder holder) {

ArrayList<Integer> list = new ArrayList<Integer>();

    Random r = new Random();

    int[] images = new int[] { R.drawable.one, R.drawable.two,
        R.drawable.three, R.drawable.four, R.drawable.five };

    int totalImgCount = 0;


    for (int i = 0; i<50; i++)
    {
        while (true) 
        {
            int myImgCount = r.nextInt(5);

                 totalImgCount += myImgCount + 1;

            if (totalImgCount<50) {

                list.add(images[myImgCount]);   //do it again if not yet at 50
                break;
            }
                   totalImgCount -= (myImgCount + 1);
        }

    }

    array1 = convertIntegers(list);

    }

我的代碼的下一部分用於為圖像提供int值:

public static int[] convertIntegers(List<Integer> integers) {
    int[] ret = new int[integers.size()];
    Iterator<Integer> iterator = integers.iterator();
    for (int i = 0; i < ret.length; i++) {
        ret[i] = iterator.next().intValue();
    }
    return ret;
}

在收到此錯誤之前,我的程序將無錯誤運行,並在屏幕上顯示5張隨機圖像。 正如我上面提到的,我試圖創建一個循環,一直顯示隨機圖像,直到圖像的總和為50。如果您需要更多信息,請告訴我! 感謝您的幫助,並提前致謝!

編輯:遺漏了我的render方法,我在哪里得到錯誤:

public void render(Canvas canvas) {

    if (canvas != null) {

        Random random = new Random();
        canvas.drawRGB(234, 237, 235);

        for (int i=0; i<50; ++i) { 
          Bitmap bmp = BitmapFactory.decodeResource(getResources(), 
array1[i]);            //This is where I am currently getting the error
          canvas.drawBitmap(bmp,random.nextInt(canvas.getWidth()-bmp.getWidth
                      ()), random.nextInt
                        (canvas.getHeight()-bmp.getHeight()),null);

        }
    }

}

聽起來您沒有將隨機數限制為數組索引(0-4)。 從此開始,將數字限制在0到4之間。

    int range = images.length-1;        
    int myImgCount = 0;

    Random random = new Random();
    for( int i = 0; i < 50; ++i)
    {
        myImgCount = random.nextInt(range);
        list.add(images[myImgCount]);
    }

調整您的渲染方法,看看是否有幫助:

    if (canvas != null)
    {             
         Random random = new Random();
         canvas.drawRGB(234, 237, 235);

         //iterate over your integer array
         //there should be 50 resource ids available
         for(int resourceId : array1)
         {
               Bitmap bmp = BitmapFactory.decodeResource(getResources(), resourceId);
               //This is where I am currently getting the error
               canvas.drawBitmap(bmp, 
                     random.nextInt(canvas.getWidth()-bmp.getWidth()), 
                     random.nextInt(canvas.getHeight()-bmp.getHeight()), null);

         }    
     }

我還將更改以下內容:

for (int i = 0; i<50; i++)
{
    while (true) 
    {
        int myImgCount = r.nextInt(5);

             totalImgCount += myImgCount + 1;

        if (totalImgCount<50) {

            list.add(images[myImgCount]);   //do it again if not yet at 50
            break;
        }
               totalImgCount -= (myImgCount + 1);
    }

}

可能的變化:

 int i = 0;

 while(i < 50)
 {
      //calculate random resource ids from your resource array here and add them to your array1
      i++;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM