1.简介
Animation提供了一系列的动画效果,可以作用于大多数控件上。它有分为两大类:Tweened Animation,Frame-by-Frame Animation。
其中,前者提供旋转,缩放,移动,淡入淡出动画效果;后者可以创建Drawable序列,这些序列可以按照指定时间显示。
2.应用
Animations的使用可以归纳为以下五个步骤:
1.创建AnimationSet对象
AnimationSet(boolean shareInterpolator)
2.创建Animation对象
淡入淡出效果:
AlphaAnimation(float fromAlpha, float toAlpha)
fromAlpha
- Starting alpha value for the animation, where 1.0 means fully opaque and 0.0 means fully transparent.
toAlpha
- Ending alpha value for the animation.
注:区间(0,1)的0表示完全透明,1表示完全不透明
缩放效果:
ScaleAnimation(float fromX, float toX, float fromY, float toY)
注:(fromX=1,fromY=1,toX=2,toY=2) //从原始大小,放大到两倍.以左边为开始点
(fromX=0,fromY=0,toX=2,toY=2) //从零放大两倍
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
注:(pivotX,pivotY)为放大的中心点
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
pivotXType
- Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotXValue
- The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge.
pivotYType
- Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotYValue
- The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge.
旋转效果:
RotateAnimation(float fromDegrees, float toDegrees)
fromDegrees
- Rotation offset to apply at the start of the animation.
toDegrees
- Rotation offset to apply at the end of the animation.
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
fromDegrees
- Rotation offset to apply at the start of the animation.
toDegrees
- Rotation offset to apply at the end of the animation.
pivotX
- The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge.
pivotY
- The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge.
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
pivotXType
- Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotXValue
- The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
pivotYType
- Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotYValue
- The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
平移效果:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
fromXDelta
- Change in X coordinate to apply at the start of the animation
toXDelta
- Change in X coordinate to apply at the end of the animation
fromYDelta
- Change in Y coordinate to apply at the start of the animation
toYDelta
- Change in Y coordinate to apply at the end of the animation
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
fromXType
- Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromXValue
- Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toXType
- Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toXValue
- Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
fromYType
- Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromYValue
- Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toYType
- Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toYValue
- Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
3.为Animation对象设置相应的数据
注:详情见API中Animation的方法摘要
4.将Animation对象放入AnimationSet中
addAnimation(Animation a)
5.使用控件加载动画
startAnimation(Animation a)
3.代码
以下代码以图片的淡入淡出为例:
public class Activity1 extends Activity implements OnClickListener{
//声明对象
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
//将对象与ID匹配
iv=(ImageView) findViewById(R.id.imageView1);
//显示图片
iv.setBackgroundResource(R.drawable.i2);
iv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//创建AnimationSet对象
AnimationSet animationset1 = new AnimationSet(true);
//创建动画对象,此为但如淡出动画。其中,0表示完全透明,1表示完全不透明。
AlphaAnimation alpha = new AlphaAnimation(0, 1);
//设置动画时间
alpha.setDuration(2000);
//将动画放入AnimationSet对象中
animationset1.addAnimation(alpha);
//部件开始动画
iv.startAnimation(animationset1);
}
4.效果