Android动画--Frame Animation



    • 新建工程 myFrameAnimation

    • 在main.xml布局中添加view子类,调整一下,效果如下:

    Android动画--Frame Animation
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:prientation="vertical" android:layout_width="fill_parent"
    4. android:layout_height="fill_parent">
    5. <LinearLayout android:prientation="horizontal"
    6. android:layout_width="fill_parent" android:layout_height="wrap_content"
    7. android:background="@drawable/bt_group_back" android:layout_marginTop="10px">
    8. <Button android:text="播放动画" android:layout_width="100px"
    9. android:id="@+id/Button_start" android:layout_height="fill_parent"></Button>
    10. <Button android:layout_width="100px" android:text="停止动画"
    11. android:id="@+id/Button_stop" android:layout_height="fill_parent"></Button>
    12. <CheckBox android:text="动画重复" android:layout_width="100px"
    13. android:id="@+id/CheckBox_ifCycle_orNot" style="?android:attr/starStyle"
    14. android:layout_height="fill_parent"></CheckBox>
    15. </LinearLayout>
    16. <ImageView android:id="@+id/rocket_image"
    17. android:layout_width="80px" android:layout_height="80px"
    18. android:background="@drawable/android_large"
    19. android:layout_marginLeft="100dp" android:layout_marginTop="100dp"></ImageView>
    20. </LinearLayout>
    复制代码
    • 找几个动态图片,把它分成单个图。(主要是为了讲解/是用Frame Animation效果,AnimationDrawable)


    • 修改mainActivity.java的代码
    1. package zyf.my.frame.animation;

    2. import android.app.Activity;
    3. import android.graphics.drawable.AnimationDrawable;
    4. import android.os.Bundle;
    5. import android.view.MotionEvent;
    6. import android.view.View;
    7. import android.widget.Button;
    8. import android.widget.CheckBox;
    9. import android.widget.ImageView;
    10. import android.widget.Toast;
    11. public class myFrameAnimatino extends Activity implements Button.OnClickListener {
    12. /** Called when the activity is first created. */
    13. AnimationDrawable frameAnimation;
    14. /*
    15. * 声明AnimationDrawable 可绘制动画 对象frameAnimation
    16. */
    17. ImageView myImage;
    18. /*
    19. * 图片View ImageView
    20. */
    21. Button start,stop;
    22. CheckBox Cycle;
    23. boolean isChecked_cycle=true;
    24. /*
    25. * (non-Javadoc)
    26. * @see android.app.Activity#onCreate(android.os.Bundle)
    27. */
    28. @Override
    29. public void onCreate(Bundle savedInstanceState) {
    30. super.onCreate(savedInstanceState);
    31. setContentView(R.layout.main);

    32. start=(Button) findViewById(R.id.Button_start);
    33. stop=(Button) findViewById(R.id.Button_stop);
    34. Cycle=(CheckBox) findViewById(R.id.CheckBox_ifCycle_orNot);
    35. /*
    36. * findViewById()从XML中获取 ButtonCheckBox
    37. */

    38. myImage = (ImageView) findViewById(R.id.rocket_image);
    39. /*
    40. * findViewById()从XML中获取ImageView 对象myImage
    41. */
    42. myImage.setBackgroundResource(R.anim.myframeanimation);
    43. /*
    44. * ImageView.setBackgroundResource()设置 图片View的背景图片
    45. * 这里是把帧动画 myframeanimation加到 图片View的背景中
    46. */
    47. frameAnimation = (AnimationDrawable) myImage.getBackground();
    48. /*
    49. * myImage.getBackground()获得背景的Drawable的对象,转换成AnimationDrawable
    50. */

    51. start.setOnClickListener(this);
    52. stop.setOnClickListener(this);
    53. }
    54. /*
    55. * (non-Javadoc)
    56. * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
    57. */
    58. @Override
    59. public boolean onTouchEvent(MotionEvent event) {
    60. frameAnimation.setOneShot(isChecked_cycle);
    61. /*
    62. * 添加触摸事件处理方法
    63. */
    64. // TODO Auto-generated method stub
    65. if(event.getAction()==MotionEvent.ACTION_DOWN){
    66. /*
    67. * MotionEvent.getAction()获取事件动作
    68. * MotionEvent.ACTION_DOWN 向下的手势动作
    69. */

    70. /*event.getAction() 返回正被执行的动作种类:
    71. * 是 ACTION_DOWN, ACTION_MOVE, ACTION_UP, 或 ACTION_CANCEL中的一个.
    72. */
    73. frameAnimation.start();
    74. /*
    75. * 启动帧动画效果
    76. */
    77. return true;

    78. }
    79. return super.onTouchEvent(event);
    80. }
    81. /*
    82. * (non-Javadoc)
    83. * @see android.view.View.OnClickListener#onClick(android.view.View)
    84. */
    85. @Override
    86. public void onClick(View button) {
    87. // TODO Auto-generated method stub
    88. switch (button.getId()) {
    89. case R.id.Button_start:{
    90. if(Cycle.isChecked()){
    91. Toast.makeText(this, "动画重复", Toast.LENGTH_LONG).show();
    92. isChecked_cycle=false;
    93. }else{
    94. Toast.makeText(this, "不重复", Toast.LENGTH_LONG).show();
    95. isChecked_cycle=true;
    96. }
    97. /*
    98. * 复选按钮选中,动画重复播放,AnimationDrawable.setOneShot(false)
    99. * 复选按钮未选中,动画不重复播放,AnimationDrawable.setOneShot(true)
    100. */

    101. frameAnimation.setOneShot(isChecked_cycle);
    102. /*
    103. * 设置重复与否
    104. */
    105. frameAnimation.start();
    106. /*
    107. *启动帧动画效果
    108. */

    109. }
    110. break;
    111. case R.id.Button_stop:{
    112. if(frameAnimation.isRunning()){
    113. /*
    114. * AnimationDrawable.isRunning(),判断帧动画是否在运行,true---运行中
    115. * 如果动画正在运行,可以停止
    116. */
    117. frameAnimation.stop();
    118. /*
    119. *停止帧动画效果
    120. */
    121. }

    122. }
    123. break;
    124. default:
    125. break;
    126. }
    127. }
    128. }
    复制代码
    • 运行结果

    Android动画--Frame Animation
    Android动画--Frame Animation
    2009-6-17 05:10:39 上传
    下载附件(29.18 KB)


    新建工程 myFrameAnimation

  • 在main.xml布局中添加view子类,调整一下,效果如下:

