您的位置:首页 > 移动开发 > Android开发

android动画之:补间动画(Tween动画)

2015-04-20 10:14 225 查看
android中Tween动画实现原理:通过对View的内容进行图形变换 (平移、缩放、旋转、透明度)的改变来实现动画效果。动画效果的定义可用XML来做也可以采用编码来做,今天简单讲下用代码来实现Tween动画中的四种动画方式。四种动画分别对就四个动画类:

渐变透明度动画效果

AlphaAnimation

渐变尺寸缩放动画效果

ScaleAnimation

画面位置移动动画效果

TranslateAnimation

画面旋转动画效果

RotateAnimation

1:平移操作

/**TranslateAnimation(float fromXDelta, float toXDelta,

   float fromYDelta, float toYDelta)

  参数fromXDelta为动画起始时 X坐标上的移动位置

   参数toXDelta为动画结束时 X坐标上的移动位置

   参数fromYDelta为动画起始时Y坐标上的移动位置

   参数toYDelta为动画结束时Y坐标上的移动位置*/

AnimationtranslateAnimation=new TranslateAnimation(0, 100, 0, 0);
/**动画持续时间(单位:毫秒)*、

translateAnimation.setDuration(3000);
/**动画插入器*/

translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);
/**设置动画结束后保持当前的位置*/

translateAnimation.setFillAfter(true);
aminationView.startAnimation(translateAnimation);/**aminationView指要实现动画的View*/
2缩放操作

/**ScaleAnimation(float fromX, float toX, float fromY, float toY,

int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);

   第一个参数fromX为动画起始时 X坐标上的伸缩尺寸 0.0表示收缩到没有

   第二个参数toX为动画结束时 X坐标上的伸缩尺寸 1.0表示正常无伸缩

   第三个参数fromY为动画起始时Y坐标上的伸缩尺寸 值小于1.0表示收缩

   第四个参数toY为动画结束时Y坐标上的伸缩尺寸 值大于1.0表示放大*/

/**定义缩放动画*/
AnimationscaleAnimation=new ScaleAnimation(0.5f, 1.0f,1.0f, 1.0f);
scaleAnimation.setDuration(2000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setRepeatCount(3);
aminationView.startAnimation(scaleAnimation);/**aminationView指要实现动画的View*/

3旋转操作

/**RotateAnimation(float fromDegrees, float toDegrees,

  int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

   第一个参数fromDegrees为动画起始时的旋转角度 此角度是当前为0及360,设置其他值则先跳至该角度的位置再由from - to的值: 负则正向转,正则反向转

   第二个参数toDegrees为动画旋转到的角度

   第三个参数pivotXType为动画在X轴相对于物件位置类型

   第四个参数pivotXValue为动画相对于物件的X坐标的开始位置 此值是以本身原始位置为原点,即如设为20%p,则向右移动父控 件的20%位移,为负数则向左移

   第五个参数pivotXType为动画在Y轴相对于物件位置类型

   第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置 此值是以本身原始位置为原点,即如设为20%p,则向下移动父控 件的20%位移,为负数则向上移*/

AnimationrotateAnimation=new RotateAnimation(0, 45);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
aminationView.startAnimation(rotateAnimation);/**aminationView指要实现动画的View*/

4透明度变化操作

/**AlphaAnimation(float fromAlpha, float toAlpha)

第一个参数fromAlpha为 动画开始时候透明度 0.0表示完全透明

第二个参数toAlpha为 动画结束时候透明度 1.0表示完全不透*/

AnimationalphaAnimation=new AlphaAnimation(1, (float) 0.1);
alphaAnimation.setDuration(3000);//设置动画持续时间为3秒
alphaAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)
aminationView.startAnimation(alphaAnimation);/**aminationView指要实现动画的View*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: