将位图绘制为主屏幕背景。如何拉伸图像?

时间:2011-04-21 07:52:34

标签: blackberry background-image

我希望背景图片能够拉伸所有屏幕。我只是创建了一个VerticalFieldManager和修改过的paint方法。

   verticalFieldManager = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH
                            | VerticalFieldManager.NO_HORIZONTAL_SCROLLBAR) {
             public void paint(Graphics graphics) {
                    graphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(),
                                        backgroundBitmap, 0, 0);
                    super.paint(graphics);
        }

    };

当我将一个ListField添加到verticalFieldManager并滚动时,背景图像不会重新绘制。滚动时如何重新绘制背景?

1 个答案:

答案 0 :(得分:2)

EncodedImage创建Bitmap,甚至直接从资源中提取EncodedImage - EncodedImage.getEncodedImageResource("res/bg.png")

然后用它来缩放它:

public static EncodedImage resize(EncodedImage eImage, int toWidth, 
        int toHeight, boolean keepAspectRatio) {

    int scaleX = Fixed32.div(
        Fixed32.toFP(eImage.getWidth()),
        Fixed32.toFP(toWidth)
    );

    int scaleY = Fixed32.div(
        Fixed32.toFP(eImage.getHeight()), 
        Fixed32.toFP(toHeight)
    );

    if (keepAspectRatio) {
        int scale = (scaleX > scaleY) ? scaleX : scaleY;
        return eImage.scaleImage32(scale, scale);
    } else {
        return eImage.scaleImage32(scaleX, scaleY);
    }
}

然后你可以在EncodedImage对象上绘制Graphics

graphics.drawImage(
    0, 0, 
    encodedImage.getScaledWidth(), ncodedImage.getScaledHeight(), 
    encodedImage, 0, 0, 0
);