Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

Displaying Images

In this exercise, you will display images of grocery products. To do this, you must use the Image control to load images dynamically. The Image control can load JPG, GIF, SWF, and PNG files at runtime. If you are developing an offline application that will not access the Internet, you can use the @Embed directive to include the Image control in the completed SWF file.

  1. Open the FlexGrocer.mxml file that you created in the previous lesson.

    If you didn’t complete the previous lesson, you can import the Lesson04/start files. Please refer to the appendix for complete instructions on importing a project should you skip a lesson or if you have a code issue you cannot resolve.

  2. Switch Flash Builder to Design view by clicking the Design View button.
  3. Be sure that the Components view is open. If it’s not, choose Window > Components.
  4. Select the Image control from the Controls folder and drag the control between the Milk and 1.99 Label controls you already added.

    When you drag the Image control from the Components view to the container, Flash Builder automatically adds the MXML to place the Image control on the screen and positions it where you drop it.

  5. Be sure that the Flex Properties view is open. If it’s not, choose Window > Properties.

    The Flex Properties view shows important attributes of the selected component—in this case, the Image control. You can see the Source property, which specifies the path to the Image file. The ID of the Image control references the instance created from the <s:Image> tag or Image class in ActionScript.

  6. Click the Source folder icon and navigate to the assets directory. Select the dairy_milk.jpg image and click Open.

    The image you selected is displayed in Design view. The source property is also added to the MXML tag.

  7. Click the Scale Mode drop-down menu and change the value to letterbox.

    In an ideal world, all the images that you use in the application would be a perfect size, but this is not always the case. Flex can scale the images in two ways. You can choose letterbox to keep the aspect ratio of the original images correct even as their size is adjusted, or you can choose stretch to distort the images to make them fit into any given width and height.

  8. Switch back to Source view and notice that Flash Builder has added an <s:Image> tag as well as the attributes you specified in the Flex Properties window.

    As you can see, it is easy to switch between Source view and Design view, and each one has its advantages. Notice as you switch back to Source view that the Image tag you were working on is now highlighted.

  9. In the <s:Image> tag that you added, insert an @Embed directive to the Image control.
    <s:Image includeIn="State1" scaleMode="letterbox" source="@Embed('assets/dairy_milk.jpg')"/>

    The @Embed directive causes the compiler to transcode and include the JPG in the SWF file at compile time. This technique has a couple advantages over the default of loading the image at runtime. First, the image is loaded at the start of the application, so the user doesn’t have to wait for the image to load before displaying when it is needed. Also, this technique can be useful if you are building offline applications that do not need to access the Internet because the appropriate images are included in the SWF file and will be correctly displayed when needed. Remember, though, that using this technique greatly increases the size of your SWF file.

  10. Save, compile, and run the application.

You should see that the Image and Label controls and button fit neatly into the layout container.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus