android 逐帧动画

来源:互联网 发布:大数据的来源包括 编辑:程序博客网 时间:2024/05/16 05:21
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false"> <item android:drawable="@drawable/main_frame_01" android:duration="100" /> <item android:drawable="@drawable/main_frame_02" android:duration="100" /> <item android:drawable="@drawable/main_frame_03" android:duration="100" /> <item android:drawable="@drawable/main_frame_04" android:duration="100" /> <item android:drawable="@drawable/main_frame_05" android:duration="100" /> <item android:drawable="@drawable/main_frame_06" android:duration="100" /> <item android:drawable="@drawable/main_frame_07" android:duration="100" /> <item android:drawable="@drawable/main_frame_08" android:duration="100" /> <item android:drawable="@drawable/main_frame_09" android:duration="100" /> <item android:drawable="@drawable/main_frame_10" android:duration="100" /> <item android:drawable="@drawable/main_frame_11" android:duration="100" /> <item android:drawable="@drawable/main_frame_12" android:duration="100" /> <item android:drawable="@drawable/main_frame_13" android:duration="100" /> <item android:drawable="@drawable/main_frame_14" android:duration="100" /> <item android:drawable="@drawable/main_frame_15" android:duration="100" /> <item android:drawable="@drawable/main_frame_16" android:duration="100" /> <item android:drawable="@drawable/main_frame_17" android:duration="100" /> <item android:drawable="@drawable/main_frame_18" android:duration="100" /> <item android:drawable="@drawable/main_frame_19" android:duration="100" /> </animation-list> 
写一个animation-list          oneshot是否只跑一次    xmls是命名空间
 public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.aaa);          m_RunButton=(Button)this.findViewById(R.id.Button01);          m_StopButton=(Button)this.findViewById(R.id.Button02);          m_RunButton.setOnClickListener( m_BtnRunClickListener);          m_StopButton.setOnClickListener(m_BtnStopClickListener);                   imgView=(ImageView)this.findViewById(R.id.ImageView01);                    imgView.setBackgroundResource(R.drawable.animation);          mAnimation = (AnimationDrawable) imgView.getBackground();         //startAnimation(mAnimation);    }  

onCreate 中 Animation 需要一个 view 所以 imgView.getBackground()返回一个view background 保证切换图片时不会重叠

animation.start() 不能写在onCreate中否则不能执行,可能系统调用view.invalidate()不能在onCreate调用的原因,否则会冲突```onCreate时

invalidate(废止)。把animation.start() 写在button的响应里。

protected void onResume() {          // TODO Auto-generated method stub          super.onResume();          //mHandler.postDelayed(mRunnable, START_DELAY);      }      private View.OnClickListener m_BtnRunClickListener=new View.OnClickListener()      {             public void onClick(View arg0) {              // TODO Auto-generated method stub              startAnimation(mAnimation);          }             };      private View.OnClickListener m_BtnStopClickListener=new View.OnClickListener()      {          public void onClick(View arg0) {              // TODO Auto-generated method stub              stopAnimation(mAnimation);          }                };      protected void startAnimation(final AnimationDrawable animation) {          if (animation != null && !animation.isRunning()) {              animation.run();          }      }           protected void stopAnimation(final AnimationDrawable animation) {          if (animation != null && animation.isRunning()) animation.stop();      }  

这样就可以了`

原创粉丝点击