Creating a simple image gallery with the Flex TileList control

by Peter deHaan on March 8, 2008

in Image, PopUpManager, TileList

Similar to a previous example, “Creating a simple image gallery with the Flex HorizontalList control”, the following example shows how you can create a simple photo gallery in Flex using the TileList control, Image control, and the PopUpManager class.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/08/creating-a-simple-image-gallery-with-the-flex-tilelist-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        global {
            modal-transparency: 0.9;
            modal-transparency-color: white;
            modal-transparency-blur: 9;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            import mx.effects.Resize;
            import mx.events.ResizeEvent;
            import mx.events.ListEvent;
            import mx.controls.Image;
            import mx.events.ItemClickEvent;
            import mx.managers.PopUpManager;

            private var img:Image;

            private function tileList_itemClick(evt:ListEvent):void {
                img = new Image();
                // img.width = 300;
                // img.height = 300;
                img.maintainAspectRatio = true;
                img.addEventListener(Event.COMPLETE, image_complete);
                img.addEventListener(ResizeEvent.RESIZE, image_resize);
                img.addEventListener(MouseEvent.CLICK, image_click);
                img.source = evt.itemRenderer.data.@fullImage;
                img.setStyle("addedEffect", image_addedEffect);
                img.setStyle("removedEffect", image_removedEffect);
                PopUpManager.addPopUp(img, this, true);
            }

            private function image_click(evt:MouseEvent):void {
                PopUpManager.removePopUp(evt.currentTarget as Image);
            }

            private function image_resize(evt:ResizeEvent):void {
                PopUpManager.centerPopUp(evt.currentTarget as Image);
            }

            private function image_complete(evt:Event):void {
                PopUpManager.centerPopUp(evt.currentTarget as Image);
            }
        ]]>
    </mx:Script>

    <mx:WipeDown id="image_addedEffect" startDelay="100" />

    <mx:Parallel id="image_removedEffect">
        <mx:Zoom />
        <mx:Fade />
    </mx:Parallel>

    <mx:XML id="xml" source="gallery.xml" />
    <mx:XMLListCollection id="xmlListColl" source="{xml.image}" />

    <mx:TileList id="tileList"
            dataProvider="{xmlListColl}"
            itemRenderer="CustomItemRenderer"
            columnCount="4"
            columnWidth="125"
            rowCount="2"
            rowHeight="100"
            themeColor="haloSilver"
            verticalScrollPolicy="on"
            itemClick="tileList_itemClick(event);" />

</mx:Application>

View CustomItemRenderer.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/08/creating-a-simple-image-gallery-with-the-flex-tilelist-control/ -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
        horizontalAlign="center"
        verticalAlign="middle">

    <mx:Image source="{data.@thumbnailImage}" />

    <mx:Label text="{data.@title}" />

</mx:VBox>

View gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/08/creating-a-simple-image-gallery-with-the-flex-tilelist-control/ -->
<gallery>
    <image title="Flex"
        thumbnailImage="assets/fx_appicon-tn.gif"
        fullImage="assets/fx_appicon.jpg" />
    <image title="Flash"
            thumbnailImage="assets/fl_appicon-tn.gif"
            fullImage="assets/fl_appicon.jpg" />
    <image title="Illustrator"
            thumbnailImage="assets/ai_appicon-tn.gif"
            fullImage="assets/ai_appicon.jpg" />
    <image title="Dreamweaver"
            thumbnailImage="assets/dw_appicon-tn.gif"
            fullImage="assets/dw_appicon.jpg" />
    <image title="ColdFusion"
            thumbnailImage="assets/cf_appicon-tn.gif"
            fullImage="assets/cf_appicon.jpg" />
    <image title="Flash Player"
            thumbnailImage="assets/fl_player_appicon-tn.gif"
            fullImage="assets/fl_player_appicon.jpg" />
    <image title="Fireworks"
            thumbnailImage="assets/fw_appicon-tn.gif"
            fullImage="assets/fw_appicon.jpg" />
    <image title="Lightroom"
            thumbnailImage="assets/lr_appicon-tn.gif"
            fullImage="assets/lr_appicon.jpg" />
    <image title="Photoshop"
            thumbnailImage="assets/ps_appicon-tn.gif"
            fullImage="assets/ps_appicon.jpg" />
</gallery>

View source is enabled in the following example.

{ 77 comments… read them below or add one }

1 peterd October 15, 2008 at 7:32 am

deenalex,

You can use the navigateToURL() function to open new web pages:

<mx:Script>
    <![CDATA[
        import flash.net.navigateToURL;

        private function gotoGoogle():void {
            navigateToURL(new URLRequest("http://www.google.com/"));
        }
    ]]>
</mx:Script>

<mx:LinkButton label="Google.com" click="gotoGoogle();" />

Peter

Reply

2 deenalex October 16, 2008 at 5:04 am

Thanks peter,

Reply

3 deenalex October 28, 2008 at 2:27 am

Hi peter,

I got enrolled in project which mainly uses ILog component, i know some thing about ilog. Can you help me out to get some good examples and how to get similar with ilog.

Thanks in advance.
Deenalex.

Reply

4 Herman November 5, 2008 at 3:25 am

Hello PeterD,

I saw your example with the TitleWindow and displays an Image control inside it. Is it also possible to add a next and a previous button in a TitleWindow, so when it’s clicked, the TitleWindow shows the next and the previous large image in the xml-file/tilelist?

Kind regards,
Herman

Reply

5 ashok November 5, 2008 at 11:44 pm

i used this code for image gallery it is working good with the relative adjustments which are to be made mandatorily.will you please let me know the procedure for giving the zooming effect for the image individually.

