Android游戏开发之Tween动画的达成
发布时间:2021-11-25 16:31:26 所属栏目:教程 来源:互联网
导读:Tween一共提供了4中动画的效果 Scale:缩放动画 Rotate:旋转动画 Translate:移动动画 Alpha::透明渐变动画 Tween与Frame动画类似都需要在resanim路径下创建动画的 布局文件 补充:最近有盆友提问可不可以不用XML配置动画,希望可以在代码中配置。那MOMO
|
Tween一共提供了4中动画的效果 Scale:缩放动画 Rotate:旋转动画 Translate:移动动画 Alpha::透明渐变动画 Tween与Frame动画类似都需要在resanim路径下创建动画的 布局文件 补充:最近有盆友提问可不可以不用XML配置动画,希望可以在代码中配置。那MOMO当然要向大家补充了噢~~~ 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秒 这个动画布局设置动画从大到小进行缩小。 <?xml version="1.0" encoding="utf-8"?> <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> 在代码中加载动画 mLitteAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); mLitteAnimation.setDuration(2000); 代码如下 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class ScaleActivity extends Activity { /**缩小动画按钮**/ Button mButton0 = null; /**放大动画按钮**/ Button mButton1 = null; /**显示动画的ImageView**/ ImageView mImageView = null; /**缩小动画**/ Animation mLitteAnimation = null; /**放大动画**/ Animation mBigAnimation = null; @Override public void onCreate(Bundle savedInstanceState) { 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(new OnClickListener() { @Override public void onClick(View arg0) { /**播放缩小动画**/ mImageView.startAnimation(mLitteAnimation); } }); mButton1 = (Button)findViewById(R.id.button1); mButton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { /**播放放大动画**/ mImageView.startAnimation(mBigAnimation); } }); } } 2.Rotate旋转动画 <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度旋转加速运动。 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromDegrees="+360" android:toDegrees="0" android:pivotX="50%" android:pivotY="50%" android:duration="2000" /> 在代码中加载动画 mLeftAnimation = new RotateAnimation(360.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); mLeftAnimation.setDuration(2000); 代码实现 import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class RotateActivity extends Activity { /**向左旋转动画按钮**/ Button mButton0 = null; /**向右旋转动画按钮**/ Button mButton1 = null; /**显示动画的ImageView**/ ImageView mImageView = null; /**向左旋转动画**/ Animation mLeftAnimation = null; /**向右旋转动画**/ Animation mRightAnimation = null; @Override public void onCreate(Bundle savedInstanceState) { 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(new OnClickListener() { @Override public void onClick(View arg0) { /**播放向左旋转动画**/ mImageView.startAnimation(mLeftAnimation); } }); mButton1 = (Button)findViewById(R.id.button1); mButton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { /**播放向右旋转动画**/ mImageView.startAnimation(mRightAnimation); } }); } } 3.Translate移动动画 <translate>标签为移动节点 android:repeatCount="infinite" 设置动画为循环播放,这里可以写具体的int数值,设置动画播放几次,但是它记录次数是从0开始数的,比如这里设置为2 那么动画从0开始数数0 、1、 2 、实际上是播放了3次。 ![]() (编辑:开发网_郴州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |



浙公网安备 33038102330466号