У меня проблема с памятью из-за растровых изображений. Мое приложение представляет собой горизонтальную карусель изображений, которая реализована с помощью ViewPager и фрагментов.Утилизация фрагмента в ViewPager
Так может ли кто-нибудь указать, как перерабатывать фрагмент и где это сделать?
Ниже приведены мои коды
public ImageSliderFragment(){}
public void initializeEvent(OnExpeditionImageViewTouch event){
this.event = event;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.view_pager_single_frame_layout, container, false);
Bundle args = getArguments();
final int pos = args.getInt(AppConstants.BUNDLE_KEY_FOR_FRAGMENT_POSITION);
position = pos;
expedition_image = ((ImageView) rootView.findViewById(R.id.stackView));
try {
expedition_image.setImageBitmap(decodeSampledBitmapFromResource(getResources(), AppConstants.imageResource.get(position), 400, 400));
} catch (Exception e) {
}
return rootView;
}
@Override
public void setExitSharedElementCallback(SharedElementCallback callback) {
super.setExitSharedElementCallback(callback);
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height/2;
final int halfWidth = width/2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight/inSampleSize) > reqHeight
&& (halfWidth/inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
PagerAdapter код
private class CustomViewPagerAdapter extends FragmentStatePagerAdapter {
private Map<Integer, ImageSliderFragment> mPageReferenceMap = new HashMap<>();
public CustomViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
ImageSliderFragment imageSliderFragment = new ImageSliderFragment();
Bundle args = new Bundle();
args.putInt(AppConstants.BUNDLE_KEY_FOR_FRAGMENT_POSITION,position);
imageSliderFragment.setArguments(args);
mPageReferenceMap.put(Integer.valueOf(position),imageSliderFragment);
return imageSliderFragment;
}
@Override
public int getCount() {
return AppConstants.NUM_PAGES;
}
@Override
public int getItemPosition(Object object) {
return /**/POSITION_NONE;//super.getItemPosition(object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//ImageSliderFragment fragment = (ImageSliderFragment)object;
super.destroyItem(container, position, object);
}
public ImageSliderFragment getFragment(int key) {
return mPageReferenceMap.get(key);
}
}
Привет, любой может указать решение для этого. Поскольку у моего приложения есть множество изображений, которые будут загружаться асинхронно, теперь через несколько минут на устройствах с низким ОЗУ он просто закрывает и освобождает память и запускается снова. –