Reply

6 deenalex November 10, 2008 at 2:20 am

Hi peter,

I have task to show bottom faced tab navigation, i mean opposite view of normal tab navigation. It depends on style of tab or i have to change codes in normal Tab navigation itself….

Thanks in Advance….
deenalex

Reply

7 michelle November 11, 2008 at 10:33 pm

Thanks for the codes! It worked fine

I just have a question to ask,
Does anyone know how to make the scroll bar transparent?

Reply

8 Rina December 18, 2008 at 9:26 pm

Hi, anyone have an idea of how to display images from mysql database instead of xml files? i am using php codes to link between flex and the database. i have insert http service into my main mxml.. and i think i need to add sometimg to my customItemRenderer. hope someone here will be able to help..

Thanks in Advance…
Rina

Reply

9 Jez December 21, 2008 at 7:40 pm

Hey,

very cool piece of code you got there. I am currently putting together a Flex Image gallery just like it, however I want to streamline everything to give the user a really good experience in terms of speed. I am honestly quite new to Flex Framework itself but know flash pretty well. Just wondered why this file is so big!? I mean I have exported it as a release but the SWF itself is like 268KB. Even the main.swf you put up here is the same so I was just wondering in terms of optimization is there something I am missing? I’d really appreciate it if someone can point me in the right direction.

Thanks for your response
J

Reply

10 Chandra Kumar February 27, 2009 at 3:33 am

I have a problem with the “image_removedEffect”. When the image is clicked, it just disappears, without playing any effect.

Reply

11 Chandra Kumar February 28, 2009 at 1:12 am

Sorry, I did a mistake. The code works fine.

Reply

12 Chandra Kumar February 28, 2009 at 1:54 am

Peter,
I do not understand why it happens like this – when I click my mouse in the TileList container, in the empty space adjacent to the image titled “Photoshop”, that image gets selected. Is it not possible to make that image get selected, only when the mouse is clicked on it?

Reply

13 ryosaeb4 April 27, 2009 at 11:43 pm

I’ve try to download the zip Flex project file.
Why flex ask me a libs folder?

Severity and Description Path Resource Location Creation Time Id
unable to open ‘/Users/walterfantauzzi/Documents/Flex Builder 3/TileList_itemClick_test/libs’ TileList_itemClick_test Unknown 1240900800790 3208

How I can use this code to learn how make a gallery XML in flex?

thanks

Reply

14 abhishek saxena April 28, 2009 at 12:22 am

thanks it really helped me… clear understanding of code..good work..

Reply

15 Peter deHaan April 28, 2009 at 8:22 am

ryosaeb4,

Yeah, you may need to create an empty /libs/ folder in your Flex project. I think I may have forgotten to publish that empty folder when exporting some of these projects.

Peter

Reply

16 brain June 6, 2009 at 12:20 pm

instead of jpgs, i load swfs into the gallery when i click on the thumbnail. i have been racking my brain trying to figure out how to add the close button to the swf that is loaded. what code do i type into flash to make that happen?

Reply

17 luko28 July 20, 2009 at 6:37 am

Hi, first – your gallery is great! I little tune up code for my gallery and it look beautiful.
But, i’ve read all comments, especially from Herman, because i also want to add prev/next buttons to popup.
As far, no result… Can you give me any idea how to do that?

Reply

18 Bart August 7, 2009 at 10:11 am

Is there anyway to prevent the image redraw when you scroll? It has a serious lag on it that I’ve noticed with this type of list component. It would be nice if you could cache the image somehow so the flex app doesn’t have to redraw it when you scroll the list

Reply

19 Peter deHaan August 7, 2009 at 10:39 am
20 abhishek September 17, 2009 at 12:03 am

how can we scroll images in tilelist slowly,
abhishekchess1@gmail.com

Reply

21 John October 30, 2009 at 4:07 am

This is great… thanks for the help!

Reply

22 Kevin December 4, 2009 at 1:24 pm

First, I want to say thanks for this wonderful example! It has helped me out a great deal. Next, to know if you could explain how to add text to the function that brings in the fullImage? For example, in the xml file if there is a node called “description” how can data be brought into the item renderer via data.@description ?

Reply

23 Martin January 21, 2010 at 10:47 am

thanks peter, for this nice gallery.
it really helped me a lot.
but, is there any possibility to change the itemClick event to a keyDown ?
when i use keyDown instead of itemClick there is a problem with the parameter throwed because the itemClick event throws a ListEvent,
but the keyDown does not. i mean of course because its only keyDown and not itemKeyDown.. but i still dont know how to fix this.

any idea?
can someone other help me?

thanks alot so far (:
martin

Reply

24 Peter deHaan January 21, 2010 at 10:52 am

@Martin,

Instead of this:

private function tileList_itemClick(evt:ListEvent):void {

Try this:

private function tileList_itemClick(evt:Event):void {

Not sure if that will work completely since the code tries to access img.source = evt.itemRenderer.data.@fullImage; in that event handler, so you may need to tweak it slightly to get the full image source.

Peter

Reply

25 Martin January 21, 2010 at 11:04 am

well, with only changing the ListEvent to Event it wont work because a normal Event type has no .itemRenderer method. am I right?
I really have no idea how to get the selected item without reading it out through event.itemRenderer.data

26 Peter deHaan January 21, 2010 at 11:25 am

@Martin,

Sorry, I don’t have time to verify right now, but maybe try changing this:

img.source = evt.itemRenderer.data.@fullImage;

To this:

img.source = tileList.selectedItem.@fullImage;

Peter

27 Martin January 22, 2010 at 3:02 pm

oh my god, yes of course..
it works.
thanks peter !

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: