In this example, 20 images are played at a frame and two buttons are used to control the start and stop of playback.
Frame. xml file
1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
3. android: orientation = "vertical"
4. android: layout_width = "fill_parent"
5. android: layout_height = "fill_parent">
6. <ImageView android: id = "@ + id/imageview"
7. android: layout_width = "200px"
8. android: layout_height = "200px"
9. android: src = "@ drawable/anim_01"
10. android: scaleType = "fitCenter"
11. android: layout_centerHorizontal = "true"
12. android: background = "# ffffff"/>
13. <Button android: id = "@ + id/start"
14. android: text = "Start Animation"
15. android: layout_below = "@ id/imageview"
16. android: layout_width = "fill_parent"
17. android: layout_height = "wrap_content"/>
18. <Button android: id = "@ + id/stop"
19. android: text = "End Animation"
20. android: layout_below = "@ id/start"
21. android: layout_width = "fill_parent"
22. android: layout_height = "wrap_content"/>
23. </RelativeLayout>
Here, the android: scaleType attribute in ImageView (ImageView. setScaleType (ImageView. ScaleType) Description:
CENTER/center is centered according to the original size of the image. When the length/width of the image exceeds the length/width of the View, the CENTER part of the captured image is displayed.
CENTER_CROP/centerCrop scales up the image size in the center, so that the image length (width) is equal to or greater than the View length (width)
CENTER_INSIDE/centerInside shows the entire content of the image in the center. The image length/width is equal to or less than the View length/width by proportional reduction or the original size.
FIT_CENTER/fitCenter: scales up or down the image to the width of the View. In the center, FIT_END/fitEnd scales up or down the image to the width of the View, which is displayed in the lower part of the View.
FIT_START/fitStart scales up/down an image proportionally to the width of the View.
FIT_XY/fitXY disable image proportion
1. public class FrameActivity extends Activity {
2.
3. private ImageView imageView;
4. private Button start;
5. private Button stop;
6. private AnimationDrawable animationDrawable;
7.
8. @ Override
9. public void onCreate (Bundle savedInstanceState ){
10. super. onCreate (savedInstanceState );
11. setContentView (R. layout. frame );
12. imageView = (ImageView) findViewById (R. id. imageview );
13. start = (Button) findViewById (R. id. start );
14. start. setOnClickListener (start_listener );
15. stop = (Button) findViewById (R. id. stop );
16. stop. setOnClickListener (stop_listener );
17.
18. animationDrawable = new AnimationDrawable ();
19. // Add an animation for each frame
20. for (int I = 1; I <= 20; I ++ ){
21. int id = getResources (). getIdentifier (
22. "anim" + (I> 9? "_": "_ 0") + I, "drawable", "com. will. frame ");
23. animationDrawable. addFrame (getResources (). getDrawable (id), 150 );
24 .}
25. // set whether to play the video repeatedly. If the value is false, the video is repeated.
26. animationDrawable. setOneShot (false );
27. imageView. setImageDrawable (animationDrawable );
28 .}
29.
30. // start playing
31. Button. OnClickListener start_listener = new Button. OnClickListener (){
32.
33. @ Override
34. public void onClick (View arg0 ){
35. animationDrawable. start ();
36 .}
37 .};
38.
39. // stop playing
40. Button. OnClickListener stop_listener = new Button. OnClickListener (){
41.
42. @ Override
43. public void onClick (View arg0 ){
44. animationDrawable. stop ();
45 .}
46 .};
47 .}