If you need to scale images or bitmaps, the class I've pasted below can be very helpful.
ScaleImage.toSize() scales an image vertically and horizontally to the size you specify (ignoring current proportions);
ScaleImage.toWidth() scales an image to the specified width constraining proportions;
ScaleImage.toHeight() scales an image to the specified width constraining proportions;
scaleToFactor is a helper method that the other methods use.
Code:
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.EncodedImage;
public class ScaleImage {
public static EncodedImage toSize(EncodedImage encoded, int newWidth, int newHeight) {
int curWidth = encoded.getWidth();
int curHeight = encoded.getHeight();
int xnumerator = Fixed32.toFP(curWidth);
int xdenominator = Fixed32.toFP(newWidth);
int xscale = Fixed32.div(xnumerator, xdenominator);
int ynumerator = Fixed32.toFP(curHeight);
int ydenominator = Fixed32.toFP(newHeight);
int yscale = Fixed32.div(ynumerator, ydenominator);
return encoded.scaleImage32(xscale, yscale);
}
public static EncodedImage toWidth(EncodedImage encoded, int newWidth) {
return scaleToFactor(encoded, encoded.getWidth(), newWidth);
}
public static EncodedImage toHeight(EncodedImage encoded, int newHeight) {
return scaleToFactor(encoded, encoded.getHeight(), newHeight);
}
public static EncodedImage scaleToFactor(EncodedImage encoded, int curSize, int newSize) {
int numerator = Fixed32.toFP(curSize);
int denominator = Fixed32.toFP(newSize);
int scale = Fixed32.div(numerator, denominator);
return encoded.scaleImage32(scale, scale);
}
}