0

私はこのような9つの画像ファイルから単一のimageViewを作成する方法から検索しました:

IMG1-IMG2-IMG3

IMG4-IMG5-IMG6

IMG7-IMG8-IMG9

私は私を助けるいくつかの興味深いトピックを見ました。これらの1つは、私のニーズに合う可能性のあるソリューションについて話します。このトピックでは、DimitarDimitrovがこれを提案します。

copyPixelsToBuffer( )、createBitmap()およびsetPixels()。ソース:AndroidのImageViewで2つの画像をレンダリングしますか?

したがって、各画像ファイルから32ビットARGBピクセルを抽出し、setPixels関数を使用して入力できるビットマップを作成できます。問題は、「32ビットARGBを抽出する方法がわからないことです。各画像ファイルからのピクセル」 ..。

私はcanvasとsurfaceViewについても見ましたが、それらを使用することはありません。さらに、最終的なオブジェクトは(ユーザーが望むときに)時々ピンチズームされるだけなので、単一のimageViewを使用して動作させる方が簡単だと思います...

したがって、(UIスレッドの使用を避けるために)AsyncTask内のコードのこの部分から始めましたが、すでにOUTOFMEMORY例外が発生しています

...
@Override
protected Bitmap doInBackground(Void... params) {
     return this.createBigBitmap();
}

public Bitmap createBigBitmap() {

Bitmap pageBitmap = Bitmap.createBitmap(800, 1066, Bitmap.Config.ARGB_8888); // OUT OF MEMORY EXCEPTION

// create an ArrayList of the 9 page Parts
ArrayList<Bitmap> pageParts = new ArrayList<Bitmap>();
for(int pagePartNum = 1; pagePartNum <= 9; pagePartNum++){
    Bitmap pagePartBitmap = getPagePart(pageNum, pagePartNum);
    pageParts.add(pagePartBitmap);
}


// try to copy the content of the 9 bitmaps into a single one bitmap
int[] pixels = null;
            int offsetX = 0, offsetY = 0, pagePartNum = 0;

            for (int x = 0; x < this.nbPageRows; x++) {
                for (int y = 0; y < this.nbPageColumns; y++) {
                    pagePartNum = x * this.nbPageColumns + y;
                    Bitmap pagePartBitmap = pageParts.get(pagePartNum);
                    // read pixels from the pagePartBitmap
                    pixels = new int[pagePartBitmap.getHeight() * pagePartBitmap.getWidth()];
                    pagePartBitmap.getPixels(pixels, 0, pagePartBitmap.getWidth(), 0, 0, pagePartBitmap.getWidth(), pagePartBitmap.getHeight());

                    // compute offsetY
                    if(x == 0)
                        offsetY = 0;
                    if(x == 1)
                        offsetY = pageParts.get(0).getHeight();
                    if(x == 2)
                        offsetY = pageParts.get(0).getHeight() * 2;

                    // compute offsetX
                    if(y == 0)
                        offsetX = 0;
                    if(y == 1)
                        offsetX = pageParts.get(0).getWidth();
                    if(y == 2)
                        offsetX = pageParts.get(0).getWidth() * 2;


                    // write pixels read to the pageBitmap
                    pageBitmap.setPixels(pixels, 0, pagePartBitmap.getWidth(), offsetX, offsetY, pagePartBitmap.getWidth(), pagePartBitmap.getHeight());
                    offsetX += pagePartBitmap.getWidth();
                    offsetY += pagePartBitmap.getHeight();

                }
            }
return pageBitmap;
}

// get a bitmap from one of the 9 existing image file page part
private Bitmap getPagePart(int pageNum, int pagePartNum) {
        String imgFilename = this.directory.getAbsolutePath()
                + File.separator + "z-"
                + String.format("%04d", pageNum)
                + "-" + pagePartNum + ".jpg";
        // ajoute le bitmap de la partie de page
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeFile(imgFilename, opt);
    }

どうもありがとうございます

4

1 に答える 1

0

BitmapRegionDecoderこれを読む

編集

OOM エラーから抜け出すには、androidでの効率的なビットマップ処理を参照してください。次のコードを使用して、ビットマップをデコードします。

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = 4;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
于 2013-02-25T11:45:45.210 に答える