com.herroworld.imagegrabber
Class ImageResizer

java.lang.Object
  extended by com.herroworld.imagegrabber.ImageWorker
      extended by com.herroworld.imagegrabber.ImageResizer
Direct Known Subclasses:
ImageGrabber

public class ImageResizer
extends ImageWorker

A simple subclass of ImageWorker that resizes images from resources given a target width and height. Useful for when the input images might be too large to simply load directly into memory.


Constructor Summary
ImageResizer(Context context, int imageSize)
          Initialize providing a single target image size (used for both width and height);
ImageResizer(Context context, int imageWidth, int imageHeight)
          Initialize providing a single target image size (used for both width and height);
 
Method Summary
static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
          Calculate an inSampleSize for use in a BitmapFactory.Options object when decoding bitmaps using the decode* methods from BitmapFactory.
static Bitmap decodeSampledBitmapFromFile(java.lang.String filename, int reqWidth, int reqHeight)
          Decode and sample down a bitmap from a file to the requested width and height.
static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
          Decode and sample down a bitmap from resources to the requested width and height.
 void setImageSize(int size)
          Set the target image size (width and height will be the same).
 void setImageSize(int width, int height)
          Set the target image width and height.
 
Methods inherited from class com.herroworld.imagegrabber.ImageWorker
cancelWork, getImageCache, loadImage, setAlwaysFetchFromServer, setExitTasksEarly, setFadeInTime, setImageCache, setImageFadeIn, setLoadingImage
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImageResizer

public ImageResizer(Context context,
                    int imageWidth,
                    int imageHeight)
Initialize providing a single target image size (used for both width and height);

Parameters:
context -
imageWidth -
imageHeight -

ImageResizer

public ImageResizer(Context context,
                    int imageSize)
Initialize providing a single target image size (used for both width and height);

Parameters:
context -
imageSize -
Method Detail

setImageSize

public void setImageSize(int width,
                         int height)
Set the target image width and height.

Parameters:
width -
height -

setImageSize

public void setImageSize(int size)
Set the target image size (width and height will be the same).

Parameters:
size -

decodeSampledBitmapFromResource

public static Bitmap decodeSampledBitmapFromResource(Resources res,
                                                     int resId,
                                                     int reqWidth,
                                                     int reqHeight)
Decode and sample down a bitmap from resources to the requested width and height.

Parameters:
res - The resources object containing the image data
resId - The resource id of the image data
reqWidth - The requested width of the resulting bitmap
reqHeight - The requested height of the resulting bitmap
Returns:
A bitmap sampled down from the original with the same aspect ratio and dimensions that are equal to or greater than the requested width and height

decodeSampledBitmapFromFile

public static Bitmap decodeSampledBitmapFromFile(java.lang.String filename,
                                                 int reqWidth,
                                                 int reqHeight)
Decode and sample down a bitmap from a file to the requested width and height.

Parameters:
filename - The full path of the file to decode
reqWidth - The requested width of the resulting bitmap
reqHeight - The requested height of the resulting bitmap
Returns:
A bitmap sampled down from the original with the same aspect ratio and dimensions that are equal to or greater than the requested width and height

calculateInSampleSize

public static int calculateInSampleSize(BitmapFactory.Options options,
                                        int reqWidth,
                                        int reqHeight)
Calculate an inSampleSize for use in a BitmapFactory.Options object when decoding bitmaps using the decode* methods from BitmapFactory. This implementation calculates the closest inSampleSize that will result in the final decoded bitmap having a width and height equal to or larger than the requested width and height. This implementation does not ensure a power of 2 is returned for inSampleSize which can be faster when decoding but results in a larger bitmap which isn't as useful for caching purposes.

Parameters:
options - An options object with out* params already populated (run through a decode* method with inJustDecodeBounds==true
reqWidth - The requested width of the resulting bitmap
reqHeight - The requested height of the resulting bitmap
Returns:
The value to be used for inSampleSize