歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android --- Frame動畫示例

Android --- Frame動畫示例

日期:2017/3/1 10:23:24   编辑:Linux編程

Frame動畫:

1、找到一組圖片c01.jpg,c02.jpg,c03.jpg,c04.jpg,c05.jpg,copy到res/drawable目錄下;

2、在res/drawable目錄下新建XML文件:frame_anim.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <animation-list
  3. xmlns:Android="http://schemas.android.com/apk/res/android" android:oneshot="false">
  4. <item android:drawable="@drawable/c01" android:duration="500"/>
  5. <item android:drawable="@drawable/c02" android:duration="500"/>
  6. <item android:drawable="@drawable/c03" android:duration="500"/>
  7. <item android:drawable="@drawable/c04" android:duration="500"/>
  8. <item android:drawable="@drawable/c05" android:duration="500"/>
  9. </animation-list>

3、在res/layout目錄下新建XML文件:frame_anim_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ImageView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:scaleType="centerInside"
  11. android:id="@+id/imgFrame"
  12. android:background="@drawable/frame_anim"></ImageView>
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content">
  16. <Button
  17. android:text="開始"
  18. android:id="@+id/btnStart"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_weight="1.0"></Button>
  22. <Button
  23. android:text="結束"
  24. android:id="@+id/btnEnd"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="1.0"></Button>
  28. </LinearLayout>
  29. </LinearLayout>

4、Activity文件中的代碼:

  1. package com.bison;
  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 AnimationDemoActivity extends Activity implements OnClickListener {
  10. // 開始按鈕
  11. private Button btnStart;
  12. // 結束按鈕
  13. private Button btnEnd;
  14. private ImageView imgFrame;
  15. // 聲明Frame動畫對象
  16. private AnimationDrawable frameAnim;
  17. /** 初始化 */
  18. public void init() {
  19. btnStart = (Button) findViewById(R.id.btnStart);
  20. btnEnd = (Button) findViewById(R.id.btnEnd);
  21. btnStart.setOnClickListener(this);
  22. btnEnd.setOnClickListener(this);
  23. imgFrame = (ImageView) findViewById(R.id.imgFrame);
  24. // 將ImageView的backgroud聲明給Frame動畫對象
  25. frameAnim = (AnimationDrawable) imgFrame.getBackground();
  26. }
  27. @Override
  28. public void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. // 加載layout.frame_anim_layout頁面
  31. setContentView(R.layout.frame_anim_layout);
  32. init();
  33. }
  34. public void onClick(View v) {
  35. // 判斷按鈕事件
  36. switch (v.getId()) {
  37. case R.id.btnStart:
  38. frameAnim.start();
  39. break;
  40. case R.id.btnEnd:
  41. frameAnim.stop();
  42. break;
  43. }
  44. }
  45. }

PS:Frame動畫原理類似於電影膠片,一幕一幕的閃過,在人的視覺停留期快速變動,形成組圖,產生動畫。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved