http://xiangai.taobao.com
http://shop148612228.taobao.com

android Frame-By-Frame Animations(一帧一帧地播放动画)的使用

程序功能,点击按钮时,图片一张一张循环播放:

直接来代码:

AppMain.java

[java] view plaincopy

  1. package lxy.litsoft; 
  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 AppMain extends Activity { 
  10. //声明对象
  11.     ImageView disPic; 
  12.     Button btDis; 
  13. public void onCreate(Bundle savedInstanceState) { 
  14. super.onCreate(savedInstanceState); 
  15.         setContentView(R.layout.main); 
  16. //实例化对象
  17.         disPic = (ImageView)findViewById(R.id.imageView01); 
  18.         btDis = (Button)findViewById(R.id.button01); 
  19. //绑定监听器
  20.         btDis.setOnClickListener(new ButtonListener()); 
  21.     } 
  22. //按钮的监听器实现
  23. class ButtonListener implements OnClickListener{ 
  24. public void onClick(View v) { 
  25. //动画播放
  26.             disPic.setBackgroundResource(R.drawable.anim); 
  27.             AnimationDrawable animationDrawable = (AnimationDrawable)disPic.getBackground(); 
  28.             animationDrawable.start(); 
  29.         } 
  30.     } 

main.xml

[html] view plaincopy

  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. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/hello"/>
  10. <ImageView
  11. android:id="@+id/imageView01"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"></ImageView>
  14. <Button
  15. android:id="@+id/button01"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:text="Anim"></Button>
  19. </LinearLayout>

res/drawable/anim.xml

[html] view plaincopy

  1. <animation-list
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:oneshot="false">
  4. <item android:drawable="@drawable/p30"
  5. android:duration="300"/>
  6. <item android:drawable="@drawable/p31"
  7. android:duration="300"/>
  8. <item android:drawable="@drawable/p32"
  9. android:duration="300"/>
  10. <item android:drawable="@drawable/p33"
  11. android:duration="300"/>
  12. <item android:drawable="@drawable/p34"
  13. android:duration="300"/>
  14. <item android:drawable="@drawable/p35"
  15. android:duration="300"/>
  16. <item android:drawable="@drawable/p36"
  17. android:duration="300"/>
  18. <item android:drawable="@drawable/p37"
  19. android:duration="300"/>
  20. </animation-list>

其中每一个item就是一帧的资源,android:drawable是图片资源,android:duration是每一帧的显示时间。

posted @ 2013-06-04 22:37  万事俱备就差个程序员  阅读(217)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