在运行时从图像集合中两次应用随机图像

时间:2018-12-22 18:38:01

标签: android

我想将3个随机图像应用于6个ImageView,以便任何2个imageview具有相同的图像,另外2个imageview具有相同的图像,最后2个imageview具有相同的图像。但是,每次刷新应用程序时,相同图像的位置都应该更改。

final Random rnd = new Random();

String str1,str2,str3;

            str1 = "img_" + rnd.nextInt(20);
            str2 = "img_" + rnd.nextInt(20);
            if(str1==str2)
            {
                 str2 = "img_" + rnd.nextInt(20);
            }
            else
            {

            }
            str3 = "img_"+ rnd.nextInt(20);

            if(str2==str3||str1==str3)
            {
                str3 = "img_"+rnd.nextInt(20);
            }
            else
            {

            }

            String STRR[] = new String[3];
            STRR[0] = str1;
            STRR[1] = str2;
            STRR[2] = str3;



            IV1.setImageDrawable
                    (
                            getResources().getDrawable(getResourceID(STRR[new Random().nextInt(STRR.length)], "drawable",
                                    getApplicationContext()))
                    );
            IV2.setImageDrawable
                    (
                            getResources().getDrawable(getResourceID(STRR[new Random().nextInt(STRR.length)], "drawable",
                                    getApplicationContext()))
                    );
            IV3.setImageDrawable
                    (
                            getResources().getDrawable(getResourceID(STRR[new Random().nextInt(STRR.length)], "drawable",
                                    getApplicationContext()))
                    );
            IV4.setImageDrawable
                    (
                            getResources().getDrawable(getResourceID(STRR[new Random().nextInt(STRR.length)], "drawable",
                                    getApplicationContext()))
                    );
            IV5.setImageDrawable
                    (
                            getResources().getDrawable(getResourceID(STRR[new Random().nextInt(STRR.length)], "drawable",
                                    getApplicationContext()))
                    );
            IV6.setImageDrawable
                    (
                            getResources().getDrawable(getResourceID(STRR[new Random().nextInt(STRR.length)], "drawable",
                                    getApplicationContext()))
                    );

0 个答案:

没有答案