Mobile Computing With Android ACST 4550 Android Animation

Slides:



Advertisements
Similar presentations
CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Advertisements

Chapter 6 Jam! Implementing Audio in Android Apps.
Chapter 6: Jam! Implementing Audio in Android Apps.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Basic Animation. Animation 4 options – Animated.gif – Frame by Frame animation – Tweened animation This is our focus – OpenGL ES Graphics API for more.
Cosc 5/4730 A little on threads and Messages: Handler class.
CS5103 Software Engineering Lecture 08 Android Development II.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Chapter 10: Move! Creating Animation
6-2 2D Graphics CSNB544 Mobile Application Development Thanks to Utexas Austin.
Flag Quiz App 1 CS7030: Mobile App Development. assets Folder 2 CS7030: Mobile App Development  drawable folder – Image contents at the root level 
Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical.
DUE Hello World on the Android Platform.
Animation.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Android Boot Camp for Developers Using Java, 3E
Animations 1 Fall 2012 CS2302: Programming Principles.
ALAA M. ALSALEHI SOFTWARE ENGINEER AT IUG Multithreading in Android.
Mobile Programming Lecture 11 Animation and TraceView.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Basic Animation. Animation 4 options – Animated.gif – Frame by Frame animation – Tweened animation This is our focus – OpenGL ES Graphics API for more.
Slide 1 VB Graphics Controls & Timer Control. Slide 2 Default Controls.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Mobile Computing Lecture#12 Graphics & Animation.
Flag Quiz Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 10: Move! Creating Animation 1 Android.
By: Eliav Menachi.  Android custom 2D graphics library  OpenGL ES 1.0 for high performance 3D graphics.
Lecture 3: Animation & Graphics Topics: Animation, Graphics, Drawing Date: Feb 2, 2016.
Animations & Multimedia LESSON 9 #2.09 USING ANIMATION AND MULTIMEDIA.
Java Thread Programming
Mobile Applications (Android Programming)
Concurrency in Android
Android Application 2D Graphics cs.
Lecture 3: Animation & Graphics
Small talk with the UI thread
Process concept.
Lesson 6: Enhancing Presentations
CS499 – Mobile Application Development
Lecture 3: Animation & Graphics
Instructor: Mazhar Hussain
Lecture 8: Graphics By: Eliav Menachi.
Android Boot Camp for Developers Using Java, 3E
Android External Resources
Arrays: Checkboxes and Textareas
S.RENUKADEVI/AP/SCD/ANDROID - Notifications
Activities and Intents
CS499 – Mobile Application Development
Mobile Computing With Android ACST 4550 Android Resources
Android Layout Basics Topics
Android Programming Lecture 8
CS5103 Software Engineering
File Handling Programming Guides.
Topics Introduction to File Input and Output
CS371m - Mobile Computing Responsiveness.
Topics Introduction Hardware and Software How Computers Store Data
Mobile Computing With Android ACST 4550 Android Database Storage
Android Topics Asynchronous Callsbacks
Android Topics Threads and the MessageQueue
Android Topics Limited Resources and why we need to consider them.
Lecture 3: Animation & Graphics
Using screens and adding two numbers - addda.cbl
Android SDK & App Development
Threads, Handlers, and AsyncTasks
Lecture 8: Graphics By: Eliav Menachi.
Mobile Programming Dr. Mohsin Ali Memon.
Topics Introduction to File Input and Output
Preference Activity class
Android Threads Dimitar Ivanov Mobile Applications with Android
Mobile Computing With Android ACST 4550 Android Animation
Presentation transcript:

Mobile Computing With Android ACST 4550 Android Animation

Working with Animation Android supports several different kinds of animation. Two of the simplest varieties are: Frame-by-frame Frame-by-frame animation involves the display of a sequence of images in rapid succession using threads or handlers. Tweening Tweened animation involves applying standard graphical transformations such as rotations and fades on a single image. The Android SDK provides some helper utilities for loading and using animation resources. These are found in android.view.animation.AnimationUtils class.

Android Handlers and Threads Scheduling Handler tasks is accomplished in two ways: One approach uses the post(), postAtTime() and postDelayed() methods. The other approach uses the sendMessage(), sendEmptyMessage(), sendMessageAtTime(), and sendMessageDelayed() methods. The post versions allow you to enqueue Runnable objects to the message queue that will execute on the UI thread when they are dequeued. The sendMessage versions allow you to enqueue a Message object containing a Bundle of data that will be processed by the Handler's handleMessage(Message) method on the UI thread. This is the method we will be using.

Creating a Handler For SendMessage() Messages Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: // Do work associated with message 1 break; case 2: // Do work associated with message 2 default: // Do default work } super.handleMessage(msg);

Creating a Message for a Handler You can create a Message to send to a Handler that contains data using the Bundle class in the following way for the 1st case statement in this example: Message message = new Message(); Bundle bundle = new Bundle(); bundle.putString("Message", someDataString); message.setData(bundle); message.what = 1; handler.sendMessage(message); “Message” is a key, and someDataString is a value paired with it. You can add many such key/value pairs this way. If you only have a single object to send you can use the Message.obtain() method to send your object as follows: handler.sendMessage(Message.obtain(handler, 2, someDataString)); If you have no Message object to send because you just need to trigger a certain case statement, you can use sendEmptyMessage as follow: handler.sendEmptyMessage(3);

