com.nostra13.universalimageloader.utils
Class ImageSizeUtils

java.lang.Object
  extended by com.nostra13.universalimageloader.utils.ImageSizeUtils

public final class ImageSizeUtils
extends Object

Provides calculations with image sizes, scales

Since:
1.8.3
Author:
Sergey Tarasevich (nostra13[at]gmail[dot]com)

Method Summary
static int computeImageSampleSize(ImageSize srcSize, ImageSize targetSize, ViewScaleType viewScaleType, boolean powerOf2Scale)
          Computes sample size for downscaling image size (srcSize) to view size (targetSize).
static float computeImageScale(ImageSize srcSize, ImageSize targetSize, ViewScaleType viewScaleType, boolean stretch)
          Computes scale of target size (targetSize) to source size (srcSize).
static ImageSize defineTargetSizeForView(android.widget.ImageView imageView, int maxImageWidth, int maxImageHeight)
          Defines target size for image.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

defineTargetSizeForView

public static ImageSize defineTargetSizeForView(android.widget.ImageView imageView,
                                                int maxImageWidth,
                                                int maxImageHeight)
Defines target size for image. Size is defined by target view parameters, configuration parameters or device display dimensions.
Size computing algorithm:
1) Get the actual drawn getWidth() and getHeight() of the View. If view haven't drawn yet then go to step #2.
2) Get layout_width and layout_height. If both of them haven't exact value then go to step #3.
3) Get maxWidth and maxHeight. If both of them are not set then go to step #4.
4) Get maxImageWidth param (maxImageWidthForMemoryCache) and maxImageHeight param (maxImageHeightForMemoryCache). If both of them are not set (equal 0) then go to step #5.
5) Get device screen dimensions.


computeImageSampleSize

public static int computeImageSampleSize(ImageSize srcSize,
                                         ImageSize targetSize,
                                         ViewScaleType viewScaleType,
                                         boolean powerOf2Scale)
Computes sample size for downscaling image size (srcSize) to view size (targetSize). This sample size is used during decoding image to bitmap.

Examples:
 srcSize(100x100), targetSize(10x10), powerOf2Scale = true -> sampleSize = 8
 srcSize(100x100), targetSize(10x10), powerOf2Scale = false -> sampleSize = 10
 
 srcSize(100x100), targetSize(20x40), viewScaleType = FIT_INSIDE -> sampleSize = 5
 srcSize(100x100), targetSize(20x40), viewScaleType = CROP       -> sampleSize = 2
 

The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1.

Parameters:
srcSize - Original (image) size
targetSize - Target (view) size
viewScaleType - Scale type for placing image in view
powerOf2Scale - true - if sample size be a power of 2 (1, 2, 4, 8, ...)
Returns:
Computed sample size

computeImageScale

public static float computeImageScale(ImageSize srcSize,
                                      ImageSize targetSize,
                                      ViewScaleType viewScaleType,
                                      boolean stretch)
Computes scale of target size (targetSize) to source size (srcSize).

Examples:
 srcSize(40x40), targetSize(10x10) -> scale = 0.25
 
 srcSize(10x10), targetSize(20x20), stretch = false -> scale = 1
 srcSize(10x10), targetSize(20x20), stretch = true  -> scale = 2
 
 srcSize(100x100), targetSize(20x40), viewScaleType = FIT_INSIDE -> scale = 0.2
 srcSize(100x100), targetSize(20x40), viewScaleType = CROP       -> scale = 0.4
 

Parameters:
srcSize - Source (image) size
targetSize - Target (view) size
viewScaleType - Scale type for placing image in view
stretch - Whether source size should be stretched if target size is larger than source size. If false then result scale value can't be greater than 1.
Returns:
Computed scale


Copyright © 2011-2013. All Rights Reserved.