定义逐帧动画非常简单,只要在元素中使用子元素定义动画的全部帧,并指定各帧的持续时间即可。 android:onshot控制该动画是否循环播放,设为false则会循环播放 也支持在Java代码中创建逐帧动画,调用addFrame(Drawable frame, int duration)向该动画中添加帧 程序获取AnimationDrawable后,接下来就可用ImageView把AnimationDrawable显示出来 示例代码: res/anim/fat_po.xml <? xml version = "1.0" encoding = "utf-8" ?>

< animation-list xmlns:android = "http://schemas.android.com/apk/res/android"
android:oneshot = "false" >

< item android:drawable = "@drawable/fat_po_f01" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f02" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f03" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f04" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f05" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f06" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f07" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f08" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f09" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f10" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f11" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f12" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f13" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f14" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f15" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f16" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f17" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f18" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f19" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f20" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f21" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f22" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f23" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f24" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f25" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f26" android:duration = "60" />
< item android:drawable = "@drawable/fat_po_f27" android:duration = "60" /> animation-list > main_activity.xml < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   
xmlns:tools = "http://schemas.android.com/tools"
   
android:layout_width = "match_parent"
   
android:layout_height = "match_parent"
   
android:orientation = "vertical" >
   
< LinearLayout
       
android:layout_width = "match_parent"
       
android:layout_height = "wrap_content"
       
android:orientation = "horizontal"
       
android:gravity = "center" >
       
< Button android:id = "@+id/play"
           
android:layout_width = "wrap_content"
           
android:layout_height = "wrap_content"
            android:text = "start"
           
/>
       
< Button android:id = "@+id/stop"
           
android:layout_width = "wrap_content"
           
android:layout_height = "wrap_content"
           
android:text = "stop"
           
/>
       
   
LinearLayout >

   
< ImageView
       
android:id = "@+id/anim"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "@anim/fat_po"
android:scaleType = "center"
android:layout_gravity = "center_horizontal"
/>
       
   
LinearLayout > main.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout.
activity_main );
// 获取两个按钮
Button play = (Button) findViewById(R.id.
play );
Button stop = (Button) findViewById(R.id.
stop );
ImageView imageView = (ImageView) findViewById(R.id.
anim );
// 获取AnimationDrawable动画对象
final AnimationDrawable anim = (AnimationDrawable) imageView
.getBackground();
play.setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View v)
{
// 开始播放动画
anim.start();
}
});
stop.setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View v)
{
// 停止播放动画
anim.stop();
}
});
}
}

更多相关文章

  1. [Android]自定义组件示例:使用attrs.xml文件定制RadioButton
  2. Android(安卓)ProgressBar 自定义样式(一)
  3. 浅谈Android辅助功能 AccessibilityService
  4. Android(安卓)动画之帧动画
  5. Android(安卓)自定义Toast显示多种方式
  6. Android(安卓)Building System 总结
  7. GitHub Android(安卓)Librarys Top 100 简介
  8. 编程回忆之Android回忆(Android标题栏的去除和全屏)
  9. Android(安卓)自定义时间选择器

随机推荐

  1. Android开发错误Unable to execute dex:
  2. Android/OPhone开发完全讲义
  3. Android 自动化测试—robotium(八)拖拽
  4. Android API 28 访问服务器失败 提示CLEA
  5. android 串口通信选择
  6. 制作ota升级包之error:Could not create t
  7. EditText 属性
  8. Android 面试之 Android 篇一
  9. Android 5.1修改底部导航栏NavigationBar
  10. android 中文 api (64) —— Scroller