Question

I want to display set of images in horizontal viewpager .I have used the Universal Image Loader Everything is fine.But after zooming, quality of images have lost even for big images.

The Size of image is 1000*1000

I have configured with the following settings

Options options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.ic_launcher)
        .cacheOnDisc()
        .cacheInMemory()
        .imageScaleType(ImageScaleType.POWER_OF_2)
        .build();

config = new ImageLoaderConfiguration.Builder(ctx)
        .defaultDisplayImageOptions(options)
        .build();
ImageLoader.getInstance().init(config);
Was it helpful?

Solution

Universal Image Loader keeps reduced images in memory to save memory. Size of every reduced image is calculated and it depends on target ImageView for this image (android:layout_width, android:layout_height, android:maxWidth, android:maxHeight parameters, android:scaleType, device screen size are considered).

By default maximum target size for every image is device's screen size. So you have images of size similar to device screen size and when you zooming you see bad quality.

So you should load in ImageView full-sized images (for zooming support without quality loss). Set your own maximum size for cached images in memory:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        ...
        .memoryCacheExtraOptions(1200, 1000) // maximum width and height of your images
        ...
        .build();

In this case you will load large Bitmaps in memory so I recommend not to cache them in memory. Disable memory caching for these images (don't call .cachenMemory() in DisplayImageOptions) and set .imageScaleType(ImageScaleType.EXACT). You can disable them globally (in .defaultDisplayImageOptions(...)) or for every display task (imageLoader.displayImage(...))

TO PREVENT OOM:

  1. Disable memory caching for these images (don't call .cachenMemory() in DisplayImageOptions)
  2. Set .imageScaleType(ImageScaleType.EXACT)
  3. Set .threadPoolSize(1) (as last try)
  4. Recycle Bitmaps in adapter:

    private class ImagePagerAdapter extends PagerAdapter {
        ...
        @Override
            public void destroyItem(View container, int position, Object object) {
            View view = (View) object;
            ((ViewPager) container).removeView(view);
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
            if (bd != null) {
                Bitmap bmp = bd.getBitmap();
                if (bmp != null) {
                    bmp.recycle();
                }
            }
        }
        ...
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top