繁体   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