android学习View Animation

在android SDK中介绍了3种animation分别是View Animation 、Drawable Animation 、Property Animation。
View Animation提供了旋转、缩放、移动、透明度变化这四种简单的功能。
Drawable Animation则是所谓的帧动画,按时间轴展示你事先准备的画面。(个人感觉很少用)

Property Animation是Android 3.0之后引入的,比起前两者来拥有更加强大的功能。


本博客介绍View Animation的基本使用。

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)
设置动画重复执行的次数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值