We usually see the most likely is frame animation, Android, of course, it is also indispensable. It's easier to use, just to create a
Animationdrawabledf object to represent the frame animation, and then through the Addframe method to add each frame to display the content, and finally through the Start method can be
To play this animation, you can also set whether or not to repeat it by using the Setoneshot method.
Activity01.java
PackageXiaohang.zhimeng;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;ImportAndroid.view.Window;ImportAndroid.view.WindowManager; Public classActivity01extendsActivity {PrivateGameview Mgameview; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set no title barrequestwindowfeature (Window.feature_no_title); //set to full screen modeGetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLA G_fullscreen); Mgameview=NewGameview ( This); Setcontentview (Mgameview); } Public BooleanOnKeyUp (intKeyCode, KeyEvent event) { if(Mgameview = =NULL) { return false; } mgameview.onkeyup (KeyCode, event); return true; } Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if(Mgameview = =NULL) { return false; } if(KeyCode = =keyevent.keycode_back) { //Close Activity This. Finish (); return true; } return Super. OnKeyDown (KeyCode, event); }}
Gameview.java
PackageXiaohang.zhimeng; ImportAndroid.content.Context; ImportAndroid.graphics.Canvas; Importandroid.graphics.drawable.AnimationDrawable; Importandroid.graphics.drawable.Drawable; Importandroid.view.KeyEvent; ImportAndroid.view.View; Public classGameviewextendsview{//Defining animationdrawable Animations PrivateAnimationdrawable frameanimation =NULL; Context Mcontext=NULL; //define a Drawable objectdrawable mbitanimation =NULL; PublicGameview (Context context) {Super(context); Mcontext=context; //instantiating a Animationdrawable objectFrameanimation =Newanimationdrawable (); /*Loading Resources*/ //here, a loop is used to load all resources with similar names.//such as "A1 ....." Picture of 15.png " for(inti = 1; I <= 15; i++){ intid = getresources (). Getidentifier ("a" + I, "drawable", Mcontext.getpackagename ()); //This method returns an object that can be drawn associated with a specific resource IDMbitanimation =getresources (). getdrawable (ID); /*Add a frame to an animation*/ //parameter mbitanimation is a picture of the frame//parameter 500 is the time at which the frame is displayed, calculated in millisecondsFrameanimation.addframe (Mbitanimation, 500); } /*The Getidentifier method of resources is used to return a unique identifier for a resource, and if it does not return 0 * 0 is not a valid identifier, say the meaning of the method several parameters * The first one is our capital The source name. * The second one is where we're going to find our resources our pictures under drawable so for drawable * Third we used the context of the Getpackagename return the package name of the application **/ //sets whether playback mode loops, False indicates loop, True indicates no loopFrameanimation.setoneshot (false); //set the animation that this class will display This. setbackgrounddrawable (frameanimation); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); } Public BooleanOnKeyUp (intKeyCode, KeyEvent event) { Switch(keycode) { Casekeyevent.keycode_dpad_up://start playing when pressing the top arrow of the phoneFrameanimation.start (); Break; } return true; } }
This article was reproduced from: http://blog.csdn.net/fuuckwtu/article/details/6518571
Android-animationdrawable (i)