簡體   English   中英

當適配器在列表視圖中消耗內存時,要么將適配器設置為列表視圖,要么將值設置為適配器

[英]when adapter consume memory in list-view either setting adapter to listview or setting values to the adapter

我有一個列表適配器

adapter = new SimpleCardStackAdapter(this);

       // System.gc(); i should call this function here or just before set adapter calling
        int i=0;
        if(cardModels.size() > VISIBLE_NUMBER_OF_CARDS){
            i = cardModels.size()-VISIBLE_NUMBER_OF_CARDS;
        }
        for ( ;i < cardModels.size(); i++) {
            //if(i<VISIBLE_NUMBER_OF_CARDS){
            adapter.add(cardModels.get(i));
            //}else{
            //    break;
            //}
        }

 // System.gc(); i should call this function or here 
        mCardContainer.setAdapter(adapter);

我的問題是適配器在setadapter或創建適配器期間消耗內存的時候

即時通訊面臨內存不足錯誤...適配器項具有一些大的位圖,在內存消耗操作發生之前,我必須手動調用垃圾回收器,謝謝。

您可以使用以下函數來獲取與要加載圖像的圖像視圖大小相對應的位圖...這甚至可以幫助防止為較小的圖像視圖創建較大的位圖。

public static Bitmap decodeSampledBitmapFromResource(String filepath,
        int reqWidth, int reqHeight) {

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

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filepath, 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) {

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) > reqHeight
            && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
    }
}

return inSampleSize;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM