歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發:如何解決Bitmap OutOfMemory?

Android開發:如何解決Bitmap OutOfMemory?

日期:2017/3/1 11:16:29   编辑:Linux編程

看了些許文章,有很多都是用到了BitmapFactory.Options,設置裡面的一些參數來實現圖片的壓縮。

現在有一個更好的方法來解決Bitmap OutOfMemory的問題,例比如你的原始bitmap只有176*144,但是畫面更新卻要352*288,那麼每次你都要通過BitmapFactory來進行拉伸。

  1. Matrix matrix = new Matrix();
  2. float Scale_Width =352;
  3. float Scale_Height = 288;
  4. matrix.postScale(Scale_Width, Scale_Height);
  5. Bitmap temp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

然後只要拉伸的過程中,程序就會多分配一塊內存來存儲拉伸的圖像.

那麼就極有可能會出現VMbort OutOfMemory,那麼怎麼解決了,如果想把176*144的圖像換成352*288的圖像,那麼你應該這樣做:

  1. Rect rect = new Rect (0,0,176,144);
  2. RectF rectf = new RectF(0,0,352,288);
  3. canvas.drawBitmap(bitmap_176,rect,rectf,null);

這樣就解決了OOM的問題,不會產生新的內存.

Copyright © Linux教程網 All Rights Reserved