动画专题研究二

来源:互联网 发布:win10太卡怎么优化 编辑:程序博客网 时间:2024/05/21 22:08

动画效果二 ----Frame Animation


  • 新建工程 myFrameAnimation

 

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


a.png

  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.         
  33.         start=(Button) findViewById(R.id.Button_start);
  34.         stop=(Button) findViewById(R.id.Button_stop);
  35.         Cycle=(CheckBox) findViewById(R.id.CheckBox_ifCycle_orNot);
  36.         /*
  37.          * findViewById()从XML获取 Button  CheckBox
  38.          */
  39.         
  40.         myImage = (ImageView) findViewById(R.id.rocket_image);
  41.         /*
  42.          * findViewById()从XML中获取ImageView 对象myImage
  43.          */
  44.         myImage.setBackgroundResource(R.anim.myframeanimation);
  45.         /*
  46.          * ImageView.setBackgroundResource()设置 图片View的背景图片
  47.          * 这里是把帧动画 myframeanimation加到 图片View的背景中
  48.          */
  49.         frameAnimation = (AnimationDrawable) myImage.getBackground();
  50.         /*
  51.          * myImage.getBackground()获得背景的Drawable的对象,转换成AnimationDrawable
  52.          */
  53.         
  54.         start.setOnClickListener(this);
  55.         stop.setOnClickListener(this);
  56.     }
  57.     /*
  58.      * (non-Javadoc)
  59.      * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
  60.      */
  61.     @Override
  62.     public boolean onTouchEvent(MotionEvent event) {
  63.         frameAnimation.setOneShot(isChecked_cycle);
  64.         /*
  65.          * 添加触摸事件处理方法
  66.          */
  67.         // TODO Auto-generated method stub
  68.         if(event.getAction()==MotionEvent.ACTION_DOWN){
  69.             /*
  70.              * MotionEvent.getAction()获取事件动作
  71.              * MotionEvent.ACTION_DOWN 向下的手势动作
  72.              */
  73.         /*event.getAction() 返回正被执行的动作种类:
  74.          * 是     ACTION_DOWN, ACTION_MOVE, ACTION_UP, 或 ACTION_CANCEL中的一个.
  75.          */
  76.             frameAnimation.start();
  77.             /*
  78.              * 启动帧动画效果
  79.              */
  80.             return true;
  81.         }
  82.         return super.onTouchEvent(event);
  83.     }
  84.     /*
  85.      * (non-Javadoc)
  86.      * @see android.view.View.OnClickListener#onClick(android.view.View)
  87.      */
  88.     @Override
  89.     public void onClick(View button) {
  90.         // TODO Auto-generated method stub
  91.         switch (button.getId()) {
  92.         case R.id.Button_start:{
  93.             if(Cycle.isChecked()){
  94.                 Toast.makeText(this, "动画重复", Toast.LENGTH_LONG).show();
  95.                 isChecked_cycle=false;
  96.             }else{
  97.                 Toast.makeText(this, "不重复", Toast.LENGTH_LONG).show();
  98.                 isChecked_cycle=true;
  99.             }
  100.             /*
  101.              * 复选按钮选中,  动画重复播放,  AnimationDrawable.setOneShot(false)
  102.              * 复选按钮未选中,动画不重复播放,AnimationDrawable.setOneShot(true)
  103.              */
  104.             
  105.             frameAnimation.setOneShot(isChecked_cycle);
  106.             /*
  107.              * 设置重复与否
  108.              */
  109.             frameAnimation.start();
  110.             /*
  111.              *启动帧动画效果
  112.              */
  113.             
  114.         }
  115.             break;
  116.         case R.id.Button_stop:{
  117.             if(frameAnimation.isRunning()){
  118.                 /*
  119.                  * AnimationDrawable.isRunning(),判断帧动画是否在运行,true---运行中
  120.                  * 如果动画正在运行,可以停止
  121.                  */
  122.                 frameAnimation.stop();
  123.                 /*
  124.                  *停止帧动画效果
  125.                  */
  126.             }
  127.             
  128.         }
  129.             break;
  130.         default:
  131.             break;
  132.         }
  133.     }
  134. }
复制代码
  • 运行结果


1.png


2.png

下载 (29.18 KB)
2009-6-17 05:10



  • 源码