Android ViewPager:位图大小超出VM预算
我有一个应用程序,我使用ViewPager,在每个页面中设置一个“片段”图像 当我在图像25后滚动查看页面时,会出现内存不足异常,但每次转到下一页时,我都会删除该图像 检查以下代码:Android ViewPager:位图大小超出VM预算,android,image,exception,Android,Image,Exception,我有一个应用程序,我使用ViewPager,在每个页面中设置一个“片段”图像 当我在图像25后滚动查看页面时,会出现内存不足异常,但每次转到下一页时,我都会删除该图像 检查以下代码: public void destroyItem(View collection, int position, Object view) { Log.d("**", "remove image"); ((ViewPager) collection).removeView((ImageView) vie
public void destroyItem(View collection, int position, Object view) {
Log.d("**", "remove image");
((ViewPager) collection).removeView((ImageView) view);
} //method: remove item from pager adapter
我尝试从此处编写代码删除ImageView,但未能解决问题:
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
有两个可能的问题:
recycle()
。这确保GC可以回收内存。但是,如果在任何其他视图中重复使用这些位图,请小心BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //reduce image size