Receiving a Message in a Handler The following shows how you would retrieve the data sent from the previous slide for case 1 (that used a Bundle), case 2 (that used Message.obtain) and case 3 (that used sendEmptyMessage): Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: String msgStr = msg.getData().getString("Message"); // Do some work associated with the string break; case 2: String msgStr = (String) msg.obj; case 3: // No object was sent, so do some work without it } super.handleMessage(msg);

Putting a Message into the MessageQueue handler.sendMessage(Message) Sends a Message object to the handler’s handleMessage() method. handler.sendEmptyMessage(CaseNumber) Sends a null object to the handler triggering the given switch case number. handler.sendMessageDelayed(Message,4000) [or sendEmptyMessageDelayed()] Sends a Message object to the handler after 4 seconds (4000 millis) have passed. This is a simple way to implement a timer because you can have the matching case statement issue another sendMessageDelayed() call with the same time delay, which loops whatever action occurs at that case statement according to the delay time. handler.sendMessageAtTime(Message,nextTimeMillis) [or sendEmptyMessageAtTime()] Sends a Message object to the handler at nextTimeMillis, which is an absolute number of milliseconds since the start time. You can determine how many milliseconds have past since start time using SystemClock.uptimeMillis().

Using Handler Tasks for Animation Since Handler Tasks can access the UI thread, you can implement animations with Handlers instead of the Thread approach we used before. The following App shows how a Handler can make the Snowman’s arms wave and make the car drive around it: (They are in samplecode07.txt) See: SnowmanCarHandlerView.java See: TicTacToeGameView.java

Defining and Using Frame-by-Frame Animation Resources Frame-by-frame animation is often used when the content changes from frame to frame. This can be used for complex frame transitions—much like a kid’s flip-book. To define frame-by-frame resources, take the following steps: Save each frame graphic as an individual drawable resource. It may help to name your graphics sequentially, in the order in which they are displayed—for example, frame1.png, frame2.png, and so on. Define the animation set resource in an XML file within the res/drawable/ resource directory hierarchy. Load, start, and stop the animation programmatically.

Defining and Using Frame-by-Frame Animation Resources (Cont’d) <?xml version="1.0" encoding="utf-8" ?> <animation-list xmlns:android= "http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/splash1" android:duration="500" /> <item android:drawable="@drawable/splash2" <item android:drawable="@drawable/splash3" </animation-list> Here’s an example of a simple frame-by-frame animation resource file called res/drawable/juggle.xml that defines a simple three-frame animation that takes 1.5 seconds to complete a single loop.

Defining and Using Frame-by-Frame Animation Resources (Cont’d) Frame-by-frame animation set resources defined with <animation-list> are represented by the Drawable subclass AnimationDrawable. The following code retrieves an AnimationDrawable resource called juggle: AnimationDrawable jugglerAnimation = (AnimationDrawable)getResources(). getDrawable(R.drawable.juggle); After you have a valid AnimationDrawable (android.graphics.drawable.AnimationDrawable), you can assign it to a View control on the screen and start and stop animation.

Defining and Using Tweened Animation Resources Tweened animation features include: Scaling Fading Rotation Translation These actions can be applied simultaneously or sequentially and might use different interpolators. Tweened animation sequences are not tied to a specific graphics file, so you can write one sequence and then use it for a variety of different graphics. For example: You can make moon, star, and diamond graphics all pulse using a single scaling sequence, or you can make them spin using a rotate sequence.

Defining Tweened Animation Sequence Resources in XML Graphic animation sequences can be stored as specially formatted XML files in the res/anim/ directory and are compiled into the application binary at build time. Here’s an example of a simple animation resource file called res/anim/spin.xml that defines a simple rotate operation—rotating the target graphic counterclockwise four times in place, taking 10 seconds to complete: <?xml version="1.0" encoding="utf-8" ?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <rotate android:fromDegrees="0" android:toDegrees="-1440" android:pivotX="50%" android:pivotY="50%" android:duration="10000" /> </set>

Using Tweened Sequence Resources Programmatically If we go back to the earlier example of a BitmapDrawable, we can now include some animation simply by adding the following code to load the animation resource file spin.xml and set the animation in motion: ImageView flagImageView = (ImageView)findViewById(R.id.ImageView01); flagImageView.setImageResource(R.drawable.flag); ... Animation an = AnimationUtils.loadAnimation(this, R.anim.spin); flagImageView.startAnimation(an); Now you have your graphic spinning. Notice that we loaded the animation using the base class object Animation.

Using Tweened Sequence Resources Programmatically (Cont’d) You can also extract specific animation types using the subclasses that match: RotateAnimation ScaleAnimation TranslateAnimation AlphaAnimation These are found in the android.view.animation package. There are a number of different interpolators you can use with your tweened animation sequences.