黑莓 - 如何调整图像大小?

Boh*_*ian 8 graphics drawing blackberry custom-controls image-scaling

我想知道我们是否可以调整图像大小.假设我们想要在黑莓屏幕上绘制尺寸为100 x 100的200x200实际尺寸的图像.

谢谢

Skr*_*rud 10

你可以简单地使用这个EncodedImage.scaleImage32()方法做到这一点.您需要为其提供要扩展宽度和高度的因子(作为a Fixed32).

下面是一些示例代码,它通过使用RIM Fixed32类将原始图像大小除以所需大小来确定宽度和高度的比例因子.

public static EncodedImage resizeImage(EncodedImage image, int newWidth, int newHeight) {
    int scaleFactorX = Fixed32.div(Fixed32.toFP(image.getWidth()), Fixed32.toFP(newWidth));
    int scaleFactorY = Fixed32.div(Fixed32.toFP(image.getHeight()), Fixed32.toFP(newHeight));
    return image.scaleImage32(scaleFactorX, scaleFactorY);
}
Run Code Online (Sandbox Code Playgroud)

如果你有幸成为OS 5.0的开发人员,Marc发布了一个新API的链接,它比我上面描述的更清晰,更通用.例如:

public static Bitmap resizeImage(Bitmap originalImage, int newWidth, int newHeight) {
    Bitmap newImage = new Bitmap(newWidth, newHeight);
    originalImage.scaleInto(newImage, Bitmap.FILTER_BILINEAR, Bitmap.SCALE_TO_FILL);
    return newImage;
}
Run Code Online (Sandbox Code Playgroud)

(当然,您可以根据需要替换过滤器/缩放选项.)