android 左移动画_Android研究院之游戏开发Tween动画的实现(十九)

本文详细介绍了Android开发中的Tween动画,包括Scale、Rotate、Translate和Alpha四种基本动画效果,以及如何通过XML和代码创建动画。Tween动画适用于设置起始点和结束点,中间过程由系统自动完成。此外,文章还提到了如何组合多种动画实现综合效果,并提供了源代码示例。
摘要由CSDN通过智能技术生成

今天和大伙讨论一下Android开发中的Tween动画的实现。首先它和上一章我们讨论的Frame动画同属于系统提供的绘制动画的方法。Tween动画主要的功能是在绘制动画前设置动画绘制的轨迹,包括时间, 位置 ,等等。但是Tween动画的缺点是它只能设置起始点与结束点的两帧,中间过程全部由系统帮我们完成。所以在帧数比较多的游戏开发中是不太会用到它的。

Tween一共提供了4中动画的效果

Scale:缩放动画

Rotate:旋转动画

Translate:移动动画

Alpha::透明渐变动画

Tween与Frame动画类似都需要在res\anim路径下创建动画的 布局文件

补充:最近有盆友提问可不可以不用XML配置动画,希望可以在代码中配置。那MOMO当然要向大家补充了噢~~~

1.Scale缩放动画

标签为缩放节点

android:fromXscale=”1.0″ 表示开始时X轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)

android:toXscale=”0.0″表示结束时X轴缩放比例为0.0(原图大小 *0.0 为缩小到看不见)

android:fromYscale=”1.0″ 表示开始时Y轴缩放比例为 1.0 (原图大小 * 1.0 为原图大小)

android:toYscale=”0.0″表示结束时Y轴缩放比例为0.0(原图大小 *0.0 为缩小的看不到了)

android:pivotX=”50%” X轴缩放的位置为中心点

android:pivotY=”50%” Y轴缩放的位置为中心点

android:duration=”2000″ 动画播放时间 这里是2000毫秒也就是2秒

这个动画布局设置动画从大到小进行缩小。

C#

1

2

3

4

5

6

7

8

9

10

android:fromXScale="1.0"

android:toXScale="0.0"

android:fromYScale="1.0"

android:toYScale="0.0"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000">

在代码中加载动画

C#

1

2

3

4

mLitteAnimation=newScaleAnimation(0.0f,1.0f,0.0f,1.0f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0.5f);

mLitteAnimation.setDuration(2000);

代码如下

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.view.animation.Animation;

importandroid.view.animation.AnimationUtils;

importandroid.widget.Button;

importandroid.widget.ImageView;

publicclassScaleActivityextendsActivity{

/**缩小动画按钮**/

ButtonmButton0=null;

/**放大动画按钮**/

ButtonmButton1=null;

/**显示动画的ImageView**/

ImageViewmImageView=null;

/**缩小动画**/

AnimationmLitteAnimation=null;

/**放大动画**/

AnimationmBigAnimation=null;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.scale);

/**拿到ImageView对象**/

mImageView=(ImageView)findViewById(R.id.imageView);

/**加载缩小与放大动画**/

mLitteAnimation=AnimationUtils.loadAnimation(this,R.anim.scalelitte);

mBigAnimation=AnimationUtils.loadAnimation(this,R.anim.scalebig);

mButton0=(Button)findViewById(R.id.button0);

mButton0.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewarg0){

/**播放缩小动画**/

mImageView.startAnimation(mLitteAnimation);

}

});

mButton1=(Button)findViewById(R.id.button1);

mButton1.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewarg0){

/**播放放大动画**/

mImageView.startAnimation(mBigAnimation);

}

});

}

}

2.Rotate旋转动画

标签为旋转节点

Tween一共为我们提供了3种动画渲染模式。

android:interpolator=”@android:anim/accelerate_interpolator” 设置动画渲染器为加速动画(动画播放中越来越快)

android:interpolator=”@android:anim/decelerate_interpolator” 设置动画渲染器为减速动画(动画播放中越来越慢)

android:interpolator=”@android:anim/accelerate_decelerate_interpolator” 设置动画渲染器为先加速在减速(开始速度最快 逐渐减慢)

如果不写的话 默认为匀速运动

android:fromDegrees=”+360″设置动画开始的角度

android:toDegrees=”0″设置动画结束的角度

这个动画布局设置动画将向左做360度旋转加速运动。

C#

1

2

3

4

5

6

7

8

9

android:interpolator="@android:anim/accelerate_interpolator"

android:fromDegrees="+360"

android:toDegrees="0"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000"

/>

在代码中加载动画

C#

1

2

3

4

mLeftAnimation=newRotateAnimation(360.0f,0.0f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF,0.5f);

mLeftAnimation.setDuration(2000);

代码实现

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.view.animation.Animation;

importandroid.view.animation.AnimationUtils;

importandroid.widget.Button;

importandroid.widget.ImageView;

publicclassRotateActivityextendsActivity{

/**向左旋转动画按钮**/

ButtonmButton0=null;

/**向右旋转动画按钮**/

ButtonmButton1=null;

/**显示动画的ImageView**/

ImageViewmImageView=null;

/**向左旋转动画**/

AnimationmLeftAnimation=null;

/**向右旋转动画**/

AnimationmRightAnimation=null;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.retate);

