An approach to the implementation of the simple frame animation frame of Android programming _android

Source: Internet
Author: User

An example of this article is the implementation of a simple frame-by-step animation frame for Android programming. Share to everyone for your reference, specific as follows:

1. Frame animation

That is, by playing a predefined picture to achieve dynamic picture, feeling like a movie.

2. Implementation steps:

① in the project to import the pictures to play. This simple example is start_icon1,2,3.

② creates a new Anim folder in the engineering Res file directory and creates a new Start_animation.xml format file that defines the order in which the animation is played and the length of time each picture shows.

The code is as follows:

<?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/start_icon1" Android: duration= "1000"/>
  <item android:drawable= "@drawable/start_icon2" android:duration= "/>"
  < Item android:drawable= "@drawable/start_icon3" android:duration= "/>"
</animation-list>

Note: This blue part of the picture displayed in turn, stored in the drawable-mdpi file, the general 1 seconds to play 24 pictures (frames) on the feeling of smooth playback, that is, duration is about 40, the default unit is milliseconds.

3, Layout file:

A ImageView control is added to the layout file to play the animated picture. The specific layout is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" match_parent "
  android:o" rientation= "vertical" >
  <button
    android:id= "@+id/button1" android:layout_width= "Wrap_content"
    android:layout_height= "wrap_content"
    android:layout_gravity= "center"
    android:text= "Start"/>
  <button
    android:id= "@+id/button2"
    android:layout_width= "wrap_content"
    android:layout_ height= "Wrap_content"
     android:layout_gravity= "center"
    android:text= "End"/>
  <imageview
    android:id= "@+id/image"
    android:background= "@anim/start_animation"
    android:layout_width= "Fill_ Parent "
    android:layout_height=" fill_parent "/>
</LinearLayout>

4, Code section:

public class Testactivity extends activity
{
animationdrawable anim;
public void OnCreate (Bundle savedinstancestate)
{
super.oncreate (savedinstancestate);
Setcontentview (r.layout.start_screen);
ImageView image = (ImageView) Findviewbyid (r.id.image);
Image.setbackgroundresource (r.anim.start_animation);
Anim = (animationdrawable) image.getbackground ();
Button start = (Button) Findviewbyid (r.id.button1);
Button stop = (button) Findviewbyid (r.id.button2);
Start.setonclicklistener (New Onclicklistener ()
{public
void OnClick (View arg0)
{
Anim.start () ;
}
});
Stop.setonclicklistener (New Onclicklistener ()
{public
void OnClick (View arg0)
{
anim.stop ();
}
});
}
}

Note: In the third step, the android:background= "@anim/start_animation" and the Image.setbackgroundresource (R.anim.start_animation) in the fourth step; As long as you choose one you can, two are written to be cumbersome, the main function is to specify the playback of the resource picture.

Summary: This application should not be used in practical applications, for beginners, it is quite interesting to play with, not only to enhance the interest in Android learning, but also to deepen some understanding of making movies

I hope this article will help you with the Android program.

Related Article

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.