Android 3.0 Animations Beginner's Guide Bring your Android applicaons to life with stunning animaons Alex Shaw BIRMINGHAM - MUMBAI D ownload from Wow! eBook <www.woweb o o k . c o m > Android 3.0 Animations Beginner's Guide Copyright © 2011 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmied in any form or by any means, without the prior wrien permission of the publisher, except in the case of brief quotaons embedded in crical arcles or reviews. Every eort has been made in the preparaon of this book to ensure the accuracy of the informaon presented. However, the informaon contained in this book is sold without warranty, either express or implied. Neither the author , nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark informaon about all of the companies and products menoned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this informaon. First published: October 2011 Producon Reference: 1211011 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84951-528-3 www.packtpub.com Cover Image by Vinayak Chiar ( vinayak.chittar@gmail.com ) Credits Author Alex Shaw Reviewers Nathan Schwermann Roger Belk Acquision Editor Tarun Singh Development Editors Pallavi Iyengar Meeta Rajani Technical Editors Lubna Shaikh Ankita Shashi Copy Editor Leonard D'Silva Project Coordinator Shubhanjan Chaerjee Proofreaders Stephen Silk Samantha Lyon Indexer Monica Ajmera Graphics Geetanjali Sawant Producon Coordinator Melwyn D'sa Cover Work Melwyn D'sa About the Author Alex Shaw has been an Android fan boy since Android 1.5 arrived, and he began developing soware for it almost immediately. He has presented at DroidCon in Berlin and London, and wrien applicaons for business, academia, and pleasure. An alumnus of The University of Edinburgh, he has kept close business and social es with the Scosh geek scene. His consulng company, Glastonbridge Soware Limited, provides development resources to the Edinburgh soware industry. In his spare me, he writes generave music applicaons and talks a lot of nonsense. Hearty thanks to my partner, Amy Worthington, for pung up with my constant stream of ideas whenever wring was on my mind. Thanks also to my Mum and to my close friends, who have supported me when stress and anxiety were taking their toll. Thanks also to the team at Packt, who put up with my errac e-mail discipline and occasional late submissions, with paence and kindness. This book has been an adventure and an experience to remember. About the Reviewers Nathan Schwermann is a husband and proud father. He aends the University of Kansas to study Computer Science. In the past years, Nathan has worked as a freelance Android developer, making many great applicaons to help pay his high tuion costs. Nathan aspires to work for an independent gaming studio. Roger Belk , also known as Big Daddy App, has developed Android applicaons for the last year. He is a self taught 43-year-old Ironworker. He builds applicaons using Eclipse with Android SDK, Java, and Google's App Inventor. You can check out his website at www.BigDaddyApp.com Roger is also a power user in Google's App Inventor forums, answering help requests from new AI developers, from the setup, to the How-To, and to the coee shop just chang and kicking around ideals for AI apps. Books that he has worked on include Animaon 3.0 and Google App Inventor. www.PacktPub.com Support les, eBooks, discount offers and more You might want to visit www.PacktPub.com for support les and downloads related to your book. Did you know that Packt oers eBook versions of every book published, with PDF and ePub les available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com , you can also read a collecon of free technical arcles, sign up for a range of free newsleers and receive exclusive discounts and oers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant soluons to your IT quesons? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's enre library of books. Why Subscribe? Fully searchable across every book published by Packt Copy and paste, print and bookmark content On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com , you can use this to access PacktLib today and view nine enrely free books. Simply use your login credenals for immediate access. Table of Contents Preface 1 Chapter 1: Animaon Techniques on Android 7 An animated applicaon: counng calculator 8 Time for acon – learning to count with the counng calculator 8 Frame animaon 10 Time for acon – playing with the frames 11 Fancy frame animaons 13 Simple fades using transion animaons 14 Tweening 14 Time for acon – nding tweens 15 The tween jazz band 15 Interpolaons—meet the drummer 16 Animaon sets—meet the conductor 16 Tweening elements in XML 16 What are tweens good at? 17 Animators – new in Android 3.0! 17 Beyond views: high-speed animang in 2 dimensions 19 Drawing loops 19 Doing your own housekeeping is hard 20 Where to use surfaces 21 What do views do anyway? 21 Time for acon – let's draw views 21 Animang awesome 3D 27 Want to go faster? 28 Making a beer applicaon 30 Always be helpful 30 Small and powered by baeries 30 Summary 32 Table of Contents [ ii ] Chapter 2: Frame Animaons 33 Making a frame animaon 34 Time for acon – the funky sck man 34 The anatomy of a frame animaon 38 XML elements 38 <animaon-list> 39 <item> 39 Timing 40 Images and Drawables 40 Screen size 41 Somemes you run out of memory 41 Making frame animaons in Java 43 Time for acon – making the sck man interacve 43 Controlling frame animaons 48 start() and stop() 48 AnimaonDrawable.setVisible(true,true) 48 Creang new animaons 48 Time for acon – programmacally dened animaon 48 More neat methods on AnimaonDrawable 52 Working properly in the GUI thread 53 Animang a transion between frames 55 Time for acon – make the transion 55 Wring XML for a transionDrawable 59 <transion> 59 <item> 59 Working with other useful methods 60 startTransion(int duraon) 60 reverseTransion(int duraon) 60 resetTransion() 60 Summary 62 Chapter 3: Tweening and Using Animators 63 Greeng the tween 63 Time for acon – making a tower of Hanoi puzzle 64 Dening starts and ends 67 Assembling the building blocks of a tween 68 Time for acon – composing a tween animaon 68 Taking a look at the dierent types of tween animaon 74 <translate> 74 <rotate> 74 <alpha> 75 <scale> 75 Common aributes 76 Declaring tweens in the correct order 76 Table of Contents [ iii ] Making tweens that last for ever 77 Time for acon – creang an everlasng tween 77 Animang layouts 81 Time for acon – laying out blocks 81 Receiving animaon events 83 Time for acon – receiving animaon events 84 Interpolang animaons 86 Time for acon – changing the rhythm with interpolators 86 Using the interpolators provided by Android 88 Linear interpolator 88 Accelerate interpolator 88 Decelerate interpolator 88 Accelerate-decelerate interpolator 88 Bounce interpolator 89 Ancipate interpolator 89 Overshoot interpolator 89 Ancipate overshoot interpolator 89 Cycle interpolator 89 Sharing interpolators 89 android:sharedInterpolator="true" 90 android:sharedInterpolator="false" 90 Creang and parameterizing interpolators 90 Finding out more 91 Summary 92 Chapter 4: Animang Properes and Tweening Pages 93 Note for developers using versions of Android before 3.0 94 Turning pages with a ViewFlipper 94 Time for acon – making an interacve book 94 Creang tween animaons in Java 103 Time for acon – creang a tween in Java 104 Wring the SlideAndScale animaon in Java 107 Wring the SlideAndScale animaon In XML 107 Animang with ObjectAnimator 108 Time for acon – animang the rolling ball 109 Construcng ObjectAnimators 111 Breaking down the construcon of ballRoller 111 Geng and seng with ObjectAnimators 112 Animang values with ValueAnimator 113 Time for acon – making a ball bounce 113 Updang the frame rate 117 Changing the interpolator 117 Time for acon – improving our bouncing ball 117 Comparing animators and tweens 119 . Animang awesome 3D 27 Want to go faster? 28 Making a beer applicaon 30 Always be helpful 30 Small and powered by baeries 30 Summary 32 Table of Contents. 206 setStyle 206 setTextAlign 207 setTextScaleX 207 setTextSize 207 setTypeface 207 Frame scheduling 207 Time for acon – creang smooth game loops 207