2016-01-24 126 views
0

我有一個包含ImageView的項目的Listview。不幸的是,如果我加載超過20個項目,我正在獲得OME。我正在使用Glide庫將項目加載到ImageViews中。我的問題是,如果有另一種方法來處理這個問題,比從我的服務器加載較小的圖像?Android - 內存不足Glide Library的異常

期待好的答案!

編輯:

public View getView(final int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.post_list_item, parent, false); 
    back = (ImageView) rowView.findViewById(R.id.imageView3); 
    filter = (ImageView) rowView.findViewById(R.id.imageView2); 
    final RelativeLayout deleteContainer = (RelativeLayout) rowView.findViewById(R.id.deleteContainer); 
    final ImageView deletAdd = (ImageView) rowView.findViewById(R.id.imageView1); 
    deletAdd.setScaleX(0.5f); 
    deletAdd.setScaleY(0.5f); 
    View backDelete = (View) rowView.findViewById(R.id.View1); 
    Glide.with(context).load(postList.get(position).getThumb()).diskCacheStrategy(DiskCacheStrategy.RESULT) 
      .override(AppData.width, AppData.height/6).centerCrop().into(back); 
    TextView title = (TextView) rowView.findViewById(R.id.textView1); 
    title.setTypeface(AppData.glogooregular); 
    if (postList.get(position).getThumb() == null) { 
     filter.setVisibility(View.GONE); 
     title.setBackgroundColor(Color.parseColor("#FFFFFF")); 
     title.setTextColor(Color.parseColor("#000000")); 
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title.getLayoutParams(); 
     params.setMargins(0, 0, 0, 0); 
     title.setLayoutParams(params); 
    } else { 
     title.getLayoutParams().height = AppData.height/6; 
    } 
    TextView date = (TextView) rowView.findViewById(R.id.textView2); 
    TextView comments = (TextView) rowView.findViewById(R.id.textView3); 
    title.setText(postList.get(position).getTitle()); 
    SimpleDateFormat month_date = new SimpleDateFormat("MMM"); 
    Calendar cal = DateToCalendar(new Date(Long.parseLong(postList.get(position).getTime()) * 1000)); 
    String month_name = month_date.format(new Date(cal.getTimeInMillis())); 
    date.setText(cal.get(Calendar.DAY_OF_MONTH) + "." + month_name + " - " + cal.get(Calendar.HOUR_OF_DAY) + ":" 
      + cal.get(Calendar.MINUTE) + " Uhr"); ... 

這是我的getView法碼。我可以從Glide LIbery獲取位圖,但我怎麼能縮小它並高效地緩存呢?

回答

1

請張貼您的適配器代碼,以便我們可以看到您的問題到底在哪裏。

我的頭頂上有幾件事你可以做,使位圖加載和處理性能更好,首先我建議將位圖格式切換到RGB_565而不是默認的ARGB_8888,這將需要50減少%的內存! 此外,我會嘗試緩存內存中的圖像,因爲適配器每次請求getView時都會調用加載函數(除非您有isLoaded標誌或其他東西),並且最後我會將位圖加載到ImageView的確切大小它不再需要任何像素。

閱讀本作的位圖處理更深入的瞭解: http://developer.android.com/training/displaying-bitmaps/index.html

+0

謝謝您的快速響應和我去嘗試了這一點,我回來算賬! –