[英]Android - Out of Memory Exception with Glide Library
我有一个包含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中获取位图,但是如何调低位图并对其进行有效缓存呢?
请发布您的适配器代码,以便我们可以确切地了解您的问题出在哪里。
在我头顶上方,您可以做一些事情来使位图的加载和处理性能更好,首先,我建议将位图格式切换为RGB_565而不是默认的ARGB_8888,这将减少50%的内存! 另外,我将尝试在内存上缓存图像,因为适配器每次请求getView时都会调用加载函数(除非您有isLoaded标志或其他东西),最后我将以ImageView的确切大小加载位图,因此它不会占用多余的像素。
阅读此书可更深入地了解位图处理: http : //developer.android.com/training/displaying-bitmaps/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.