歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android圖形與圖像處理-逐幀動畫

Android圖形與圖像處理-逐幀動畫

日期:2017/3/1 10:15:09   编辑:Linux編程

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. import android.app.Activity;
  3. import android.graphics.drawable.AnimationDrawable;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. public class FatPo extends Activity {
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. //獲取兩個按鈕
  15. Button play = (Button) findViewById(R.id.play);
  16. Button stop = (Button) findViewById(R.id.stop);
  17. ImageView imageView = (ImageView)findViewById(R.id.anim);
  18. //獲取AnimationDrawable動畫對象
  19. final AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();
  20. play.setOnClickListener(new OnClickListener() {
  21. @Override
  22. public void onClick(View v) {
  23. // TODO Auto-generated method stub
  24. //開始播放動畫
  25. anim.start();
  26. }
  27. });
  28. stop.setOnClickListener(new OnClickListener() {
  29. @Override
  30. public void onClick(View v) {
  31. // TODO Auto-generated method stub
  32. //停止播放動畫
  33. anim.stop();
  34. }
  35. });
  36. }
  37. }
Copyright © Linux教程網 All Rights Reserved