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 int computeMinImageSampleSize(ImageSize srcSize)
          Computes minimal sample size for downscaling image so result image size won't exceed max acceptable OpenGL texture size.
static ImageSize defineTargetSizeForView(ImageAware imageAware, ImageSize maxImageSize)
          Defines target size for image aware view.
 
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(ImageAware imageAware,
                                                ImageSize maxImageSize)
Defines target size for image aware view. Size is defined by target view parameters, configuration parameters or device display 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

computeMinImageSampleSize

public static int computeMinImageSampleSize(ImageSize srcSize)
Computes minimal sample size for downscaling image so result image size won't exceed max acceptable OpenGL texture size.
We can't create Bitmap in memory with size exceed max texture size (usually this is 2048x2048) so this method calculate minimal sample size which should be applied to image to fit into these limits.

Parameters:
srcSize - Original image size
Returns:
Minimal 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-2014. All Rights Reserved.