Pro Android Flash ■ ■ ■ Stephen Chin Dean Iverson Oswald Campesato Paul Trani i Pro Android Flash Copyright © 2011 by Stephen Chin, Dean Iverson, Oswald Campesato, and Paul Trani All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3231-5 ISBN-13 (electronic): 978-1-4302-3232-2 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Steve Anglin Development Editor: Tom Welsh Technical Reviewer: James Bucanek Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jennifer L. Blackwell Copy Editor: Mary Ann Fugate Compositor: MacPS, LLC Indexers: BIM Inde×ing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. To my loving wife and daughter, who supported me in completing this book, with full knowledge that they would be without a father for several months. Stephen Chin To my mother, who always made sure our house was full of books. Dean Iverson To my parents. Oswald Campesato To my father, who loves me regardless of how much I try to impress him. Paul Trani Contents at a Glance Contents .............................................................................................................. v About the Authors ............................................................................................... x About the Technical Reviewer ........................................................................... xi Acknowledgments ............................................................................................ xii Foreword ......................................................................................................... xiii Introduction ...................................................................................................... xiv ■Chapter 1: Introducing Mobile Flash ............................................................... 1(cid:1) ■Chapter (cid:18): Targeting Applications for the Mobile Profile .............................. 29(cid:1) ■Chapter (cid:19): Building Flash and Flex Applications for Android ........................ 71(cid:1) ■Chapter (cid:20): Graphics and Animation ............................................................. 121(cid:1) ■Chapter (cid:21): Application Deployment and Publication ................................... 165(cid:1) ■Chapter (cid:22): Adobe AIR and Native Android Apps .......................................... 199(cid:1) ■Chapter (cid:23): Taking Advantage of Hardware Inputs ....................................... 243(cid:1) ■Chapter (cid:24): Rich Media Integration ............................................................... 279(cid:1) ■Chapter (cid:25): The Designer-Developer Workflow ............................................. 339(cid:1) ■Chapter 1(cid:16): Performance Tuning .................................................................(cid:14)359(cid:1) ■Chapter 1(cid:17): Beyond Mobile: Tablets and TV .................................................399(cid:1) Index ............................................................................................................... 427 iv Contents Contents at a Glance .......................................................................................... iv(cid:1) About the Authors ............................................................................................... x(cid:1) About the Technical Reviewer ........................................................................... xi(cid:1) Acknowledgments ............................................................................................ xii Foreword ......................................................................................................... xiii Introduction ...................................................................................................... xiv ■Chapter 1: Introducing Mobile Flash ............................................................... 1(cid:1) Why Android? .......................................................................................................................................................... 1(cid:1) Flash on Other Platforms ........................................................................................................................................ 2(cid:1) Exploring Android .................................................................................................................................................... 3(cid:1) The Flash Platform .................................................................................................................................................. 5(cid:1) The Flash Runtime ............................................................................................................................................. 5(cid:1) Flash Tooling ...................................................................................................................................................... 7(cid:1) Running Applications from Flash Professional ................................................................................................ 10(cid:1) Running Applications from Flash Builder ......................................................................................................... 13(cid:1) Running Applications from the Command Line ................................................................................................ 26(cid:1) Summary .............................................................................................................................................................. 28(cid:1) ■Chapter 2: Targeting Applications for the Mobile Profile .............................. 29(cid:1) Screen Size ........................................................................................................................................................... 29(cid:1) Screen Resolution vs. Density ......................................................................................................................... 31(cid:1) Simulating Device-Independent Pixels in Flash ............................................................................................... 32(cid:1) Density in Flex Applications ............................................................................................................................. 33(cid:1) Density Explorer Application ............................................................................................................................ 34(cid:1) Density Support in CSS .................................................................................................................................... 40(cid:1) Screen Orientation ................................................................................................................................................ 43(cid:1) Portrait/Landscape Switching in Flex .............................................................................................................. 44(cid:1) Automatic Orientation Flipping in Flash ........................................................................................................... 48(cid:1) Rotating Smiley Flash Orientation Example ..................................................................................................... 50(cid:1) Multitouch and Gestures ....................................................................................................................................... 52(cid:1) Mobile Gestures ............................................................................................................................................... 54(cid:1) Flash Scrapbook Example ................................................................................................................................ 56(cid:1) v ■ CONTENTS Touch Point API ................................................................................................................................................ 64(cid:1) Summary .............................................................................................................................................................. 70(cid:1) ■Chapter 3: Building Flash and Flex Applications for Android ........................ 71(cid:1) Constructing Mobile UIs with Flex ........................................................................................................................ 71(cid:1) ViewNavigatorApplication ................................................................................................................................ 73(cid:1) Important Events in the Life of a View ............................................................................................................. 75(cid:1) TabbedViewNavigatorApplication .................................................................................................................... 77(cid:1) Just an Application .......................................................................................................................................... 82(cid:1) ViewNavigator and Views ................................................................................................................................ 84(cid:1) Passing Data Between Views .......................................................................................................................... 93(cid:1) Persisting View and Session Data ................................................................................................................... 94(cid:1) Visual Controls ...................................................................................................................................................... 96(cid:1) Text Controls .................................................................................................................................................... 99(cid:1) Soft Keyboard Support ................................................................................................................................... 104(cid:1) Button Controls .............................................................................................................................................. 107(cid:1) Flex Lists ........................................................................................................................................................ 112(cid:1) Slider, Scroller, and BusyIndicator Controls .................................................................................................. 116(cid:1) Summary ............................................................................................................................................................ 119(cid:1) ■Chapter 4: Graphics and Animation ............................................................. 121(cid:1) Using Spark Primitives for 2D Shapes ................................................................................................................ 121(cid:1) Drawing Rectangles and Ellipses ................................................................................................................... 122(cid:1) Using Linear and Radial Gradients ................................................................................................................. 123(cid:1) Rendering Cubic Bezier Curves ...................................................................................................................... 126(cid:1) Another Path Element Example ..................................................................................................................... 130(cid:1) Using Spark Filters ......................................................................................................................................... 131(cid:1) Applying Transformations to Geometric Shapes ................................................................................................. 133(cid:1) Creating Scaled Effects .................................................................................................................................. 134(cid:1) Creating Animation Effects in Spark ................................................................................................................... 136(cid:1) Using the Animate Element ............................................................................................................................ 137(cid:1) Animation: Parallel and Sequence ................................................................................................................. 139(cid:1) Creating 3D Effects ............................................................................................................................................. 142(cid:1) Creating Spark Skins .......................................................................................................................................... 146(cid:1) Generating 2D Charts and Graphs in Spark ........................................................................................................ 150(cid:1) Creating 2D Bar Charts .................................................................................................................................. 151(cid:1) Creating 2D Pie Charts ................................................................................................................................... 153(cid:1) Using FXG with Spark .................................................................................................................................... 156(cid:1) A Sketching Program ..................................................................................................................................... 158(cid:1) Summary ............................................................................................................................................................ 164(cid:1) ■Chapter 5: Application Deployment and Publication ................................... 165(cid:1) Setting Up an Android Emulator .......................................................................................................................... 165(cid:1) Installing the Android SDK ............................................................................................................................. 166(cid:1) Creating an Android Virtual Device ................................................................................................................ 168(cid:1) Installing AIR Within the Emulator ................................................................................................................. 171(cid:1) Emulator Key Bindings ................................................................................................................................... 172(cid:1) Deploying AIR Applications ................................................................................................................................. 175(cid:1) Setting Up ADT ............................................................................................................................................... 175(cid:1) Application Permissions ................................................................................................................................ 176(cid:1) vi ■ CONTENTS Icons and Resources ...................................................................................................................................... 179(cid:1) Code Signing Certificates ............................................................................................................................... 183(cid:1) Creating Certificates Using ADT ..................................................................................................................... 185(cid:1) Publishing from Flash Professional ............................................................................................................... 187(cid:1) Exporting Release Builds from Flash Builder ................................................................................................. 188(cid:1) Running Flex Applications in the Android Emulator ....................................................................................... 189(cid:1) Deploying from the Command Line ................................................................................................................ 191(cid:1) Publishing AIR Apps to Android Market .............................................................................................................. 194(cid:1) Step 1: Create an Android Market Developer Account ................................................................................... 194(cid:1) Step 2: Package Your Application .................................................................................................................. 194(cid:1) Step 3: Upload Your Adobe AIR Application ................................................................................................... 195(cid:1) Publishing AIR Apps to the Amazon Appstore ..................................................................................................... 196(cid:1) Summary ............................................................................................................................................................ 198(cid:1) ■Chapter 6: Adobe AIR and Native Android Apps .......................................... 199(cid:1) Invoking URI Handlers in Adobe AIR ................................................................................................................... 200(cid:1) Launching Custom HTML Pages in Adobe AIR .................................................................................................... 203(cid:1) Navigating to HTML Pages in Adobe AIR ............................................................................................................. 205(cid:1) Accessing SQLite in Adobe AIR ........................................................................................................................... 208(cid:1) Learning Basic Concepts in Android ................................................................................................................... 212(cid:1) Major Features of Android 3.0 ....................................................................................................................... 213(cid:1) Download/Installation of Android ................................................................................................................... 214(cid:1) Key Concepts in Android ................................................................................................................................ 214(cid:1) Creating Android Applications ............................................................................................................................ 221(cid:1) The Structure of an Android Application ........................................................................................................ 222(cid:1) The Main Files in an Android Application ....................................................................................................... 223(cid:1) Sending Notifications in Android Applications .................................................................................................... 227(cid:1) Adobe AIR and Native Android Integration .......................................................................................................... 239(cid:1) Summary ............................................................................................................................................................ 241(cid:1) ■Chapter 7: Taking Advantage of Hardware Inputs ....................................... 243(cid:1) Microphone ......................................................................................................................................................... 243(cid:1) Camera and CameraUI ........................................................................................................................................ 246(cid:1) Camera .......................................................................................................................................................... 247(cid:1) Manipulating the Camera’s Video Stream ..................................................................................................... 251(cid:1) CameraRoll .................................................................................................................................................... 260(cid:1) CameraUI ....................................................................................................................................................... 266(cid:1) Accelerometer ..................................................................................................................................................... 269(cid:1) The Accelerometer and AccelerometerEvent Classes ................................................................................... 270(cid:1) Geolocation ......................................................................................................................................................... 273(cid:1) Summary ............................................................................................................................................................ 276(cid:1) ■Chapter 8: Rich Media Integration ............................................................... 279(cid:1) Playing Sound Effects ......................................................................................................................................... 279(cid:1) The SoundEffect Class ................................................................................................................................... 280(cid:1) Embedded SoundEffect Example ................................................................................................................... 281(cid:1) Sophisticated Sound Solutions ........................................................................................................................... 283(cid:1) Playing Recorded Sound ..................................................................................................................................... 286(cid:1) Generating Sound Data on the Fly ................................................................................................................. 286(cid:1) Handling the State Transitions ....................................................................................................................... 289(cid:1) vii ■ CONTENTS A Flash Music Player ........................................................................................................................................... 294(cid:1) Playing MP3 Files ........................................................................................................................................... 295(cid:1) From Prototype to Application ....................................................................................................................... 299(cid:1) Playing Video ...................................................................................................................................................... 324(cid:1) Optimizing Video for Mobile Devices ............................................................................................................. 324(cid:1) Spark VideoPlayer .......................................................................................................................................... 325(cid:1) Video with NetStream .................................................................................................................................... 328(cid:1) Playing Video with OSMF ............................................................................................................................... 332(cid:1) VideoRecorder Example ................................................................................................................................. 335(cid:1) Summary ............................................................................................................................................................ 338(cid:1) ■Chapter 9: The Designer-Developer Workflow ............................................. 339(cid:1) The Visual Designer’s Role ................................................................................................................................. 340(cid:1) Starting in Adobe Device Central ................................................................................................................... 340(cid:1) Using Device Central ...................................................................................................................................... 340(cid:1) Adobe Photoshop ........................................................................................................................................... 346(cid:1) Graphic File Formats ...................................................................................................................................... 350(cid:1) Adobe Illustrator ............................................................................................................................................ 352(cid:1) Adobe Fireworks ............................................................................................................................................ 353(cid:1) The Developer’s Role .......................................................................................................................................... 355(cid:1) The Developer’s Toolbox ................................................................................................................................ 355(cid:1) Summary ............................................................................................................................................................ 358(cid:1) ■Chapter 1(cid:16): Performance Tuning ................................................................. 359(cid:1) Mobile Performance Tuning Basics .................................................................................................................... 360(cid:1) Perceived vs. Actual Performance ...................................................................................................................... 360(cid:1) Tuning Graphics Performance ............................................................................................................................ 362(cid:1) The Elastic Racetrack .................................................................................................................................... 363(cid:1) Reducing Code Execution Time ..................................................................................................................... 364(cid:1) Speeding Up Rendering ................................................................................................................................. 365(cid:1) Scene Bitmap Caching ................................................................................................................................... 368(cid:1) GPU Rendering ............................................................................................................................................... 376(cid:1) Performant Item Renderers ................................................................................................................................ 378(cid:1) Flex Image Classes ........................................................................................................................................ 382(cid:1) Text Component Performance ....................................................................................................................... 383(cid:1) Built-In Item Renderers .................................................................................................................................. 385(cid:1) Performance Monitoring APIs and Tools ............................................................................................................. 388(cid:1) Hi-ReS! Stats ................................................................................................................................................. 388(cid:1) PerformanceTest v2 Beta .............................................................................................................................. 389(cid:1) Flash Builder Profiler ..................................................................................................................................... 390(cid:1) The Future of Flash Performance ....................................................................................................................... 391(cid:1) Faster Garbage Collection .............................................................................................................................. 391(cid:1) Faster ActionScript Performance ................................................................................................................... 393(cid:1) Concurrency ................................................................................................................................................... 394(cid:1) Threaded Render Pipeline .............................................................................................................................. 395(cid:1) Stage3D ......................................................................................................................................................... 396(cid:1) Summary ............................................................................................................................................................ 398(cid:1) ■Chapter 11: Beyond Mobile: Tablets and TV ................................................ 399(cid:1) Scaling the Screen .............................................................................................................................................. 399(cid:1) viii ■ CONTENTS State-Based Customization ................................................................................................................................ 402(cid:1) Using State Groups ........................................................................................................................................ 403(cid:1) Project-Based Customization ......................................................................................................................... 410(cid:1) Implementing the Custom Tablet Interface .................................................................................................... 416(cid:1) Transitioning to TV .............................................................................................................................................. 420(cid:1) Porting to PlayBook ............................................................................................................................................. 421(cid:1) Investigating iOS ................................................................................................................................................. 425(cid:1) Summary ............................................................................................................................................................ 426(cid:1) Index ............................................................................................................... 427 ix