【转】animation-list旋转的地球以及Timer的使用
2012-11-15 15:15
387 查看
转自:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-3708.html
用到animation-list其实就是帧叠加 ,通常要注意两个问题
1.一般布局要用在frameLayout
2.它呢一般作为背景 然后在取出来
public class EarthAnimationActivity extends Activity { private static final String TAG = "EarthAnimationActivity"; protected static final int FORWARD = 0; protected static final int REVERSE = 1; private Button earthButton; private AnimationDrawable earthButtonAnimation; protected int direction; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); earthButton = (Button) findViewById(R.id.earth_button); earthButtonAnimation = (AnimationDrawable) earthButton.getBackground(); direction = FORWARD; earthButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if ( ! earthButtonAnimation.isRunning() ) { earthButtonAnimation.start(); earthButton.setText(R.string.click_me_to_stop); } else { earthButtonAnimation.stop(); int resId = R.anim.earth_animation_rev; if ( direction == FORWARD ) { direction = REVERSE; } else { resId = R.anim.earth_animation; direction = FORWARD; } earthButton.setBackgroundResource(resId); earthButtonAnimation = (AnimationDrawable) earthButton.getBackground(); earthButton.setText(R.string.click_me_to_start); } } }); } /* (non-Javadoc) * @see Android.app.Activity#onResume() */ @Override protected void onResume() { super.onResume(); (new Timer(false)).schedule(new AnimationTimer(earthButtonAnimation), 100); } /* (non-Javadoc) * @see android.app.Activity#onPause() */ @Override protected void onPause() { super.onPause(); earthButtonAnimation.stop(); } private static class AnimationTimer extends TimerTask { AnimationDrawable animation; public AnimationTimer(AnimationDrawable animation) { this.animation = animation; } @Override public void run() { animation.start(); this.cancel(); } } }
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frame_layout" android:background="@drawable/space_background"> <Button android:id="@+id/earth_button" android:background="@anim/earth_animation" android:text="@string/click_me_to_stop" android:layout_gravity="center" android:textStyle="bold" android:textColor="#ff9900" android:layout_marginBottom="12dip" android:layout_marginRight="12dip" android:layout_marginTop="12dip" android:maxHeight="296dip" android:maxWidth="296dip" android:layout_height="wrap_content" android:layout_width="wrap_content" android:width="296dip" android:height="296dip" android:layout_marginLeft="-10dip" android:textSize="24dip"></Button> </FrameLayout>
上面主vxml
下面是地球的正反旋转
<?xml version="1.0" encoding="UTF-8"?> <!-- Animation frames for earth --> <!-- android:id="@+id/earth_animation" --> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" android:visible="true"> <item android:drawable="@drawable/earth0" android:duration="150" /> <item android:drawable="@drawable/earth1" android:duration="150" /> <item android:drawable="@drawable/earth2" android:duration="150" /> <item android:drawable="@drawable/earth3" android:duration="150" /> <item android:drawable="@drawable/earth4" android:duration="150" /> <item android:drawable="@drawable/earth5" android:duration="150" /> <item android:drawable="@drawable/earth6" android:duration="150" /> <item android:drawable="@drawable/earth7" android:duration="150" /> <item android:drawable="@drawable/earth8" android:duration="150" /> <item android:drawable="@drawable/earth9" android:duration="150" /> </animation-list>
<?xml version="1.0" encoding="UTF-8"?> <!-- Animation frames for earth --> <!-- android:id="@+id/earth_animation_rev" --> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" android:visible="true"> <item android:drawable="@drawable/earth9" android:duration="150" /> <item android:drawable="@drawable/earth8" android:duration="150" /> <item android:drawable="@drawable/earth7" android:duration="150" /> <item android:drawable="@drawable/earth6" android:duration="150" /> <item android:drawable="@drawable/earth5" android:duration="150" /> <item android:drawable="@drawable/earth4" android:duration="150" /> <item android:drawable="@drawable/earth3" android:duration="150" /> <item android:drawable="@drawable/earth2" android:duration="150" /> <item android:drawable="@drawable/earth1" android:duration="150" /> <item android:drawable="@drawable/earth0" android:duration="150" /> </animation-list>
相关文章推荐
- 使用Animation-list实现等待旋转圆圈动画
- 使用Animation-list实现等待旋转圆圈动画
- 集合框架(六)探究如何使用以及何时使用ArrayList或LinkedList来存储元素
- 多线程学习-ListenableFuture使用介绍以及示例
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android提高篇之自定义dialog实现processDialog“正在加载”效果、使用Animation实现图片旋转
- 安卓自动化测试工具MonkeyRunner之使用ID进行参数化,以及List选择某项和弹出框点击确定的写法
- Android中View绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- 使用Animation-list实现等待加载动画效果
- Spring学习笔记 关于Bean属性的初始化 - 使用inner bean以及List, Map与Set的初始化
- 数组、List、ArrayList的使用以及for、foreach效率比较
- 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- python:在for遍历list时使用remove出现的问题以及解析
- android 视图动画的使用,Animation重写 实现旋转效果
- vector,list以及迭代器的使用与注意
- 黑马程序员_List集合类基本操作以及ListIterator的使用方法
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- MS Windows Timer的限制以及如何使用更高分辨率的计时手段。
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解