你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中的动画详解系列

飞舞的蝴蝶

[日期:2014-08-03] 来源:Linux社区  作者:dawanganban [字体:大 中 小]

这一篇来使用逐帧动画和补间动画来实现一个小例子,首先我们来看看Android中的补间动画。

Android中使用Animation代表抽象的动画类,该类包括下面几个子类:

AlphaAnimation:透明改变动画。

ScaleAnimation:大小缩放动画。

TranslateAnimation:位移变化动画。

RotateAnimation:旋转动画。

我们下面使用位移动画和逐帧动画来实现这个小例子,先看看运行效果:

蝴蝶挥动翅膀的逐帧动画文件:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!-- 定义动画循环播放 -->
  3. <animation-listxmlns:android="http://schemas.android.com/apk/res/android"
  4. android:oneshot="false">
  5. <itemandroid:drawable="@drawable/butterfly_f01"android:duration="120"/>
  6. <itemandroid:drawable="@drawable/butterfly_f02"android:duration="120"/>
  7. <itemandroid:drawable="@drawable/butterfly_f03"android:duration="120"/>
  8. <itemandroid:drawable="@drawable/butterfly_f04"android:duration="120"/>
  9. <itemandroid:drawable="@drawable/butterfly_f05"android:duration="120"/>
  10. <itemandroid:drawable="@drawable/butterfly_f06"android:duration="120"/>
  11. </animation-list>

界面布局文件:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. android:background="@drawable/background"
  7. >
  8. <ImageView
  9. android:id="@+id/butterfly"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:background="@anim/butteryfly"
  13. />
  14. </LinearLayout>

具体逻辑及位移动画:

 

  1. package com.example.butteryfly;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import android.app.Activity;
  5. import android.graphics.drawable.AnimationDrawable;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.view.Display;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.ImageView;
  13. publicclass MainActivity extends Activity {
  14. privatefloat curX = 0;
  15. privatefloat curY = 30;
  16. privatefloat nextX = 0;
  17. privatefloat nextY = 0;
  18. privateint windowW = 0;
  19. privateint windowH = 0;
  20. private ImageView imageView;
  21. Handler handler = new Handler(){
  22. publicvoid handleMessage(android.os.Message msg) {
  23. if(msg.what == 0x123){
  24. if(nextX > windowW || nextY > windowH){
  25. curX = nextX = 0;
  26. }else{
  27. nextX += 8;
  28. }
  29. nextY = curY + (float) (Math.random() * 20 - 10);
  30. TranslateAnimation anim = new TranslateAnimation(curX, nextX, curY, nextY);
  31. curX = nextX;
  32. curY = nextY;
  33. anim.setDuration(200);
  34. imageView.startAnimation(anim);
  35. }
  36. };
  37. };
  38. @Override
  39. protectedvoid onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. imageView = (ImageView) findViewById(R.id.butterfly);
  43. Display display = getWindowManager().getDefaultDisplay();
  44. windowW = display.getWidth();
  45. windowH = display.getHeight();
  46. final AnimationDrawable butterfly = (AnimationDrawable) imageView.getBackground();
  47. imageView.setOnClickListener(new OnClickListener() {
  48. @Override
  49. publicvoid onClick(View arg0) {
  50. butterfly.start();
  51. new Timer().schedule(new TimerTask() {
  52. @Override
  53. publicvoid run() {
  54. handler.sendEmptyMessage(0x123);
  55. }
  56. }, 0, 200);
  57. }
  58. });
  59. }
  60. }

更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2014-08/105023p3.htm

linux
【内容导航】
第1页:逐帧动画 第2页:飞舞的蝴蝶
第3页:自定义动画研究 第4页:Activity之间切换动画
相关资讯       Android动画详解 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款