Android Boot Camp for Developers Using Java 3
Android Boot Camp for Developers Using Java, 3 E Chapter 10: Move! Creating Animation Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 1
Objectives In this chapter, you learn to: • Create an Android application with Frame and Tween animation • Understand Frame animation • Understand Tween animation • Add an animation-list XML file • Code the Animation. Drawable object • Set the background Drawable resource • Launch the start( ) and stop( ) methods Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 2
Objectives (continued) • Add Tween animation to the application • Create a Tween XML file that rotates an image • Determine the rotation pivot, duration, and repeat count of a Tween animation • Load the start. Activity Tween animation in a second Activity • Change the orientation of the emulator Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 3
Creating Animation • Animation is everywhere – Words with Friends – Angry Birds – Cut the Rope – Candy Crush • A motion tween is used to animate the object – Specifies start state – Transition type – Number of times to animate Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 4
Creating Animation (continued) Steps to complete the App: 1. Add the four images to the drawable folder. 2. Add a Frame animation XML file to the project. 3. Add the layout for the image and button objects in main. xml. 4. Set the duration between frames in the frame-by-frame animation. 5. Declare and instantiate the Image. View, Button, and Animation. Drawable controls. 6. Code the On. Click. Listeners for the Button controls. 7. Run the Frame animation application. 8. Add a Tween animation XML file to rotate the last surfing image. 9. Create a second Activity named Tween. java to launch the rotation Tween animation. 10. When the application executes, change the orientation of the emulator. Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 5
Android Animation • Two types of animation for Android: – Frame animation • A sequence of photos playing as a slide show • Similar to cartoon animation • Images rapidly replaced by new, similar images – Tween animation • Created by a series of transformations on a single image • Transformations include position, size, shape rotation, color, and transparency Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 6
Adding the Layout for the Frame Image and Button Controls Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 7
Adding the Layout for the Frame Image and Button Controls (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 8
Adding the Layout for the Frame Image and Button Controls (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 9
Adding the Layout for the Frame Image and Button Controls (continued) Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 10
Creating Frame-by-frame Animation • Animation-list root element is needed to reference images stored in the drawable folders – Set the android: oneshot property to false for the animation plays repeatedly Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 11
Creating Frame-by-frame Animation Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 12
Creating Frame-by-frame Animation Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 13
Coding the Animation. Drawable Object • The Animation. Drawable class – provides the methods for drawable animations to create a sequence of frame-by-frame images – In Android development, frame-based animations and image transitions are defined as drawables – The instance of Animation. Drawable is instantiated as a class variable because it is used in multiple methods within the Main. Activity class Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 14
Setting the Background Resource – A 9 -patch image • Contains pre-defined “stretching” areas that maintain the same look on different screen sizes • Named for the nine areas, called patches, that scale separately – set. Background. Resource method places images in the frame-by-frame slide type display • Each frame points to one of the images that were assembled in the XML resource file • The img. Frame instance is the image that you want to animate and is set to the animation drawable as its background Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 15
Setting the Background Resource Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. (continued) 16
Setting the Background Resource Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. (continued) 17
Setting the Background Resource Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. (continued) 18
Adding Two Button Controls Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 19
Adding Two Button Controls Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 20
Adding Two Button Controls Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 21
Adding Two Button Controls Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 22
Using the Start and Stop Methods Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 23
Using the Start and Stop Methods Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. (continued) 24
Creating Tween Animation Tween effects are transitions that change objects from one state to another Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 25
Creating a Second Activity and XML Layout to Launch the Tween Animation Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 26
Creating a Second Activity and XML Layout to Launch the Tween Animation Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 27
Adding the Layout for the Tween Image Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 28
Coding a Tween Rotation XML File Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 29
Coding a Start. Animation • The Start. Animation method begins animating a View object by calling the Animation. Utils class utilities to access the resources necessary to load the animation Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 30
Coding a Start. Animation Android Boot Camp for Developers Using Java, 3 rd Ed. (continued) © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 31
Changing the Emulator to Landscape Orientation • Most Android devices automatically rotate the display from portrait to landscape when the user turns the device • The default screen orientation layout is vertical • Pressing the Fn+left Ctrl+F 12 keys simultaneously rotates the phone emulator to landscape orientation • You can also press the 7 key on the keypad when Num Lock is turned off Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 32
Running and Testing the Application • Tap or click the Run ‘app’ button on the Standard toolbar and display the app in the Nexus 5 emulator • Tap or click the START FRAME ANIMATION button to begin the Frame animation of the four Northern Lights images, as shown in Figure 10 -1 • The animation should rotate six times and then end Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 33
Summary • Frame animation assigns a sequence of images to play as in a slide show with a specified interval between images • To create a Frame animation, you write code in an XML file to load a sequence of images from the drawable folder • In the animation-list code, you can include the oneshot property to determine how many times to play the animation Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 34
Summary (continued) • When you add the XML file with the animation-list code to the Android project, select Drawable as the resource type and select animation-list as the root element so that Android stores the XML file in the res/drawable folder • The Animation. Drawable class provides the methods for drawable animations to create a sequence of frame-by-frame images • You can set the Background property of an image to any full Drawable resource such as a. png file Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 35
Summary (continued) • In Main. Activity. java, also include an instance of Animation. Drawable and assign it as the background of the animation images • You can use the start( ) and stop( ) methods of the drawable objects to control a Frame animation • A Tween animation manipulates a Drawable image by adding tween effects, which are predefined transitions that change an object from one state to another Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 36
Summary (continued) • The XML file for a Tween animation defines rotate attributes such as the number of degrees to spin, the pivot location, the rotation duration, and the number of times to repeat the rotation • To launch a Tween animation, use the start. Animation method, which begins animating a View object by calling the Animation. Utils class utilities to access the resources it needs to play the animation Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 37
Summary (continued) • To switch the emulator to use a landscape orientation on a PC, press the Fn+left Ctrl+F 12 keys. To rotate the emulator to the original portrait position, press the Fn+left Ctrl+F 12 keys again. Mac users can press the Fn+Ctrl+F 12 keys to change the orientation Android Boot Camp for Developers Using Java, 3 rd Ed. © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. 38
- Slides: 38