animation-list旋转的地球以及Timer的使用

用到animation-list其实就是帧叠加 ,通常要注意两个问题

1.一般布局要用在frameLayout

2.它呢一般作为背景 然后在取出来

  1. public class EarthAnimationActivity extends Activity {  
  2.   
  3.     private static final String TAG = "EarthAnimationActivity";  
  4.   
  5.     protected static final int FORWARD = 0;  
  6.     protected static final int REVERSE = 1;  
  7.   
  8.     private Button earthButton;  
  9.   
  10.     private AnimationDrawable earthButtonAnimation;  
  11.   
  12.     protected int direction;  
  13.   
  14.       
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  20.         setContentView(R.layout.main);  
  21.   
  22.         earthButton = (Button) findViewById(R.id.earth_button);  
  23.         earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();  
  24.         direction = FORWARD;  
  25.           
  26.         earthButton.setOnClickListener(new OnClickListener() {  
  27.   
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 if ( ! earthButtonAnimation.isRunning() ) {  
  31.                     earthButtonAnimation.start();  
  32.                     earthButton.setText(R.string.click_me_to_stop);  
  33.                 }  
  34.                 else {  
  35.                     earthButtonAnimation.stop();  
  36.                     int resId = R.anim.earth_animation_rev;  
  37.                     if ( direction == FORWARD ) {  
  38.                         direction = REVERSE;  
  39.                     }  
  40.                     else {  
  41.                         resId = R.anim.earth_animation;  
  42.                         direction = FORWARD;  
  43.                     }  
  44.                     earthButton.setBackgroundResource(resId);  
  45.                     earthButtonAnimation = (AnimationDrawable) earthButton.getBackground();  
  46.                     earthButton.setText(R.string.click_me_to_start);  
  47.                 }  
  48.             }  
  49.               
  50.         });  
  51.     }  
  52.   
  53.     /* (non-Javadoc) 
  54.      * @see android.app.Activity#onResume() 
  55.      */  
  56.     @Override  
  57.     protected void onResume() {  
  58.         super.onResume();  
  59.   
  60.         (new Timer(false)).schedule(new AnimationTimer(earthButtonAnimation), 100);  
  61.     }  
  62.   
  63.     /* (non-Javadoc) 
  64.      * @see android.app.Activity#onPause() 
  65.      */  
  66.     @Override  
  67.     protected void onPause() {  
  68.         super.onPause();  
  69.         earthButtonAnimation.stop();  
  70.     }  
  71.   
  72.     private static class AnimationTimer extends TimerTask {  
  73.         AnimationDrawable animation;  
  74.           
  75.         public AnimationTimer(AnimationDrawable animation) {  
  76.             this.animation = animation;  
  77.         }  
  78.   
  79.         @Override  
  80.         public void run() {  
  81.             animation.start();  
  82.             this.cancel();  
  83.         }  
  84.           
  85.     }  
  86.       
  87. }  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:id="@+id/frame_layout"  
  5.     android:background="@drawable/space_background">  
  6.   
  7.     <Button android:id="@+id/earth_button" android:background="@anim/earth_animation"  
  8.         android:text="@string/click_me_to_stop" android:layout_gravity="center"  
  9.         android:textStyle="bold" android:textColor="#ff9900"  
  10.         android:layout_marginBottom="12dip" android:layout_marginRight="12dip"  
  11.         android:layout_marginTop="12dip" android:maxHeight="296dip"  
  12.         android:maxWidth="296dip" android:layout_height="wrap_content"  
  13.         android:layout_width="wrap_content" android:width="296dip"  
  14.         android:height="296dip" android:layout_marginLeft="-10dip"  
  15.         android:textSize="24dip"></Button>  
  16.   
  17. </FrameLayout>  

 上面主vxml

下面是地球的正反旋转

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <!-- Animation frames for earth -->  
  3.     <!--  android:id="@+id/earth_animation" -->  
  4. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:oneshot="false" android:visible="true">  
  6.     <item android:drawable="@drawable/earth0" android:duration="150" />  
  7.     <item android:drawable="@drawable/earth1" android:duration="150" />  
  8.     <item android:drawable="@drawable/earth2" android:duration="150" />  
  9.     <item android:drawable="@drawable/earth3" android:duration="150" />  
  10.     <item android:drawable="@drawable/earth4" android:duration="150" />  
  11.     <item android:drawable="@drawable/earth5" android:duration="150" />  
  12.     <item android:drawable="@drawable/earth6" android:duration="150" />  
  13.     <item android:drawable="@drawable/earth7" android:duration="150" />  
  14.     <item android:drawable="@drawable/earth8" android:duration="150" />  
  15.     <item android:drawable="@drawable/earth9" android:duration="150" />  
  16. </animation-list>  

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.     <!-- Animation frames for earth -->  
  3.     <!--  android:id="@+id/earth_animation_rev" -->  
  4. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:oneshot="false" android:visible="true">  
  6.     <item android:drawable="@drawable/earth9" android:duration="150" />  
  7.     <item android:drawable="@drawable/earth8" android:duration="150" />  
  8.     <item android:drawable="@drawable/earth7" android:duration="150" />  
  9.     <item android:drawable="@drawable/earth6" android:duration="150" />  
  10.     <item android:drawable="@drawable/earth5" android:duration="150" />  
  11.     <item android:drawable="@drawable/earth4" android:duration="150" />  
  12.     <item android:drawable="@drawable/earth3" android:duration="150" />  
  13.     <item android:drawable="@drawable/earth2" android:duration="150" />  
  14.     <item android:drawable="@drawable/earth1" android:duration="150" />  
  15.     <item android:drawable="@drawable/earth0" android:duration="150" />  
  16. </animation-list>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 定时器 timer_list 是一个内核数据结构,用于管理内核中的定时器。它是一个双向链表,每个节点表示一个定时器。timer_list 的定义位于 `<linux/timer.h>` 头文件中。 每个 timer_list 节点的定义如下: ```c struct timer_list { struct list_head entry; // 定时器节点的链表指针 unsigned long expires; // 定时器的到期时间 void (*function)(unsigned long); // 定时器回调函数 unsigned long data; // 传递给回调函数的参数 struct tvec_base *base; // 定时器所属的时间轮 int slack; // 定时器的松弛时间 }; ``` 其中,`entry` 是一个 `list_head` 结构,用于将节点连接到定时器链表中。`expires` 表示定时器的到期时间,以 jiffies 单位表示。`function` 是定时器的回调函数,在定时器到期时被调用。`data` 是传递给回调函数的参数。`base` 表示定时器所属的时间轮,`slack` 是定时器的松弛时间,用于处理定时器的精度。 在使用 timer_list 时,可以使用以下函数进行初始化和操作: - `timer_setup(struct timer_list *timer, void (*function)(unsigned long), unsigned int flags)`:初始化一个定时器,并指定回调函数和标志。 - `init_timer(struct timer_list *timer)`:初始化一个定时器。 - `add_timer(struct timer_list *timer)`:将定时器添加到定时器链表中。 - `del_timer(struct timer_list *timer)`:从定时器链表中删除定时器。 - `mod_timer(struct timer_list *timer, unsigned long expires)`:修改定时器的到期时间。 这些函数可以通过 `<linux/timer.h>` 头文件中的宏来调用。通过操作 timer_list,可以实现在 Linux 内核中的定时器功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值