Changing a VBox container’s background alpha in Flex

by Peter deHaan on January 5, 2008

in VBox

The following example shows how you can set both an background image and background color and change its alpha to for a Flex VBox container by setting the backgroundImage, backgroundColor, and backgroundAlpha styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/05/changing-a-vbox-containers-background-alpha-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        .containerVBox {
            backgroundColor: haloBlue;
            borderThickness: 1;
            borderColor: black;
            borderStyle: solid;
            paddingLeft: 5;
            paddingRight: 5;
            paddingTop: 5;
            paddingBottom: 5;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            [Bindable]
            [Embed("Fx.png")]
            private var flexLogo:Class;

            private function backImage_change(evt:Event):void {
                if (backImage.selected) {
                    vBox.setStyle("backgroundImage", flexLogo);
                } else {
                    vBox.setStyle("backgroundImage", null);
                }
            }

            private function backColor_change(evt:Event):void {
                if (backColor.selected) {
                    vBox.setStyle("backgroundColor", colorPicker.selectedColor);
                } else {
                    vBox.setStyle("backgroundColor", null);
                }
            }
        ]]>
    </mx:Script>

    <mx:String id="lorem" source="lorem.txt" />

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="backgroundAlpha:">
                <mx:HSlider id="slider"
                        minimum="0.0"
                        maximum="1.0"
                        value="1.0"
                        liveDragging="true"
                        tickInterval="0.1" />
            </mx:FormItem>
            <mx:FormItem label="show backgroundImage:">
                <mx:CheckBox id="backImage"
                        selected="true"
                        change="backImage_change(event);" />
            </mx:FormItem>
            <mx:FormItem label="show backgroundColor:">
                <mx:CheckBox id="backColor"
                        selected="true"
                        change="backColor_change(event);" />
            </mx:FormItem>
            <mx:FormItem label="backgroundColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="white"
                        enabled="{backColor.selected}"
                        change="backColor_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:VBox styleName="containerVBox">
        <mx:VBox id="vBox"
                backgroundImage="{flexLogo}"
                backgroundAlpha="{slider.value}"
                backgroundColor="white"
                backgroundAttachment="fixed"
                width="500"
                height="250">
            <mx:Text width="100%" text="{lorem}" />
        </mx:VBox>
    </mx:VBox>

</mx:Application>

View source is enabled in the following example.

{ 1 comment… read it below or add one }

1 FlexFlip May 8, 2008 at 1:27 am

Peter, how would you do it the other way around: keeping a containers background alpha 100% but add a transparent border to it?
Strangely there is a container.backgroundAlpha property, but no container.borderAlpha.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: