我正在开发一个应用程序,其中包含带有大图像的各种 fragment Activity 。我不想跨 Activity 将这些图像保存在内存中,因此我使用 setImageBitmap(null) 取消绑定(bind)图像。并在 onResume() 中 retore 还原图像。 但是,如果应用程序具有完整引用,为什么还要对在 xml 中设置的图像做同样的事情呢? 在离开 Activity 时强制 fragment 中的所有图像不保留在堆中并在从其他 Activity 返回时强制重新创建 View 的最佳方法是什么? 为什么我设置“setoffscreenPageLimit(1)”并且三个 fragment 的图像始终保留在堆中? 3 是最小值? 我正在使用 fragmentStatePagerAdapter。
谢谢!
最佳答案
通过“setoffscreenPageLimit(1)”,您可以告诉 ViewPager 也将下一个 Fragment 加载到内存中。我的意思是,加载当前 Fragment 的左侧和右侧的 Fragment。
并且不要尝试设置“setoffscreenPageLimit(0)”,这是行不通的。
-编辑-
此外,您可以像这样以编程方式设置图像,而不是通过 xml 设置它们。
ImageView imageView = (ImageView)findViewById(R.id.imageview);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
imageView.setImageBitmap(bitmap);
关于android - 为什么图像由android :src in xml remain stored in heap?设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460106/