Search code examples
tabris

Scale background image during screen rotation in android using Tabris


I have following server side code in the top level page to set background image for all pages in the app.

@Override
public void createContent(final Composite parent, final PageData oData) {
    Image bg = ResourceManager.getImage( LnfSettings.IMAGE_PAGE_BACKGROUND );

    Composite comp = parent.getParent();
    int width = comp.getDisplay().getClientArea().width;
    int height = comp.getDisplay().getClientArea().height;

    comp.setBackgroundImage( new Image( bg.getDevice(), bg.getImageData().scaledTo(
                    width, height ) ) );

    ... more code here to create layout and contents
}

Above code correctly sets background for all pages and also scaled image to fit with the different screen sizes. But if I rotate the screen, image doesn't get scaled according to the new screen dimension. How to deal with this issue? I am using Tabris 1.4.


Solution

  • You can add a resize listener to your composite like this:

    comp.addListener(SWT.Resize, listenerComp);
    
    Listener listenerComp = new Listener() {
        @Override
        public void handleEvent(Event event) {
            ...
        }
    };
    

    Just get sure you cache your scaled Images so you do not scale and create new images on every rotation.