How can I resize a picture before displaying it in a view with Android Universal Image Loader?
Solution 1
Read Java docs attentively:
Options.inSampleSize
of incoming options will NOT be considered. Library calculate the most appropriate sample size itself according toimageScaleType(...)
options.
Also look into ImageSizeUtil.defineTargetSizeForView(ImageView imageView, int maxImageWidth, int maxImageHeight)
which defines target size for image:
Size is defined by target view parameters, configuration parameters or device display dimensions. Size computing algorithm:
- Get the actual drawn
getWidth()
andgetHeight()
of the View. If view haven't drawn yet then go to step #2.- Get
layout_width
andlayout_height
. If both of them haven't exact value then go to step #3.- Get
maxWidth
andmaxHeight
. If both of them are not set then go to step #4.- Get
maxImageWidth
param (maxImageWidthForMemoryCache) andmaxImageHeight
param (maxImageHeightForMemoryCache). If both of them are not set (equal 0) then go to step #5.- Get device screen dimensions.
UIL defines result Bitmap size according to imageScaleType
and targetSize
(and ImageView
's scaleType).
Solution 2
Just try this:
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.blank)
.showImageForEmptyUri(R.drawable.no_image)
.cacheInMemory()
.cacheOnDisc()
.decodingOptions(resizeOptions)
.postProcessor(new BitmapProcessor() {
@Override
public Bitmap process(Bitmap bmp) {
return Bitmap.createScaledBitmap(bmp, 300, 300, false);
}
})
.build();
Solution 3
You can set the imageview height and with in xml. You image will fit exactly to the mentioned height and width. Your downloaded image fits to the height and width mentioned in your xml.
You can also have a look at the issue which is similar to yours.
https://github.com/nostra13/Android-Universal-Image-Loader/issues/183
Related videos on Youtube
John F
Updated on January 04, 2020Comments
-
John F over 4 years
I successfully applied the Universal Image Loader library (1.8.3 version) to my app, and I'm trying to resize the image before displaying it in the gridview item (because sometime image is too big to cache it in memory.)
Here is what I am trying:
... BitmapFactory.Options resizeOptions = new BitmapFactory.Options(); resizeOptions.inSampleSize = 3; // decrease size 3 times resizeOptions.inScaled = true; options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.blank) .showImageForEmptyUri(R.drawable.no_image) .cacheInMemory() .cacheOnDisc() .decodingOptions(resizeOptions) .build(); ...
This code doesn't make image 3 times smaller for some reason.
Does someone have a better way to resize an image by exactly the specified density?
-
Blundell about 11 yearsNovoda ImageLoader will do automatic thumbnails and tiered caching for you :-) github.com/novoda/ImageLoader
-
-
John F about 11 yearsThe imageScaleType and defineTargetSizeForView are methods for image displaying but I need solution to resize image before putting it in the cache. For example I have grid view with 2 columns (each grid item is square view with imageview 120dip side inside) and I have two variants of image on a web server, 120x80 px and 640x480 px. The small image is too small for app image view but therefore your lib cache it very well. If I use large images then imageview looks perfect but lib can't put images to cache and always re-download them during gridview scrolling.
-
nostra13 about 11 years@user1328818 "If I use large images then imageview looks perfect but lib can't put images to cache..." What do mean by that?
-
John F about 11 yearsok, let me periphrases, if I use small images (120x80) then your library caches them very well and scrolling through my grid view I that images gets from cache after fetching from web. But if use big images (640x480) your library doesn't cache them and images loads each time I scroll through the grid view.
-
nostra13 about 11 yearsUIL doesn't cache images only if don't say to do it. Enable
.cacheInMemory()
and/or 'cacheOnDisc()' in display options and your images will be cached. Also consider memory cache size. Memory cache is limited so some bitmaps can be deleted if cache size exceeds the limit.