我遇到問題。我的主菜單/開始屏幕有一個大圖形。這會導致一些舊設備的內存不足異常。這是一個分辨率爲1920 * 1080的PNG文件,我使用ImageFormat RGB565。你有什麼想法,我可以減少使用的內存?Android載入圖形
1
A
回答
0
如前所述你可以按比例縮小圖片,但如果你因爲某些原因不想或不能做,你可能要檢查該鏈接:
http://developer.android.com/training/displaying-bitmaps/index.html
你也可以添加
largeHeap=true
要增加內存一點點,但它只會在新設備上運行的Android 2.x的不支持
0
這是你AR是什麼Ë尋找:
BitmapFactory.Options.inSampleSize
如果設置爲true,則生成位圖將撥出其 像素,使得他們可以在系統需要回收 內存中清除。在這種情況下,當需要再次訪問像素 (例如,繪製位圖,調用getPixels())時,它們將自動重新解碼爲 。爲了進行重新解碼,位圖必須 可以通過共享對輸入的引用或對其進行復制來訪問編碼數據。這種區別是由 inInputShareable控制的。如果這是真的,那麼位圖可以對輸入保持淺的 引用。如果這是錯誤的,那麼位圖將 顯式地複製輸入數據,並保留它。即使允許共享 ,實現仍可能決定對輸入數據進行深層複製。
來源: http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize
因此,基本上可以調整基於屏幕分辨率和可用的RAM inSampleSize
讓你總是有位圖的足夠版本給定設備。這將防止發生OutOfMemoryError
錯誤。
下面是如何利用它的一個例子:
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 = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height/(float) reqHeight);
final int widthRatio = Math.round((float) width/(float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
來源:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html 還有那個鏈接下一些更多的信息,所以我會建議您檢查一下。
相關問題
- 1. Chartkick圖形不斷載入
- 2. 從@ android/drawable載入圖像
- 3. android有效載入位圖
- 4. 從uri載入圖像android
- 5. Android:載入多張圖片
- 6. Android。 libgdx。加載大量圖形
- 7. Android WebView不會加載JavaScript圖形
- 8. Android位圖圖形
- 9. 條形圖android
- 10. Android圖形
- 11. 圖形爲Android
- 12. Android位圖形狀菱形
- 13. Android圓形浮動圖形
- 14. Android圖形制作線條條形圖
- 15. android列表查看圖片載入中
- 16. 從iframe載入Chrome Android意圖
- 17. 預先載入圖像的android
- 18. Android aChartEngine條形圖
- 19. Android圖形佈局
- 20. android graphview條形圖
- 21. Android圖形效果
- 22. Android Facebook圖形API
- 23. Android 2D圖形庫
- 24. 圖形API在android
- 25. Android 3D條形圖
- 26. 的Android製圖/圖形
- 27. Android圖形,動態視圖
- 28. ANDROID上的圖形/圖塊
- 29. 以圖形導入圖像
- 30. 加載圖形文件
縮小圖像的大小。爲什麼您需要爲小型Android設備提供這種尺寸的圖像? – syb0rg 2013-02-24 20:14:46