开发者

Drawing Bitmap as MainScreen Background. How to Stretch Image?

I want the background image to stretch all screen. I simply created a VerticalFieldManager and modified paint method.

   verticalFieldManager = new VerticalFieldManager(VerticalFieldManager.USE_ALL_WIDTH
                            | VerticalFieldManager.NO_HORIZONTAL_SCROLLBAR) {
             public void paint(Graphics graphics) {
                    g开发者_如何学编程raphics.drawBitmap(0, 0, this.getWidth(), this.getHeight(),
                                        backgroundBitmap, 0, 0);
                    super.paint(graphics);
        }

    };

And when i add a ListField to the verticalFieldManager and scroll, background image is not repaint. How can i repaint background when scroll?


Create EncodedImage from your Bitmap, or even directly extract EncodedImage from resources - EncodedImage.getEncodedImageResource("res/bg.png").

Then use this to scale it:

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);
    }
}

Then you may draw the EncodedImage on the Graphics object:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