Dynamically change the background of a window in Adobe AIR?

By | February 6, 2011

Change the background of your window with your selected image.
Copy and paste the following code to your AIR file and view the result.

Note: Please make sure that you have an image in your application source folder.
Here the image name is “image.jpg” else you will get URL not found error.

xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  creationComplete="onCreationComplete(event)" height="800" width="800" >



            import mx.events.FlexEvent;

            private var bgLoader : Loader;
            private var bgBitmapData : BitmapData;
// set your require background image path
            private var bgImagePath:String = "image.jpg";

/************************************************************************
backgroundImage="assets/myimage.jpg" height="800" width="800" >
</mx:WindowedApplication>
backgroundImage="assets/myimage.jpg" will set as a background Image.
************************************************************************/

            private function onCreationComplete(event:FlexEvent):void
            {
                bgLoader = new Loader ();
                bgLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, bgLoadingComplete, false, 0, true);
  &nbs p;             bgLoader.load (new URLRequest (bgImagePath));
            }
            private function bgLoadingComplete (event:Event):void
            {
                bgBitmapData = new BitmapData (bgLoader.width, bgLoader.height);
                bgBitmapData.draw ( bgLoader );

                bgCanvas.graphics.beginBitmapFill (bgBitmapData);
                bgCanvas.graphics.drawRect (0, 0, stage.stageWidth, stage.stageHeight);
                bgCanvas.graphics.endFill ();
            }

        ]]>


    id="bgCanvas" height="100%" width="100%" />

Leave a Reply

Your email address will not be published. Required fields are marked *