Center, Stretch, Tile, Zoom -able Image Background Skin
Posted by: shaun in Flex, Skins, ImagesSo we have the ability in Flex to select an image as the background of a container. However, the only control we get over this image is the ability to specify its size. From my background in .NET, I am accustomed to being able to specify the layout of a background image as one of four options:
- Centered: shows up as its default size, centered in the container
- Stretched: made to fill all area of the background (causing distortion if necessary)
- Tiled: repeats itself to fill entire background area
- Zoomed: image stretched as large as possible while maintaining aspect ratio & centered
So what I’m providing here (right-click for code) is my method of providing this functionality as a skin in Flex.
This should work for any containers in your app, and all you have to do to use it is specify the class as your borderSkin, provide a backgroundImage, and set the backgroundSize style to center, stretch, tile, or zoom.
Possible improvements: allowing a border as well as the image. I haven’t needed a border as well as the image yet, so I haven’t done this.
~Shaun


Entries (RSS)
June 4th, 2008 at 8:49 am
Hey, I just wanted to say thanks. This totally helped me out!
I love your code browser UI too - very slick.
June 4th, 2008 at 2:05 pm
Thanks for posting this! The example only shows a black screen for me, but I was able to get your code working. I was using an example from somewhere else (http://labs.flexcoders.nl/samples/RepeatedBackground2/srcview/index.html) that mostly worked but had bugs where there would be no background if the content was larger than the viewable flash area (ie, scrollbars appeared). I modified yours to work with embedded images.. which reduces the image laoding/caching requirement and simplifies the code a lot… I believe now it contains the best of both worlds! Thanks again.
June 21st, 2008 at 11:14 am
Nick, could you post your modified (simplified) code too? I also want to do the same with an embedded image (actually an SVG rather than a bitmap).