2016-01-24 85 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

谢谢您的快速响应和我去尝试了这一点,我回来算账! –