Android Animation by Frame animation (frame Animation) Examples of detailed _android

Source: Internet
Author: User

The example of this article analyzes the frame animation of Android animation. Share to everyone for your reference, specific as follows:

Before starting an instance, refer to a passage in the Official document:

Frame animation is a series of pictures in a certain order to show the process, and the mechanism of the film is very similar to what we call a frame-by-step animation. A frame animation can be defined in an XML file, or it can be fully encoded.

If defined in an XML file, we can place it in the Anim or drawable directory under/res (/res/[anim | drawable]/filename.xml), and the filename can be referenced in code as a resource ID, if it is implemented entirely by encoding, We need to use the Animationdrawable object.

If you are defining an animation in an XML file, the syntax is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <animation-list xmlns:android=
"http://schemas.android.com/apk" /res/android "
  android:oneshot=[" true "| "false"] >
  <item
    android:drawable= "@[package:]drawable/drawable_resource_name"
    android: duration= "integer"/>
</animation-list>

It is to be noted that:

The <animation-list> element is required and must be a root element that can contain one or more <item> elements; Android:onshot If true, this animation will only execute once. If False, it loops all the time.
The <item> element represents a frame animation, android:drawable specifies the picture resource corresponding to this frame animation, android:druation represents the duration of the frame, integer, in milliseconds.

The next example of the document I'm not explaining it, because we're going to have to demonstrate this process in conjunction with our own examples.

We create a new project called Anim, name the four consecutive pictures as f1.png,f2.png,f3.png,f4.png, place them in the drawable directory, and create a new Frame.xml file:

<?xml version= "1.0" encoding= "Utf-8"?> <animation-list xmlns:android=
"http://schemas.android.com/apk" /res/android "
  android:oneshot=" false ">
  <item android:drawable=" @drawable/f1 "android:duration=" 300 "/>
  <item android:drawable=" @drawable/f2 android:duration= "/> <item"
  @ Drawable/f3 "android:duration="/>
  <item android:drawable= "@drawable/f4" android:duration= "the/>"
</animation-list>

We can place the Frame.xml file in the drawable or Anim directory, the official document is placed in the drawable, you can be placed according to preferences, placed in both directories can be run.

Then introduce the layout file Res/layout/frame.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
 "http://schemas.android.com/" Apk/res/android "
 android:orientation=" vertical "
 android:layout_width=" fill_parent "
 android:layout_" height= "Fill_parent" >
 <imageview
  android:id= "@+id/frame_image" android:layout_width= "Fill_"
  Parent "
  android:layout_height=" fill_parent "
  android:layout_weight=" 1 "/>
 <button
  Android:layout_width= "Fill_parent"
  android:layout_height= "wrap_content"
  android:text= "StopFrame"
  android:onclick= "Stopframe"/>
 <button
  android:layout_width= "Fill_parent"
  android: layout_height= "Wrap_content"
  android:text= "Runframe" android:onclick= "Runframe
  "/>
</ Linearlayout>

We define a imageview as the carrier of the animation, and then define two buttons, respectively, to stop and start the animation.

Here's how to animate the animation by loading the animation definition file. We'll start by saying this:

Package Com.scott.anim;
Import android.app.Activity;
Import android.graphics.drawable.AnimationDrawable;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ImageView;
public class Frameactivity extends activity {
  private imageview image;
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.frame);
    Image = (ImageView) Findviewbyid (r.id.frame_image);
    Image.setbackgroundresource (r.anim.frame);
    Animationdrawable Anim = (animationdrawable) image.getbackground ();
    Anim.start ();
  }


It seems perfect, as the official document says, but when we run this program we find that it stays in the first frame and doesn't show the animation that we expect, and you might be disappointed to say, "Why?", and then you put the corresponding code in a button click event, the animation is executed smoothly, Then move back to the oncreate, or no effect, this time I guess you will be flustered roar: "What the fuck!". But what is the reason? How to solve it?

This behavior occurs because when we call the Animationdrawable start method in OnCreate, the Windows window object is not fully initialized, and animationdrawable cannot be fully appended to the Windows window object. So what should we do? We need to put this code in the Onwindowfocuschanged method, and when the activity is presented to the user, the Onwindowfocuschanged method is invoked, and it is at this time that we achieve our animation effect. Of course, Onwindowfocuschanged was invoked after OnCreate, as shown in the figure:

Then we need to rewrite the code:

Package Com.scott.anim;
Import android.app.Activity;
Import android.graphics.drawable.AnimationDrawable;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ImageView;
public class Frameactivity extends activity {
  private imageview image;
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.frame);
    Image = (ImageView) Findviewbyid (r.id.frame_image);
  }
  @Override public
  void Onwindowfocuschanged (Boolean hasfocus) {
    super.onwindowfocuschanged (hasfocus);
    Image.setbackgroundresource (r.anim.frame);
    Animationdrawable Anim = (animationdrawable) image.getbackground ();
    Anim.start ();
  }


Run it, and the animation will be displayed properly.

If on some occasions we need to implement an animation in pure code, we can write this:

Animationdrawable anim = new animationdrawable ();
for (int i = 1; I <= 4; i++) {
  int id = getresources (). Getidentifier ("F" + I, "drawable", Getpackagename ());
  drawable drawable = Getresources (). getdrawable (ID);
  Anim.addframe (drawable);
Anim.setoneshot (false);
Image.setbackgrounddrawable (anim);
Anim.start ();

The complete Frameactivity.java code is as follows:

Package Com.scott.anim;
Import android.app.Activity;
Import android.graphics.drawable.AnimationDrawable;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.ImageView;
  public class Frameactivity extends activity {private ImageView image;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.frame);
  Image = (ImageView) Findviewbyid (r.id.frame_image);
    @Override public void Onwindowfocuschanged (Boolean hasfocus) {super.onwindowfocuschanged (hasfocus); Image.setbackgroundresource (R.anim.frame); Sets the animation resource file to the ImageView background animationdrawable anim = (animationdrawable) image.getbackground ();  Gets the ImageView background, at this time has been compiled into Animationdrawable Anim.start ();
    Start animation} public void Stopframe (view view) {animationdrawable Anim = (animationdrawable) image.getbackground (); if (anim.isrunning ()) {//If it is running, stop anim.stop ();
    } public void Runframe (view view) {//Full code-implemented animation effect animationdrawable Anim = new animationdrawable (); for (int i = 1; I <= 4; i++) {//Get the corresponding resource id int id = getresources () in R.java according to the resource name and directory. Getidentifier ("F" + I, "
      Drawable ", Getpackagename ());
      Gets the Drawable object drawable drawable = Getresources (). getdrawable (ID) based on the resource ID;
    Add this frame to the animationdrawable anim.addframe (drawable, 300); } anim.setoneshot (FALSE); Set to Loop image.setbackgrounddrawable (ANIM);  Set the animation to ImageView background anim.start ();

 Start animation}

I hope this article will help you with your Android programming.

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.