Creating a simple image gallery using the Spark PopUpAnchor control in Flex 4

The following example shows how you can create a simple image gallery using the Spark PopUpAnchor control in Flex 4 using the PopUpAnchor, BitmapImage, and MX Image controls.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/07/creating-a-simple-image-gallery-using-the-spark-popupanchor-control-in-flex-4/ -->
<s:Application name="Spark_PopUpAnchor_gallery_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
 
    <s:HGroup horizontalCenter="0" top="20">
        <!-- 1 -->
        <s:Group mouseDown="fullImg1.displayPopUp = true;">
            <!-- embedded thumbnail image -->
            <s:BitmapImage id="thumbImg1"
                    source="@Embed('assets/fx_appicon-tn.gif')" />
            <s:PopUpAnchor id="fullImg1"
                    popUpHeightMatchesAnchorHeight="false"
                    width="100%" height="100%">
                <s:popUp>
                    <!-- dynamically loaded full image -->
                    <mx:Image source="assets/fx_appicon.jpg"
                            width="300" height="300"
                            mouseUp="fullImg1.displayPopUp = false;" />
                </s:popUp>
            </s:PopUpAnchor>
        </s:Group>
 
        <!-- 2 -->
        <s:Group mouseDown="fullImg2.displayPopUp = true;">
            <s:BitmapImage id="thumbImg2"
                    source="@Embed('assets/ai_appicon-tn.gif')" />
            <s:PopUpAnchor id="fullImg2"
                    popUpHeightMatchesAnchorHeight="false"
                    width="100%" height="100%">
                <s:popUp>
                    <mx:Image source="assets/ai_appicon.jpg"
                            width="300" height="300"
                            mouseUp="fullImg2.displayPopUp = false;" />
                </s:popUp>
            </s:PopUpAnchor>
        </s:Group>
 
        <!-- 3 -->
        <s:Group mouseDown="fullImg3.displayPopUp = true;">
            <s:BitmapImage id="thumbImg3"
                           source="@Embed('assets/fl_player_appicon-tn.gif')" />
            <s:PopUpAnchor id="fullImg3"
                           popUpHeightMatchesAnchorHeight="false"
                           width="100%" height="100%">
                <s:popUp>
                    <mx:Image source="assets/fl_player_appicon.jpg"
                              width="300" height="300"
                              mouseUp="fullImg3.displayPopUp = false;" />
                </s:popUp>
            </s:PopUpAnchor>
        </s:Group>
    </s:HGroup>
 
</s:Application>

View source is enabled in the following example.

Or you could convert the PopUpAnchor code into a custom component, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/07/creating-a-simple-image-gallery-using-the-spark-popupanchor-control-in-flex-4/ -->
<s:Application name="Spark_PopUpAnchor_gallery_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        xmlns:comps="comps.*">
 
    <s:HGroup horizontalCenter="0" top="20">
        <!-- 1 -->
        <comps:PopUpThumb id="thumb1"
                thumbnailImgClass="@Embed('assets/fx_appicon-tn.gif')"
                fullImgSrc="assets/fx_appicon.jpg" />
 
        <!-- 2 -->
        <comps:PopUpThumb id="thumb2"
                thumbnailImgClass="@Embed('assets/ai_appicon-tn.gif')"
                fullImgSrc="assets/ai_appicon.jpg" />
 
        <!-- 3 -->
        <comps:PopUpThumb id="thumb3"
                thumbnailImgClass="@Embed('assets/fl_player_appicon-tn.gif')"
                fullImgSrc="assets/fl_player_appicon.jpg" />
    </s:HGroup>
 
</s:Application>

And the custom PopUpThumb component, comps/PopUpThumb.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/02/07/creating-a-simple-image-gallery-using-the-spark-popupanchor-control-in-flex-4/ -->
<s:Group name="PopUpThumb"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        mouseDown="fullImg.displayPopUp = true;">
 
    <fx:Script>
        <![CDATA[
            [Bindable]
            public var thumbnailImgClass:Class;
 
            [Bindable]
            public var fullImgSrc:String;
        ]]>
    </fx:Script>
 
    <!-- embedded thumbnail image -->
    <s:BitmapImage id="thumbImg"
                   source="{thumbnailImgClass}" />
    <s:PopUpAnchor id="fullImg"
                   popUpHeightMatchesAnchorHeight="false"
                   width="100%" height="100%">
        <s:popUp>
            <!-- dynamically loaded full image -->
            <mx:Image source="{fullImgSrc}"
                      width="300" height="300"
                      mouseUp="fullImg.displayPopUp = false;" />
        </s:popUp>
    </s:PopUpAnchor>
 
</s:Group>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.