/**拿到ImageView对象**/

mImageView=(ImageView)findViewById(R.id.imageView);

/**加载向左与向右旋转动画**/

mLeftAnimation=AnimationUtils.loadAnimation(this,R.anim.retateleft);

mRightAnimation=AnimationUtils.loadAnimation(this,R.anim.retateright);

mButton0=(Button)findViewById(R.id.button0);

mButton0.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewarg0){

/**播放向左旋转动画**/

mImageView.startAnimation(mLeftAnimation);

}

});

mButton1=(Button)findViewById(R.id.button1);

mButton1.setOnClickListener(newOnClickListener(){

@Override

publicvoidonClick(Viewarg0){

/**播放向右旋转动画**/

mImageView.startAnimation(mRightAnimation);

}

});

}

}

3.Translate移动动画

标签为移动节点

android:repeatCount=”infinite” 设置动画为循环播放,这里可以写具体的int数值,设置动画播放几次,但是它记录次数是从0开始数的,比如这里设置为2 那么动画从0开始数数0 、1、 2 、实际上是播放了3次。

剩下的几个标签上面已经介绍过了。

这个动画布局设置动画从左到右(0.0),从上到下(320,480)做匀速移动。

C#

1

2

3

4

5

6

7

8

9

android:fromXDelta="0"

android:toXDelta="320"

android:fromYDelta="0"

android:toYDelta="480"

android:duration="2000"

android:repeatCount="infinite"

/>

在代码中加载动画

C#

1

2

mAnimation=newTranslateAnimation(0,320,0,480);

mAnimation.setDuration(2000);

代码实现

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.animation.Animation;

importandroid.view.animation.AnimationUtils;

importandroid.widget.ImageView;

publicclassTranslateActivityextendsActivity{

/**显示动画的ImageView**/

ImageViewmImageView=null;

/**移动动画**/

AnimationmAnimation=null;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.translate);

/**拿到ImageView对象**/

mImageView=(ImageView)findViewById(R.id.imageView);

/**加载移动动画**/

mAnimation=AnimationUtils.loadAnimation(this,R.anim.translate);

/**播放移动动画**/

mImageView.startAnimation(mAnimation);

}

}

4 .Alpha:透明渐变动画

标签为alpha透明度节点

android:fromAlpha=”1.0″ 设置动画起始透明度为1.0 表示完全不透明

android:toAlpha=”0.0″设置动画结束透明度为0.0 表示完全透明

也就是说alpha的取值范围为0.0 – 1.0 之间

这个动画布局设置动画从完全不透明渐变到完全透明。

C#

1

2

3

4

5

6

7

android:fromAlpha="1.0"

android:toAlpha="0.0"

android:repeatCount="infinite"

android:duration="2000">

在代码中加载动画

C#

1

2

mAnimation=newAlphaAnimation(1.0f,0.0f);

mAnimation.setDuration(2000);

代码实现

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.animation.Animation;

importandroid.view.animation.AnimationUtils;

importandroid.widget.ImageView;

publicclassAlphaActivityextendsActivity{

/**显示动画的ImageView**/

ImageViewmImageView=null;

/**透明动画**/

AnimationmAnimation=null;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.translate);

/**拿到ImageView对象**/

mImageView=(ImageView)findViewById(R.id.imageView);

/**加载透明动画**/

mAnimation=AnimationUtils.loadAnimation(this,R.anim.alpha);

/**播放透明动画**/

mImageView.startAnimation(mAnimation);

}

}

5.综合动画

可以将上面介绍的4种动画设置在一起同时进行播放,那么就须要使用标签将所有须要播放的动画放在一起。

这个动画布局设置动画同时播放移动、渐变、旋转。

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

android:interpolator="@android:anim/accelerate_interpolator"

android:fromDegrees="+360"

android:toDegrees="0"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000"

android:repeatCount="infinite"

/>

android:toAlpha="0.0"

android:repeatCount="infinite"

android:duration="2000">

android:fromXDelta="0"

android:toXDelta="320"

android:fromYDelta="0"

android:toYDelta="480"

android:duration="2000"

android:repeatCount="infinite"

/>

代码实现

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.animation.Animation;

importandroid.view.animation.AnimationUtils;

importandroid.widget.ImageView;

publicclassAllActivityextendsActivity{

/**显示动画的ImageView**/

ImageViewmImageView=null;

/**综合动画**/

AnimationmAnimation=null;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.translate);

/**拿到ImageView对象**/

mImageView=(ImageView)findViewById(R.id.imageView);

/**加载综合动画**/

mAnimation=AnimationUtils.loadAnimation(this,R.anim.all);

/**播放综合动画**/

mImageView.startAnimation(mAnimation);

}

}

言归正传,总的来说这章内容还是比较简单的。老规矩每篇文章都会附带源代码,最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习雨松MOMO希望可以和大家一起进步。

最后编辑:2012-08-08作者:雨松MOMO

专注移动互联网,Unity3D游戏开发

捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值