Android游戏开发之Tween动画的实现
Tween一共提供了4中动画的效果
Scale:缩放动画
Rotate:旋转动画
Translate:移动动画
Alpha::透明渐变动画
Tween与Frame动画类似都需要在res\anim路径下创建动画的 布局文件
1.Scale缩放动画
<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秒
这个动画布局设置动画从大到小进行缩小。
- <scale xmlns:android="http://schemas.android.com/apk/res/android"
- ? ?? ?? ?? ?? ? 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">
- </scale>
复制代码
代码如下- android:interpolator="@android:anim/decelerate_interpolator" 设置动画渲染器为减速动画(动画播放中越来越慢)
- ? ?? ???android:interpolator="@android:anim/accelerate_interpolator"
- ? ?? ???android:fromDegrees="+360"
- ? ?? ???android:toDegrees="0"
- ? ?? ???android:pivotX="50%"
- ? ?? ???android:pivotY="50%"
- ? ?? ???android:duration="2000"
- />
复制代码
代码实现- 剩下的几个标签上面已经介绍过了。
- ? ? android:fromXDelta="0"
- ? ? android:toXDelta="320"
- ? ? android:fromYDelta="0"
- ? ? android:toYDelta="480"
- ? ? android:duration="2000"
- ? ?? ???android:repeatCount="infinite"
- />
复制代码
代码实现- android:toAlpha="0.0"设置动画结束透明度为0.0 表示完全透明
- ? ? android:fromAlpha="1.0"
- ? ? android:toAlpha="0.0"
- ? ? android:repeatCount="infinite"
- ? ? android:duration="2000">
- </alpha>
复制代码
代码实现- <set xmlns:android="http://schemas.android.com/apk/res/android">
- ? ? <rotate
- ? ?? ???android:interpolator="@android:anim/accelerate_interpolator"
- ? ?? ???android:fromDegrees="+360"
- ? ?? ???android:toDegrees="0"
- ? ?? ???android:pivotX="50%"
- ? ?? ???android:pivotY="50%"
- ? ?? ???android:duration="2000"
- ? ?? ???android:repeatCount="infinite"
- ? ? />
- ? ? <alpha??android:fromAlpha="1.0"
- ? ? android:toAlpha="0.0"
- ? ? android:repeatCount="infinite"
- ? ? android:duration="2000">
- ? ?? ???</alpha>
- <translate
- ? ? android:fromXDelta="0"
- ? ? android:toXDelta="320"
- ? ? android:fromYDelta="0"
- ? ? android:toYDelta="480"
- ? ? android:duration="2000"
- ? ?? ???android:repeatCount="infinite"
- />
- </set>
复制代码
代码实现- import android.app.Activity;
- import android.os.Bundle;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.ImageView;
- public class AllActivity extends Activity {
- ? ? /**显示动画的ImageView**/
- ? ? ImageView mImageView = null;
- ? ? /**综合动画**/
- ? ? Animation mAnimation = null;
- ? ? @Override
- ? ? public void onCreate(Bundle savedInstanceState) {
- ? ?? ???super.onCreate(savedInstanceState);
- ? ?? ???setContentView(R.layout.translate);
- ? ?? ???/**拿到ImageView对象**/
- ? ?? ???mImageView = (ImageView)findViewById(R.id.imageView);
- ? ?? ???/**加载综合动画**/
- ? ?? ???mAnimation = AnimationUtils.loadAnimation(this, R.anim.all);
- ? ?? ???/**播放综合动画**/
- ? ?? ???mImageView.startAnimation(mAnimation);
- ? ? }
- }
也就是说alpha的取值范围为0.0 - 1.0 之间
这个动画布局设置动画从完全不透明渐变到完全透明。- <alpha??xmlns:android="http://schemas.android.com/apk/res/android"
这个动画布局设置动画从左到右(0.0),从上到下(320,480)做匀速移动。- <translate??xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 设置动画渲染器为先加速在减速(开始速度最快 逐渐减慢)
如果不写的话 默认为匀速运动
android:fromDegrees="+360"设置动画开始的角度
android:toDegrees="0"设置动画结束的角度
这个动画布局设置动画将向左做360度旋转加速运动。- <rotate xmlns:android="http://schemas.android.com/apk/res/android"