Android图形与图像处理-逐帧动画


Android图形与图像处理-逐帧动画

实例:功夫熊猫

创建项目:FatPo

项目运行效果:

   

项目截图:

布局文件:main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout 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.     android:background="#fff"  
  7.     >  
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  9.     android:orientation="horizontal"  
  10.     android:layout_width="fill_parent"  
  11.     android:layout_height="wrap_content"  
  12.     android:gravity="center"  
  13.     >  
  14. <Button   
  15.     android:id="@+id/play"  
  16.     android:layout_width="wrap_content"  
  17.     android:layout_height="wrap_content"  
  18.     android:text="@string/play"/>  
  19. <Button   
  20.     android:id="@+id/stop"  
  21.     android:layout_width="wrap_content"  
  22.     android:layout_height="wrap_content"  
  23.     android:text="@string/stop"/>      
  24. </LinearLayout>  
  25. <ImageView    
  26.     android:id="@+id/anim"  
  27.     android:layout_width="wrap_content"   
  28.     android:layout_height="wrap_content"   
  29.     android:background="@anim/fat_po"  
  30.     android:scaleType="center"  
  31.     />  
  32. </LinearLayout>  

动画资源资源文件:res\anim\fat_po.xml

要事先导入相应的图片资源

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 指定动画循环播放 -->  
  3. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:oneshot="false">  
  5.     <!-- 添加多个帧 -->  
  6.     <item android:drawable="@drawable/fat_po_f01" android:duration="60"/>  
  7.     <item android:drawable="@drawable/fat_po_f02" android:duration="60"/>  
  8.     <item android:drawable="@drawable/fat_po_f03" android:duration="60"/>  
  9.     <item android:drawable="@drawable/fat_po_f04" android:duration="60" />  
  10.     <item android:drawable="@drawable/fat_po_f05" android:duration="60" />  
  11.     <item android:drawable="@drawable/fat_po_f06" android:duration="60" />  
  12.     <item android:drawable="@drawable/fat_po_f07" android:duration="60" />  
  13.     <item android:drawable="@drawable/fat_po_f08" android:duration="60" />  
  14.     <item android:drawable="@drawable/fat_po_f09" android:duration="60" />  
  15.     <item android:drawable="@drawable/fat_po_f10" android:duration="60" />  
  16.     <item android:drawable="@drawable/fat_po_f11" android:duration="60" />  
  17.     <item android:drawable="@drawable/fat_po_f12" android:duration="60" />  
  18.     <item android:drawable="@drawable/fat_po_f13" android:duration="60" />  
  19.     <item android:drawable="@drawable/fat_po_f14" android:duration="60" />  
  20.     <item android:drawable="@drawable/fat_po_f15" android:duration="60" />  
  21.     <item android:drawable="@drawable/fat_po_f16" android:duration="60" />  
  22.     <item android:drawable="@drawable/fat_po_f17" android:duration="60" />  
  23.     <item android:drawable="@drawable/fat_po_f18" android:duration="60" />  
  24.     <item android:drawable="@drawable/fat_po_f19" android:duration="60" />  
  25.     <item android:drawable="@drawable/fat_po_f20" android:duration="60" />  
  26.     <item android:drawable="@drawable/fat_po_f21" android:duration="60" />      
  27.     <item android:drawable="@drawable/fat_po_f22" android:duration="60" />      
  28.     <item android:drawable="@drawable/fat_po_f23" android:duration="60" />      
  29.     <item android:drawable="@drawable/fat_po_f24" android:duration="60" />      
  30.     <item android:drawable="@drawable/fat_po_f25" android:duration="60" />      
  31.     <item android:drawable="@drawable/fat_po_f26" android:duration="60" />      
  32.     <item android:drawable="@drawable/fat_po_f27" android:duration="60" />      
  33. </animation-list>  

Activity文件:FatPo.java

  1. package wwj.fatpo;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.AnimationDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.ImageView;  
  10.   
  11. public class FatPo extends Activity {  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         //获取两个按钮   
  18.         Button play = (Button) findViewById(R.id.play);  
  19.         Button stop = (Button) findViewById(R.id.stop);  
  20.         ImageView imageView = (ImageView)findViewById(R.id.anim);  
  21.         //获取AnimationDrawable动画对象   
  22.         final AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();  
  23.         play.setOnClickListener(new OnClickListener() {  
  24.               
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 // TODO Auto-generated method stub   
  28.                 //开始播放动画   
  29.                 anim.start();  
  30.             }  
  31.         });  
  32.         stop.setOnClickListener(new OnClickListener() {  
  33.               
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 // TODO Auto-generated method stub   
  37.                 //停止播放动画   
  38.                 anim.stop();  
  39.             }  
  40.         });  
  41.     }  
  42. }  

相关内容