Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mobile Computing With Android ACST 4550 Android Animation

Similar presentations


Presentation on theme: "Mobile Computing With Android ACST 4550 Android Animation"— Presentation transcript:

1 Mobile Computing With Android ACST 4550 Android Animation

2 Animation To make this work, you must repeatedly restore the background and all other drawings done on the canvas when redrawing the image. This means you cannot put the multiple drawing commands with changed coordinates inside a single onDraw() call. You need to modify the coordinates in a different method and then invoke the onDraw() method making the call: invalidate(); You also need a pause between each drawing to make this work, because if you do not pause between each step the motion would go to fast for the eye to see and the steps would appear to happen simultaneously.

3 Pausing During Animation
A loop can execute thousands of instructions within a second, so when you use a loop in an animation, the pictures may be drawn too quickly. So you need to pause between frames, but how long should you pause? Our eyes can see between 25 to 40 frames per second (fps) – 40 fps is about 1 frame every 25 milliseconds (ms) and 25 fps is about 1 frame ever 40 ms– so you need to pause for at least 40 ms or less to create the illusion of smooth motion

4 Timers There are many ways to make a task run at a certain interval in Android, and we will cover some of the ways that are native to the Android OS in the future. However, using the Timer class from the standard java.util library is a very reliable approach. The only issue that is tricky with this approach is that you cannot access the Android UI thread directly from the thread the Timer triggers. Instead the Timer thread must call a separate method to post a new thread which will run on the Android UI thread. Although this may sound convoluted, it is a simple pattern to follow as shown in the example that follows.

5 java.util.Timer Example
Setting up the timer for INTERVAL milliseconds: // Constructor header code goes up here myTimer = new Timer(); myTimer.schedule(new TimerTask() { @Override public void run() { TimerMethod(); } }, 0, INTERVAL); // End the Constructor down here private void TimerMethod() { //Same thread as the timer, so need to call this.runOnUiThread() method an // Activity thread, or call this.post() if you are in a View instead this.runOnUiThread(Timer_Tick); private Runnable Timer_Tick = new Runnable() { public void run() { //This method runs in the Android UI thread. // Do your UI tasks here }; See SnowmanAnimatedView.java


Download ppt "Mobile Computing With Android ACST 4550 Android Animation"

Similar presentations


Ads by Google