4

みんな、5つの異なるビットマップからビットマップを作成するソリューションがあります。ただし、このソリューションをより高速にしたいと考えています。同じことを行う別の簡単で迅速な方法を見つけることができませんでした。

private void createImage() {
    // Magic numbers from image files
    int numberOfImages = 5;
    int imgWidth = 125;
    int imgHeight = 300;
    int totalwidth = imgWidth * numberOfImages;
    int totalheight = imgHeight;


    img = Bitmap.createBitmap(totalwidth, totalheight, Bitmap.Config.ARGB_8888);

    for (int i = 0; i < numberOfImages; i++) {
        Bitmap imgFile = BitmapFactory.decodeResource(context.getResources(), context.getResources().getIdentifier(
                "f" + i, "drawable", context.getPackageName()));
        for (int x = 0; x < imgWidth; x++) {
            for (int y = 0; y < imgHeight; y++) {
                int targetX = x + (i * imgWidth);
                int targetY = y;
                int color = imgFile.getPixel(x, targetY);
                img.setPixel(targetX, targetY, color);
            }
        }
    }


    img = Bitmap.createScaledBitmap(img, screenWidth, screenHeight, true);

}
4

3 に答える 3

4

あなたはほとんどそこにいます。それらすべてに十分な幅のビットマップを作成しました。

次に行う必要があるのは、ビットマップを一度に 1 つずつ直接描画することですが、ピクセルごとではなく、ビットマップ全体を一度に描画します。

これは、 something call を使用して実行できますCanvas。これは、ビットマップに物事を描画するのに役立ちます。

img = Bitmap.createBitmap(totalwidth, totalheight, Bitmap.Config.ARGB_8888);
Canvas drawCanvas = new Canvas(img);

for (int i = 0; i < numberOfImages; i++) {
    Bitmap imgFile = BitmapFactory.decodeResource(context.getResources(), context.getResources().getIdentifier(
            "f" + i, "drawable", context.getPackageName()));

          // Draw bitmap onto combo bitmap, at offset x, y.
          drawCanvas.drawBitmap(imgFile, i * imgWidth, 0, null);


}
于 2012-12-01T22:14:11.050 に答える
1

Bitmap.getPixels()Bitmap.setPixels()メソッドが役立つかもしれないと思います。

private void createImage() {
    // Magic numbers from image files
    int numberOfImages = 5;
    int imgWidth = 125;
    int imgHeight = 300;
    int totalwidth = imgWidth * numberOfImages;
    int totalheight = imgHeight;


    img = Bitmap.createBitmap(totalwidth, totalheight, Bitmap.Config.ARGB_8888);

    int buf[] = new int[totalwidth * totalheight];

    for (int i = 0; i < numberOfImages; ++i) {
        Bitmap imgFile = BitmapFactory.decodeResource(context.getResources(),
            context.getResources().getIdentifier(
                "f" + i, "drawable", context.getPackageName()));
        imgFile.getPixels(buf, i*imgWidth, totalwidth, 0, 0, imgWidth, imgHeight);
    }
    img.setPixels(buf, 0, totalwidth, 0, 0, totalwidth, totalheight);

    img = Bitmap.createScaledBitmap(img, screenWidth, screenHeight, true);

}

私はそれを試していませんが、 ドキュメントに基づいて、それはうまくいくはずだと思います。6つの関数呼び出しだけですべての作業を行うので、かなり高速になると思います。これらの関数は、配列をループするよりもはるかに巧妙なことを行うと仮定しています(ダイレクトメモリコピーなど)。

于 2012-12-01T22:24:12.217 に答える