Android Animation Demo (Frame-by-Frame Animation)

Source: Internet
Author: User

The previous article introduced the Animation of Animation: The Tween Animation.

Next, this article introduces another Animation form of Animation: Frame-by-Frame Animation.

Frame Animation is a process of displaying a series of images in a certain sequence. It is similar to the mechanism of playing a movie. It is called Frame-by-Frame animation. Frame animations can be defined in XML files or fully encoded (the source code Demo of these two implementation methods will be provided later ).

The following sections describe:

1. Implementation of definitions in xml:

Implementation:

Source code:

Layout file: main. xml:

 
     
      
      
      
  
 

Frame. xml:

  
 Android: oneshot = "false">
     
     
     
     
     
     
 

FrameDemoActivity:

Package com.zhy.com; import android. app. activity; import android. graphics. drawable. animationDrawable; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView;/*** Frame-by-Frame animation Frame Animation instance **/public class FrameDemoActivity extends Activity {private Button startBtn; // start Animation Button private Button stopBtn; // stop the Animation button private ImageView imageView; // display the image private AnimationDrawable anim; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // instantiate the control startBtn = (Button) findViewById (R. id. startButton); stopBtn = (Button) findViewById (R. id. stopButton); imageView = (ImageView) findViewById (R. id. image); // specifies the imageView list of animation frames. setBackgroundResource (R. anim. frame); // AnimationDrawable -- Drawableanim = (AnimationDrawable) imageView associated with a frame-by-frame animation. getBackground (); // button event startBtn. setOnClickListener (new OnClickListener () {public void onClick (View v) {// start anim animation. start () ;}}); stopBtn. setOnClickListener (new OnClickListener () {public void onClick (View v) {anim. stop (); // stop playing }});}}

II. Implementation of direct code encoding:

Implementation:

Source code:

Layout file:

Activity_main:

 
     
      
      
      
  
 

MainActivity:

Package com. framedemo2; import android. app. activity; import android. graphics. drawable. animationDrawable; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; public class MainActivity extends Activity implements OnClickListener {private Button startBtn; // start Animation Button p Rivate Button stopBtn; // stop the Animation Button private ImageView imageView; // display the image private AnimationDrawable anim; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // instantiate the control startBtn = (Button) findViewById (R. id. startButton); stopBtn = (Button) findViewById (R. id. stopButton); imageView = (ImageView) findViewById (R. id. image); anim = new AnimationDrawable (); startBtn. setOnClickListener (this); stopBtn. setOnClickListener (this); for (int I = 1; I <= 3; I ++) {// obtain R Based on the Resource Name and directory. the corresponding resource IDint id = getResources () in java (). getIdentifier ("f" + I, "drawable", getPackageName (); // obtain the Drawable object Drawable drawable = getResources () Based on the Resource ID (). getDrawable (id); // Add this frame to anim. addFrame (drawable, 300);} anim. setOneShot (false); // if it is set to false, it will only be played once and will not be followed Loop playback. ImageView. setBackgroundDrawable (anim); // set the animation to ImageView background} @ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. startButton: anim. start (); break; case R. id. stopButton: anim. stop (); break; default: break ;}}}

The following provides the source code of the above two methods for your reference:

Source code for frame-by-frame animation in xml format:

Click to download source code

The source code for frame-by-frame animation is directly encoded:

Click to download source code

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.