Android programming simple frame by frame animation Frame implementation method

  • 2020-11-26 18:59:00
  • OfStack

An example of Android programming is given to illustrate the implementation of Frame. To share for your reference, the details are as follows:

1. Frame by frame animation

That is, by playing pre-sorted images to achieve a dynamic screen, feel like a movie.

2. Implementation Steps:

Import the picture to play in the project. In this simple example, start_icon1, 2,3.

Create a new anim folder in the res file directory, and create a new ES17en_animation.xml file. This file is used to define the sequence of the animation images and the display time of each image.

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="500" />
  <item android:drawable="@drawable/start_icon3" android:duration="600" />
</animation-list>

Note: The images displayed in the blue part in turn are stored in drawable-ES26en file. 24 images (frames) will be played in one second, which means duration is about 40, and the default unit is milliseconds.

3. Layout file:

Add a 1ImageView control to the layout file to play animated images. 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:orientation="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=" The end of the " />
  <ImageView
    android:id="@+id/image"
    android:background="@anim/start_animation"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
</LinearLayout>

4. Code Part:


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: android:background in Step 3 =" @anim /start_animation" and ES44en.setBackgroundResource (R.anim.start_animation) in step 4; Just choose one. Both are redundant, and the main function is to specify the resource image to play.

Summary: This kind of application should not be used in practical application. For beginners, it is quite interesting to play with it. It not only increases their interest in Android learning, but also deepens their understanding of film-making

I hope this article has helped you with Android programming.


Related articles: