Drawing Bitmap as MainScreen Background. How to St

2019-03-05 18:19发布

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) {
                    graphics.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?

1条回答
老娘就宠你
2楼-- · 2019-03-05 19:01

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
);
查看更多
登录 后发表回答