In Android, you cannot directly use Gif images to play frame animations. The following animations cannot be played in Android:
Android provides another solution, which is to use the AnimationDrawable function to enable frame-by-frame playback. But how can we split gif images into images of each frame? The following describes two types of good software that can help us scatter images.
Gifsplitter2.0
: Gif split http://www.bkjia.com/uploadfile/2012/0315/20120315102824652.rar
The usage is as follows:
This software splits the image into bmp images, which are relatively large and are not recommended here, saving unnecessary bytes as much as possible. Therefore, the following software is recommended here.
Easygifanimator
Software downloads: animation splitter http://www.bkjia.com/uploadfile/2012/0315/20120315103116885.rar
The usage is as follows:
Click the file to export the frame file.
After getting the frame file, we can write code, create an anim animation folder under the res directory, and write the following code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Animation-list android: oneshot = "false"
Xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: duration = "150" android: drawable = "@ drawable/xiu0"/>
<Item android: duration = "150" android: drawable = "@ drawable/xiu1"/>
<Item android: duration = "150" android: drawable = "@ drawable/xiu2"/>
<Item android: duration = "150" android: drawable = "@ drawable/xiu3"/>
</Animation-list>
The corresponding item is the sequence of the image from the start to the end, the duration is the playback interval of each frame by frame, and the oneshot is false, which indicates loop playback. If it is set to true, the playback stops once.
The code for the corresponding Activity is as follows:
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. ImageView;
Public class animActivity extends Activity implements OnClickListener {
ImageView iv = null;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Iv = (ImageView) findViewById (R. id. ImageView01 );
Iv. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
AnimationDrawable anim = null;
Object ob = iv. getBackground ();
Anim = (AnimationDrawable) ob;
Anim. stop ();
Anim. start ();
}
}
Use the AnimationDrawable object to obtain the image, and then specify the AnimationDrawable to start playing the animation.
Tip: This method does not play the video by default. You must trigger an event to play the animation.
So how can I use images for automatic playback? We can consider whether the progress bar will be converted by default, that is, the circular progress bar, yes. We can modify and merge it or play it automatically. Create a styles file under the Values file and write the following code:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "animStyle" parent = "@ android: style/Widget. ProgressBar. Large">
<Item name = "android: indeterminateDrawable"> @ anim/test </item>
</Style>
</Resources>
The Style File above is used to set an animation file for the Widget. ProgressBar. Large. We can set its style in XML to make it work for us.
<ProgressBar android: id = "@ + id/ProgressBar01" style = "@ style/animStyle"
Android: layout_width = "128px" android: layout_height = "128px"> </ProgressBar>
OK, it is so simple. Let's take a look at the running effect:
Source code download: http://www.bkjia.com/uploadfile/2012/0315/20120315104124146.rar
From Terry _ Dragon