Frame-by-frame animation for Android Learning

Source: Internet
Author: User

Animations are divided into Frame-by-Frame, Tween, and attribute animations.

1. frame-by-frame animation

XML resource files are usually used for definition as follows:

Android: oneshot: sets whether to play the animation cyclically. false indicates that loop playback is the default setting. The xml file defines the Frame Animation resource. You can use an ImageView in the program to display the animation.

It should be pointed out that the animation represented by AnimationDrawable is not played by default. It needs to be started in the program and start () and stop () are called ().

 
 
 
 
 
 
 
 

Example of bullet explosion effect

Import java. lang. reflect. field; import android. app. activity; import android. content. context; import android. graphics. canvas; import android. graphics. drawable. animationDrawable; import android. media. mediaPlayer; import android. OS. bundle; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. frameLayout; import android. widget. imageView; public class Blast extends Activity {private MyView myView; private AnimationDrawable anim; private MediaPlayer bomb; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // use the FrameLayout layout manager, which allows the component to control its position FrameLayout frame = new FrameLayout (this); setContentView (frame); // sets the background frame. setBackgroundResource (R. drawable. back); // load the sound bomb = MediaPlayer. create (this, R. raw. bomb); myView = new MyView (this); // set myView to display the blast animation myView. setBackgroundResource (R. anim. blast); // set myView to hide myView by default. setVisibility (View. INVISIBLE); // get the animation object anim = (AnimationDrawable) myView. getBackground (); frame. addView (myView); frame. setOnTouchListener (new OnTouchListener () {public boolean onTouch (View source, MotionEvent event) {// only process the pressed event (avoid generating two animation effects each time) if (event. getAction () = MotionEvent. ACTION_DOWN) {// stop playing anim first. stop (); float x = event. getX (); float y = event. getY (); // control the display position of myView. setLocation (int) y-40, (int) x-20); myView. setVisibility (View. VISIBLE); // start the anim animation. start (); // play the sound bomb. start () ;}return false ;}}) ;}// define a custom View, this custom View is used to play the "explosion" effect class MyView extends ImageView {public MyView (Context context) {super (context) ;}// define a method, this method is used to control the display position of MyView public void setLocation (int top, int left) {this. setFrame (left, top, left + 40, top + 40);} // rewrite this method to control if the animation is played to the last frame, hide this Viewprotected void onDraw (Canvas canvas) {try {Field field = AnimationDrawable. class. getDeclaredField ("mCurFrame"); field. setAccessible (true); // get the current anim animation frame int curFrame = field. getInt (anim); // if the last frame has been reached if (curFrame = anim. getNumberOfFrames ()-1) {// hide setVisibility (View. INVISIBLE) ;}} catch (Exception e) {} super. onDraw (canvas );}}}
Blast. xml

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.