1

次のコードを使用して、ギャラリーからビットマップをロードしています。

 bitmap = (BitmapFactory.decodeFile(picturePath)).copy(Bitmap.Config.ARGB_8888, true);
 bitmap = Bitmap.createScaledBitmap(bitmap, screenWidth, screenHeight, true);
 bitmapCanvas = new Canvas(bitmap);
 invalidate(); // refresh the screen

質問:

最初に完全にデコードしてコピーし、画面の幅と高さに合わせてスケーリングすると、画像の読み込みに非常に時間がかかるようです。とにかくインポートした画像をユーザーに拡大させないので、実際には完全な密度で写真をロードする必要はありません。

そのように、読み込み時間と RAM を削減する方法はありますか? (縮小された画像を直接読み込む) 上記のコーディングをさらに変更するにはどうすればよいですか?

4

2 に答える 2

0

透明度がない場合は、ARGB_8888の代わりにRGB_565を試す価値があるかもしれません。

于 2013-02-03T15:57:23.797 に答える
0

この RAM とロード時間の短縮に対する答えを見つけて、outofmemory他の同様の質問からのエラーを回避してください。

//get importing bitmap dimension
   Options op = new Options();
   op.inJustDecodeBounds = true;
   Bitmap pic_to_be_imported = BitmapFactory.decodeFile(picturePath, op);
   final int x_pic = op.outWidth;
   final int y_pic = op.outHeight;

//The new size we want to scale to
    final int IMAGE_MAX_SIZE= (int) Math.max(DrawViewWidth, DrawViewHeight);

    int scale = 1;
    if (op.outHeight > IMAGE_MAX_SIZE || op.outWidth > IMAGE_MAX_SIZE) 
    {
        scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / 
               (double) Math.max(op.outHeight, op.outWidth)) / Math.log(0.5)));
    }

    final BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;        

//Import the file using the o2 options: inSampleSized
    bitmap = (BitmapFactory.decodeFile(picturePath, o2));
    bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
于 2013-02-13T04:33:22.727 に答える