这个例子是一帧一帧的播放20张图片,通过两个按钮控制播放的开始和停止

frame.xml文件

 
  
  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/p_w_picpathview"  
  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="开始动画" 
  15.         android:layout_below="@id/p_w_picpathview"  
  16.         android:layout_width="fill_parent" 
  17.         android:layout_height="wrap_content"/> 
  18.     <Button android:id="@+id/stop" 
  19.         android:text="结束动画" 
  20.         android:layout_below="@id/start"  
  21.         android:layout_width="fill_parent" 
  22.         android:layout_height="wrap_content"/> 
  23. </RelativeLayout> 

其中,ImageView中的android:scaleType属性(ImageView.setScaleType(ImageView.ScaleType))说明:

CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例

 
 
  
  1. public class FrameActivity extends Activity { 
  2.      
  3.     private ImageView p_w_picpathView; 
  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.         p_w_picpathView = (ImageView)findViewById(R.id.p_w_picpathview); 
  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.         //添加每一帧动画
  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.         //设置手否重复播放,false为重复 
  26.         animationDrawable.setOneShot(false); 
  27.         p_w_picpathView.setImageDrawable(animationDrawable); 
  28.     } 
  29.      
  30.     //开始播放 
  31.     Button.OnClickListener start_listener = new Button.OnClickListener() { 
  32.  
  33.         @Override 
  34.         public void onClick(View arg0) { 
  35.             animationDrawable.start(); 
  36.         } 
  37.     }; 
  38.      
  39.     //停止播放 
  40.     Button.OnClickListener stop_listener = new Button.OnClickListener() { 
  41.  
  42.         @Override 
  43.         public void onClick(View arg0) { 
  44.             animationDrawable.stop();   
  45.         } 
  46.     };