Android - Glide 库出现内存不足异常
Android - Out of Memory Exception with Glide Library
我有一个 Listview,其中的项目包含 ImageView。不幸的是,如果我加载超过 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 获取位图,但我如何才能将其缩小并有效地缓存它?
请 post 您的适配器代码,以便我们可以确定您的问题出在哪里。
在我的脑海中,您可以做一些事情来使位图的加载和处理性能更好,首先我建议将位图格式切换为 RGB_565 而不是默认的 ARGB_8888,这样会少占用50%的内存!
此外,我会尝试在内存中缓存图像,因为每次请求 getView 时适配器都会调用加载函数(除非你有一个 isLoaded 标志或其他东西),最后我会以 ImageView 的确切大小加载位图,所以它不会占用比需要更多的像素。
阅读此内容以更深入地了解位图处理:
http://developer.android.com/training/displaying-bitmaps/index.html
我有一个 Listview,其中的项目包含 ImageView。不幸的是,如果我加载超过 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 获取位图,但我如何才能将其缩小并有效地缓存它?
请 post 您的适配器代码,以便我们可以确定您的问题出在哪里。
在我的脑海中,您可以做一些事情来使位图的加载和处理性能更好,首先我建议将位图格式切换为 RGB_565 而不是默认的 ARGB_8888,这样会少占用50%的内存! 此外,我会尝试在内存中缓存图像,因为每次请求 getView 时适配器都会调用加载函数(除非你有一个 isLoaded 标志或其他东西),最后我会以 ImageView 的确切大小加载位图,所以它不会占用比需要更多的像素。
阅读此内容以更深入地了解位图处理: http://developer.android.com/training/displaying-bitmaps/index.html