Blurred imageview after zooming while using Universal Image Loader in android

谁说我不能喝 提交于 2019-12-18 13:52:29

问题


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);

回答1:


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();
                }
            }
        }
        ...
    }
    


来源:https://stackoverflow.com/questions/12634088/blurred-imageview-after-zooming-while-using-universal-image-loader-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!