Drawing Bitmap as MainScreen Background. How to Stretch Image?

夙愿已清 提交于 2020-02-16 06:23:54

问题


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:


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


来源:https://stackoverflow.com/questions/5741145/drawing-bitmap-as-mainscreen-background-how-to-stretch-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!