给定4个不同的图像文件,如何在Android Activity中生成随机图块的网格?

时间:2019-03-27 14:47:44

标签: java android xml

我正在用9个图块乘14个图块的android活动创建一个网格,每个图块是从4种不同图块类型中的1种随机选择的:沙漠,草丛,岩石和水。

我尝试做一个嵌套的for循环,并通过findViewById()函数编辑磁贴。每个图块都有对应于其位置的唯一ID,但是我找不到将for循环中的数字链接到图块位置ex的方法:R.id.tile + i + j

    protected void randomizeTiles(){
        Random rand = new Random();
        ArrayList<Class <? extends GenericTile>> tileTypes
                = new ArrayList<Class <? extends GenericTile>>();
        tileTypes.add(0, Desert.class);
        tileTypes.add(1, Grass.class);
        tileTypes.add(2, Rock.class);
        tileTypes.add(3, Water.class);

        this.boardLayout = new GenericTile[9][13];

        AssetManager manager;
        manager = getAssets();

        for(int i=0; i < 13; i++){
            for(int j=0; j < 9; j++){
                int n = rand.nextInt(4);
                this.boardLayout[j][i] = (GenericTile) tileTypes.get(n).newInstance();
                ImageView tilePic = (ImageView) findViewById(R.id.tile11);
                InputStream open;
                try{
                    open = manager.open(this.boardLayout[j][i].path);
                    Bitmap bitmap = BitmapFactory.decodeStream(open);
                    // Assign the bitmap to an ImageView in this layout
                    tilePic.setImageBitmap(bitmap);
                }
                catch(FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
 <GridLayout
        android:columnCount="9"
        android:rowCount="14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:alignmentMode="alignMargins">

        <!-- row 1 -->

        <LinearLayout
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="1dp"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile11"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

            </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile12"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile13"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile14"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile15"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile16"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile17"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile18"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile19"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

.............................

        <!-- row 13 -->

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile131"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile132"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile133"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile134"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile135"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile136"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile137"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile138"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

        <LinearLayout
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="1dp"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/tile139"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/desert"
                tools:srcCompat="@drawable/desert" />

        </LinearLayout>

    </GridLayout>


当前代码仅将其中一个图块随机化,其余图块设置为默认的沙漠图片。

1 个答案:

答案 0 :(得分:0)

欢迎Patrick来到StackOverflow。

您似乎面临一个体系结构问题:“如何将for循环的ij镜像到9x13的ImageViews网格中。”

可以使用多种技术来解决。我在这里给你一个简单的例子:

创建一个包含所有ImageView的9x13数组。因此,获取fooArrayOfImageViews[0][12]将为您提供右上方的ImageView。行:

 this.boardLayout[j][i] = (GenericTile) tileTypes.get(n).newInstance();
 ImageView tilePic = (ImageView) findViewById(R.id.tile11);
 .....
 tilePic.setImageBitmap(bitmap);

,成为

 this.boardLayout[j][i] = (GenericTile) tileTypes.get(n).newInstance();
 ImageView tilePic = fooArrayOfImageViews[j][i];
 .....
 tilePic.setImageBitmap(bitmap);

希望它会引导您朝正确的方向前进。