android动画效果有两种方式实现。一是在xml里设置,二是在java中设置。mars说在xml里能体现出面向对象的有点,能消除重复代码。我觉得在java里也能做到啊。有错轻喷。把animation封装起来不适也能体现面向对象的设计模式么??废话不多说了,帖上封装好的animation代码,都是最基础的,复杂点的话自己可以加上不同的参数。
//Animation动态生成
class MyAnimation {
//Controller用于给ViewGroup加载动画
LayoutAnimationController getController(AnimationSet set) {
LayoutAnimationController lac = new LayoutAnimationController(set);
//设置动画执行的顺序,这里是当child加载出来时就显示动画
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
//设置延迟,也就是间隔时间
lac.setDelay(0.2f);
return lac;
}
//Set主要用于加载多个动画
AnimationSet getSet(int wait, Animation... animations) {
AnimationSet set = new AnimationSet(true);
//将animation加载到set里
for (Animation anima : animations) {
set.addAnimation(anima);
}
//开始等待时间
set.setStartOffset(wait);
//持续时间
set.setDuration(300);
//动画结束时,View将停留在动画结束的位置
set.setFillAfter(true);
//设置动画运动曲线,这里是减速
set.setInterpolator(new DecelerateInterpolator());
return set;
}
//渐变
AlphaAnimation getAlpha() {
return new AlphaAnimation(0, 1);
}
//移动
TranslateAnimation getTrans() {
return new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_SELF, 0);
}
//缩放
ScaleAnimation getScale() {
return new ScaleAnimation(0, 1, 0, 1);
}
//旋转
RotateAnimation getRotate() {
return new RotateAnimation(90, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
}
}
只想要一种效果的话,可以直接View.setAnimation。想要获得多种效果的话需要获得一个set对象,将想要的效果装载到set里,再setAinmation(set)。如果想要给一个ViewGroup添加动画的话,需要LayoutAnimationController对象,来装载set,再ViewGroup.setLayoutAnimation(LayoutAnimationController),注意这里的方法不一样哦。ViewGroup也可以是ListView和GridView等具有多个view的控件。我写小程序的时候发现,让ListVIew加载动画只会在Activity显示的时候显示出来,而在baseAdapter里的getView里个控件加载动画时只会在下拉的时候显示出来,于是我就把两个地方全加上了。。。