1

ここで質問するのは初めてなので、トピックにとどまるようにします。適切なサイズのビットマップオブジェクトのArrayListを作成し、それらを順番に描画することで、背景をランダムに生成しようとしています。ちなみに、この実装は単一のビットマップを正しくロードして機能します。それはただリストにつまずいているだけです。

コードに入る前に、理想的には、個々のピクセルまたはタイルを追加して単一のビットマップを作成し、実際にそのバリエーションをいくつか試しましたが、すべて黒い画面になることを指摘しておきます。キャンバスへの描画方法に問題があるのではないかと思い始めています。とにかく、これが私が持っているものです:

まず、ランダムなArrayListを生成しますが、現在は3色のみを使用しています。リストを返すようにしますが、これはスレッド内のプライベートメソッドであり、スレッドの変数の1つを参照しているため、それほど重要ではありません。

    private void genMap(Resources res)
    {
        // Load basic tiles.
        Bitmap green = BitmapFactory.decodeResource(res, R.drawable.green);
        Bitmap red = BitmapFactory.decodeResource(res, R.drawable.red);
        Bitmap blue = BitmapFactory.decodeResource(res, R.drawable.blue);

                    // All tiles must be the same size.
        int tile_width = green.getWidth();
        int tile_height = green.getHeight();
        int num_x = mCanvasWidth / tile_width;
        int num_y = mCanvasHeight / tile_height;

        for (int j = 0; j < num_y; j++)
        {
            for (int i = 0; i < num_x; i++)
            {
                double r = Math.random();
                Bitmap tile;
                if (r <= 1/3) {tile = green;}
                else if (r <= 2/3) {tile = red;}
                else {tile = blue;}
                // Create a new Bitmap in order to avoid referencing the old value.
                mBackgroundImages.add(Bitmap.createBitmap(tile));
            }
        }
    }

つまり、これがランダム値がパターンにマッピングされる方法です。このメソッドはスレッドのコンストラクターで呼び出され、onCreateが呼び出されるたびに呼び出されます。今のところ、リストをクリアして、毎回新しいランダムパターンを作成しています。

...
Resources res = context.getResources();
mBackgroundImages = new ArrayList<Bitmap>();
genMap(res);
...

そして最後に、描画メソッド。BitmapFactory.decodeResourcesを介して単一のビットマップを正常にロードすることはできますが、これを行うと黒い画面が表示されます。

    private void doDraw(Canvas canvas)
    {
        /* Draw the bg.
         * Remember, Canvas objects accumulate.
         * So drawn first = most in the background. */
        if (canvas != null)
        {
            if (mBackgroundImages.size() > 0)
            {
                int tile_width = mBackgroundImages.get(0).getWidth();
                int tile_height = mBackgroundImages.get(0).getHeight();
                for (int y = 0; y < mCanvasHeight / tile_height; y++)
                {
                    for(int x = 0; x < mCanvasWidth / tile_width; x++)
                    {
                         // Draw the Bitmap at the correct position in the list; Y * XWIDTH + X, at pos X * XWIDTH, Y * YWIDTH.
                         canvas.drawBitmap(mBackgroundImages.get((x + y * (mCanvasWidth/tile_width))), x * tile_width, y * tile_height, null);
                    }
                }
            }
        }
    }

助けていただければ幸いです、ありがとう。

4

1 に答える 1

0

次の行の優先順位を再確認しましたか?

((x + y * (mCanvasWidth/tile_width)))

興味深い質問ですが、無地を描いていますか?また、これが実際にビットマップを作成しているかどうかも確認します。

mBackgroundImages.add(Bitmap.createBitmap(tile));

たくさんのビットマップを作成する代わりに、実際には参照を保持できるかもしれませんが、それは後で来る可能性があります

于 2012-10-02T14:23:29.437 に答える