Drawing Bitmap as MainScreen Background. How to Stretch Image?

前端 未结 1 541
半阙折子戏
半阙折子戏 2021-01-28 07:56

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

   verticalFieldManager = new VerticalField         


        
相关标签:
1条回答
  • 2021-01-28 08:46

    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 讨论(0)
提交回复
热议问题