android記憶體優化

2021-07-22 08:33:07 字數 1660 閱讀 3528

(1)、使用option進行壓縮。

public static bitmap createimagethumbnail(string filepath)catch (exception e)   

return bitmap;  

}    

public static int computesamplesize(bitmapfactory.options options, int minsidelength, int maxnumofpixels)  

} else  

return roundedsize;  

}    

private static int computeinitialsamplesize(bitmapfactory.options options,int minsidelength, int maxnumofpixels)  

if ((maxnumofpixels == -1) && (minsidelength == -1)) else if (minsidelength == -1) else  

}

(2)、使用lrucache      

1、獲取所需的快取空間。(重寫sizeof方法)    

private lrucachemmemorycache;

private lrucacheutils()

@override

protected void entryremoved(boolean evicted, string key,

bitmap oldvalue, bitmap newvalue)

};}

2、實現操作

public void clearcache() 

mmemorycache = null;}}

public synchronized void addbitmaptomemorycache(string key, bitmap bitmap) else

log.w(tag, "the res is aready exits");

}public synchronized bitmap getbitmapfrommemcache(string key)

return null;

}/**

* 移除快取

* * @param key

*/public synchronized void removeimagecache(string key)

}}

(3)、軟引用(已經不推崇使用,因為gc主要處理物件就是軟引用和弱引用,現在就用lrucache)

(4)、選擇rgb_565 (乙個畫素兩個byte)  代替 alpha_8(1)、argb_444(2)、argb_8888(4)

(5)、使用完要及時recycle

二、變數    1、盡量使用final static代替static 

2、能直接訪問的字段就不要使用get、set了。  

3、全域性變數盡量少一些。 

4、能放在外面的東西盡量不要放在for迴圈當中。   

以上就是一些基礎的吧,希望對大家有所幫助。

android記憶體優化

android因其系統的特殊性,安裝的軟體預設都安裝到記憶體中,所以隨著使用者安裝的軟體越來越多,可供執行的程式使用的記憶體越來越小,這就要求我們在開發android程式時,盡可能的少占用記憶體。根據我個人的開發經驗總結了如下幾點優化記憶體的方法 建立或其他方式獲得的物件如不再使用,則主動將其置為n...

Android效能優化 記憶體優化

記憶體優化 下面介紹一下常用的一些記憶體優化的方法 從慕課網學來的 資料結果優化 1,如果經常使用字串拼接的話,要用stringbuilder,避免直接使用使用 的方式 2,使用arraymap,sparsearray代替hashmap 3,避免記憶體抖動的情況 物件復用 1,復用系統自帶資源 2,...

android記憶體優化 Bitmap

眾說周知在android開發中bitmap是吃記憶體的大戶,所以談到android記憶體優化首先得對bitmap進行優化 優化方法主要有 1 對於無用的bitmap要及時進行 釋放bitmap的方法 if bitmap null bitmap.isrecycled bitmap.recycle 方法...