View Animation是android 刚开始推出的两种动画中的一种,又叫做tween animation(补间动画),我们在Eclipse中点击
New Android XML File按钮时,选择Resource Type会看见下拉框中有一个Tween Aniation的类型,这个就是View Animation
如果我们想使用xml来定义View Animation,在Root Element中的选择框中我们会看见有五种类型的element,他们分别是:
1、alpha:透明度,通过设置这个element的属性我们能够设计透明度渐变的动画。
2、rotate:旋转,设置这个element的属性我们能够设计围绕某个点旋转的动画,这个选择是平面旋转,不是立体旋转。
3、scale:缩放,设置这个element的属性我们能够设计缩小放大的动画。
4、set:这个是个综合的lelement,也就是可以将多个其他的element放到这个中,这样就能产生具有多种效果的动画。
5、translate:平移,通过设置这个element的属性,我们能够设计出控件移动的动画的效果。
通过上面介绍,我们就知道view Animation其实就只有四种动画属性,透明度,旋转,缩放,平移。然后通过这四种属性的
混合来设计新的动画。这四种动画能够通过xml文件来进行设置,也能够通过代码来进行设置。
一、通过xml文件来进行设置
首先我们在xml文件中定义一个translate动画
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="100"
android:duration="2000"
android:repeatCount="1"
android:fillBefore="true">
</translate>
然后在代码中使用这种动画,比如对一个按钮实现平移动画,点击按钮就产生这个动画
translation = (Button)findViewById(R.id.translation);
translation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(ViewAnimation.this, R.anim.tween5);
translation.startAnimation(hyperspaceJumpAnimation);
}
});
先使用AnimationUtiles的loadAnimation()加载xml文件,然后将这个Animation对象通过View控件的startAnimation()方法启动。
二、Java代码实现
translation = (Button)findViewById(R.id.translation);
translation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation hyperspaceJumpAnimation = new TranslateAnimation(0, 100, 0, 0);
hyperspaceJumpAnimation.setDuration(2000);
hyperspaceJumpAnimation.setRepeatCount(1);
hyperspaceJumpAnimation.setRepeatMode(Animation.REVERSE);
translation.startAnimation(hyperspaceJumpAnimation);
}
});
Java代码实现和XML实现方式没什么区别,其实就是将xml中的属性通过代码来进行设置。
下面就将五种方式的实现代码全部贴出来
/**
* View Animation*/
public class ViewAnimation extends Activity implements OnClickListener{
Button translation,scale,rotation,alpha,set,setanimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_animation);
translation = (Button)findViewById(R.id.translation);
scale = (Button)findViewById(R.id.scale);
rotation = (Button)findViewById(R.id.rotation);
alpha = (Button)findViewById(R.id.aplha);
set = (Button)findViewById(R.id.set);
setanimation = (Button)findViewById(R.id.setanimation);
translation.setOnClickListener(this);
scale.setOnClickListener(this);
rotation.setOnClickListener(this);
alpha.setOnClickListener(this);
set.setOnClickListener(this);
setanimation.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//xml文件实现
/*switch(v.getId()){
case R.id.translation:
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.tween5);
translation.startAnimation(hyperspaceJumpAnimation);
break;
case R.id.scale:
Animation scale1 = AnimationUtils.loadAnimation(this, R.anim.tween3);
scale.startAnimation(scale1);
break;
case R.id.rotation:
Animation rotation1 = AnimationUtils.loadAnimation(this, R.anim.tween2);
rotation.startAnimation(rotation1);
break;
case R.id.aplha:
Animation aplpa1 = AnimationUtils.loadAnimation(this, R.anim.tween1);
alpha.startAnimation(aplpa1);
break;
case R.id.set:
Animation set1 = AnimationUtils.loadAnimation(this, R.anim.tween4);
set.startAnimation(set1);
break;
case R.id.setanimation:
Intent intent = new Intent(this, PropertyAnimation.class);
startActivity(intent);
break;
default:
break;
}*/
//java代码实现
switch(v.getId()){
case R.id.translation:
Animation hyperspaceJumpAnimation = new TranslateAnimation(0, 100, 0, 0);
hyperspaceJumpAnimation.setDuration(2000);
hyperspaceJumpAnimation.setRepeatCount(1);
hyperspaceJumpAnimation.setRepeatMode(Animation.REVERSE);
translation.startAnimation(hyperspaceJumpAnimation);
break;
case R.id.scale:
Animation scale1 = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, 50, 50);
scale1.setDuration(2000);
scale1.setRepeatCount(1);
scale1.setRepeatMode(Animation.REVERSE);
scale.startAnimation(scale1);
break;
case R.id.rotation:
Animation rotation1 = new RotateAnimation(0, 270, 50, 50);
rotation1.setDuration(2000);
rotation1.setRepeatCount(1);
rotation1.setRepeatMode(Animation.REVERSE);
rotation.startAnimation(rotation1);
break;
case R.id.aplha:
Animation aplpa1 = new AlphaAnimation(0.0f, 1.0f);
aplpa1.setDuration(2000);
aplpa1.setRepeatCount(1);
aplpa1.setRepeatMode(Animation.REVERSE);
alpha.startAnimation(aplpa1);
break;
case R.id.set:
AnimationSet set1 = new AnimationSet(false);
Animation scale = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, 50, 50);
scale.setDuration(2000);
scale.setRepeatCount(1);
scale.setRepeatMode(Animation.REVERSE);
Animation translator = new TranslateAnimation(0, 100, 0, 0);
translator.setDuration(2000);
translator.setRepeatCount(1);
translator.setRepeatMode(Animation.REVERSE);
set1.addAnimation(scale);
set1.addAnimation(translator);
set.startAnimation(set1);
break;
case R.id.setanimation:
Intent intent = new Intent(this, PropertyAnimation.class);
startActivity(intent);
break;
default:
break;
}
}
}
顺便将set的xml文件贴出来,源代码就不上传了。
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillBefore="true">
<rotate
android:fromDegrees="0"
android:toDegrees="270"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000">
</rotate>
<translate
android:fromXDelta="0"
android:toXDelta="100"
android:duration="2000"
android:repeatCount="1">
</translate>
</set>
View Animation挺简单的,我们使用的最多的还是Propety Animation。View Animation的API接口主要在android.view.animation
这个包中,至于View Animation和Propety Animation的区别等到下篇介绍Propety Animation时再进行介绍。