Android - Glide库的内存异常

时间:2016-01-24 08:55:45

标签: java android listview out-of-memory android-glide

我有一个包含ImageView的项目的Listview。不幸的是,如果我装载的物品超过20件,我会得到一个OME。我正在使用Glide库将项目加载到ImageViews中。我的问题是,是否有另一种方法来处理这个问题,而不是从我的服务器加载较小的图像?

期待很好的答案!

编辑:

 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 个答案:

答案 0 :(得分:2)

请发布您的适配器代码,以便我们了解您的问题究竟在哪里。

在我的脑海中有一些你可以做的事情来使位图的加载和处理表现更好,首先我建议将位图格式转换为RGB_565而不是默认的ARGB_8888,它需要50%少记忆! 此外,我会尝试在内存上缓存图像,因为每次请求getView时,适配器都会调用加载函数(除非你有一个isLoaded标志或者什么),最后我会以ImageView的确切大小加载位图,所以它不再需要像素。

阅读本文以深入理解位图处理: http://developer.android.com/training/displaying-bitmaps/index.html