Android动画--Frame Animation
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:prientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayout android:prientation="horizontal"
  6. android:layout_width="fill_parent" android:layout_height="wrap_content"
  7. android:background="@drawable/bt_group_back" android:layout_marginTop="10px">
  8. <Button android:text="播放动画" android:layout_width="100px"
  9. android:id="@+id/Button_start" android:layout_height="fill_parent"></Button>
  10. <Button android:layout_width="100px" android:text="停止动画"
  11. android:id="@+id/Button_stop" android:layout_height="fill_parent"></Button>
  12. <CheckBox android:text="动画重复" android:layout_width="100px"
  13. android:id="@+id/CheckBox_ifCycle_orNot" style="?android:attr/starStyle"
  14. android:layout_height="fill_parent"></CheckBox>
  15. </LinearLayout>
  16. <ImageView android:id="@+id/rocket_image"
  17. android:layout_width="80px" android:layout_height="80px"
  18. android:background="@drawable/android_large"
  19. android:layout_marginLeft="100dp" android:layout_marginTop="100dp"></ImageView>
  20. </LinearLayout>
复制代码
  • 找几个动态图片,把它分成单个图。(主要是为了讲解/是用Frame Animation效果,AnimationDrawable)


  • 修改mainActivity.java的代码
  1. package zyf.my.frame.animation;

  2. import android.app.Activity;
  3. import android.graphics.drawable.AnimationDrawable;
  4. import android.os.Bundle;
  5. import android.view.MotionEvent;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.ImageView;
  10. import android.widget.Toast;
  11. public class myFrameAnimatino extends Activity implements Button.OnClickListener {
  12. /** Called when the activity is first created. */
  13. AnimationDrawable frameAnimation;
  14. /*
  15. * 声明AnimationDrawable 可绘制动画 对象frameAnimation
  16. */
  17. ImageView myImage;
  18. /*
  19. * 图片View ImageView
  20. */
  21. Button start,stop;
  22. CheckBox Cycle;
  23. boolean isChecked_cycle=true;
  24. /*
  25. * (non-Javadoc)
  26. * @see android.app.Activity#onCreate(android.os.Bundle)
  27. */
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);

  32. start=(Button) findViewById(R.id.Button_start);
  33. stop=(Button) findViewById(R.id.Button_stop);
  34. Cycle=(CheckBox) findViewById(R.id.CheckBox_ifCycle_orNot);
  35. /*
  36. * findViewById()从XML中获取 ButtonCheckBox
  37. */

  38. myImage = (ImageView) findViewById(R.id.rocket_image);
  39. /*
  40. * findViewById()从XML中获取ImageView 对象myImage
  41. */
  42. myImage.setBackgroundResource(R.anim.myframeanimation);
  43. /*
  44. * ImageView.setBackgroundResource()设置 图片View的背景图片
  45. * 这里是把帧动画 myframeanimation加到 图片View的背景中
  46. */
  47. frameAnimation = (AnimationDrawable) myImage.getBackground();
  48. /*
  49. * myImage.getBackground()获得背景的Drawable的对象,转换成AnimationDrawable
  50. */

  51. start.setOnClickListener(this);
  52. stop.setOnClickListener(this);
  53. }
  54. /*
  55. * (non-Javadoc)
  56. * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
  57. */
  58. @Override
  59. public boolean onTouchEvent(MotionEvent event) {
  60. frameAnimation.setOneShot(isChecked_cycle);
  61. /*
  62. * 添加触摸事件处理方法
  63. */
  64. // TODO Auto-generated method stub
  65. if(event.getAction()==MotionEvent.ACTION_DOWN){
  66. /*
  67. * MotionEvent.getAction()获取事件动作
  68. * MotionEvent.ACTION_DOWN 向下的手势动作
  69. */

  70. /*event.getAction() 返回正被执行的动作种类:
  71. * 是 ACTION_DOWN, ACTION_MOVE, ACTION_UP, 或 ACTION_CANCEL中的一个.
  72. */
  73. frameAnimation.start();
  74. /*
  75. * 启动帧动画效果
  76. */
  77. return true;

  78. }
  79. return super.onTouchEvent(event);
  80. }
  81. /*
  82. * (non-Javadoc)
  83. * @see android.view.View.OnClickListener#onClick(android.view.View)
  84. */
  85. @Override
  86. public void onClick(View button) {
  87. // TODO Auto-generated method stub
  88. switch (button.getId()) {
  89. case R.id.Button_start:{
  90. if(Cycle.isChecked()){
  91. Toast.makeText(this, "动画重复", Toast.LENGTH_LONG).show();
  92. isChecked_cycle=false;
  93. }else{
  94. Toast.makeText(this, "不重复", Toast.LENGTH_LONG).show();
  95. isChecked_cycle=true;
  96. }
  97. /*
  98. * 复选按钮选中,动画重复播放,AnimationDrawable.setOneShot(false)
  99. * 复选按钮未选中,动画不重复播放,AnimationDrawable.setOneShot(true)
  100. */

  101. frameAnimation.setOneShot(isChecked_cycle);
  102. /*
  103. * 设置重复与否
  104. */
  105. frameAnimation.start();
  106. /*
  107. *启动帧动画效果
  108. */

  109. }
  110. break;
  111. case R.id.Button_stop:{
  112. if(frameAnimation.isRunning()){
  113. /*
  114. * AnimationDrawable.isRunning(),判断帧动画是否在运行,true---运行中
  115. * 如果动画正在运行,可以停止
  116. */
  117. frameAnimation.stop();
  118. /*
  119. *停止帧动画效果
  120. */
  121. }

  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. }
复制代码
  • 运行结果

Android动画--Frame Animation
Android动画--Frame Animation
2009-6-17 05:10:39 上传
下载附件(29.18 KB)