在android SDK中介绍了3种animation分别是View Animation 、Drawable Animation 、Property Animation。
View Animation提供了旋转、缩放、移动、透明度变化这四种简单的功能。
Drawable Animation则是所谓的帧动画,按时间轴展示你事先准备的画面。(个人感觉很少用)
setFillAfter(Boolean fillAfter)
如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
setStartOffSet(long startOffSet)
设置动画执行之前的等待时间
setRepeatCount(int repeatCount)
设置动画重复执行的次数
View Animation提供了旋转、缩放、移动、透明度变化这四种简单的功能。
Drawable Animation则是所谓的帧动画,按时间轴展示你事先准备的画面。(个人感觉很少用)
Property Animation是Android 3.0之后引入的,比起前两者来拥有更加强大的功能。
1、旋转效果
//构造函数传入true表示使用默认的interpolator
AnimationSet animationset = new AnimationSet(true);
//构造函数有4种,当前示例随意选择一种,可查阅文档使用其它构造方式
RotateAnimation rotateanimation = new RotateAnimation(
0, //起始角度
360, //旋转角度(正负分别表示顺逆时针)
Animation.RELATIVE_TO_SELF,1.0f, //表示锚点x坐标为自身x轴的大小
Animation.RELATIVE_TO_SELF,1.0f //表示锚点y坐标为自身y轴的大小
);//当前指定锚点效果即围绕图像的右下点旋转
rotateanimation.setDuration(1000); //设置动画的执行时间,单位毫秒
animationSet.addAnimation(rotateAnimation); //添加动画到AnimationSet
image.startAnimation(animationSet1); //开始动画,image为ImageView类型,引用一张图片
2、透明度变化效果
Animationset animationset = new AnimationSet(true);
//参数表示从完全透明到完全不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
3、移动效果
AnimationSet animationSet = new AnimationSet(true);
//构造函数有3种,当前构造函数指定移动的起点与终点坐标
TranslateAnimation translateAnimation =new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f, //表示x轴起点坐标为0
Animation.RELATIVE_TO_SELF,0f, //表示x轴终点坐标为0
Animation.RELATIVE_TO_SELF,0f, //表示y轴起点坐标为0
Animation.RELATIVE_TO_SELF,1.0f //表示y轴终点坐标为自身长度
); //当前效果表示图形向下移动自身y轴长度
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
4、缩放效果
<span style="font-size:18px;">AnimationSet animationSet = new AnimationSet(true);
//构造函数有4种,当前构造函数指定变化长宽与锚点
ScaleAnimation scaleAnimation = new ScaleAnimation(
0, //图形在x轴的初始长度(与自身长度之比)
1.0f, //图形在x轴变化后的长度(与自身长度之比)
0, //图形在y轴的初始长度(与自身长度之比)
1.0f, //图形在y轴变化后的长度(与自身长度之比)
//以下四个参数指定缩放时的锚点,不理解锚点者可将第6、8个参数改为1.0f运行即可明白
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f
);//当前效果图形以中心为锚点从看不见逐渐增大至原本大小
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);</span>
以上执行效果如下:
setFillAfter(Boolean fillAfter)
如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
setStartOffSet(long startOffSet)
设置动画执行之前的等待时间
setRepeatCount(int repeatCount)
设置动画重复执行的次数