Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is easy to do if you make to leap to using a <code>Canvas</code>; those support drawing off the screen without any trouble. However, it will be a more complicated to implement. You'd need to implement a custom <code>View</code> and write your own animation in code. Essentially this comes down to simple 2D graphics handling rather than Views using built-in XML animations. There may be a way to do it with XML, but I'm much more familiar with canvases. A very good place to see how this is handled in code is the Lunar Lander example game that comes with the SDK. </p> <p>Roughly the steps you'll need to follow are:</p> <ol> <li><p>Put a custom view in the XML file, using something like <code>&lt;your.package.AnimatingView&gt;</code>, setting its size to fill-parent.</p></li> <li><p>Then define a AnimatingView class, which <code>extends SurfaceView and implements SurfaceHolder.Callback</code>. (This gives you access to the drawing <code>Canvas</code> immediately rather than by using the <code>invalidate()</code> method. This is important because invalidate() only refreshes when the thread is idle, e.g. at the end of the loop. To implement your animation, you need to have it drawing immediately.)</p></li> <li><p>You can then implement a loop which draws your moving image across the screen. The loop needs to start by drawing the whole background (because the canvas doesn't automatically get erased) and then draw the image at its new position based on the time that has passed. For example, if you want your animation to take 1 second to do, then you know that if 200ms have passed, the view should only have moved 200/1000, or 1/5, of the way from its starting position to the final position.</p></li> </ol> <p>You can see some examples of what I mean in my other answers to animation questions: basic reply about the <a href="https://stackoverflow.com/questions/2438733/how-to-do-animations-in-android-chess-game">usefulness of using SurfaceView</a> and and <a href="https://stackoverflow.com/questions/2439301/animating-and-rotating-an-image-in-a-surface-view">example of the loop to use</a>. Note: the second question was about rotating, and hence some of the difficulties I talked about won't be relevant to you. Good luck!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload