android 动画代码实现

1、效果图跟上一遍博客一样,请参考Android动画效果Animation之Tween实现简单动画

接下来直接java代码实现如何实现动画:

定义

[java]  view plain copy
  1. private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;  
  2.     private AnimationSet animationSet;  
分别是透明度动画、旋转动画、尺寸伸缩动画、移动动画

 

具体实现代码如下:

 

[java]  view plain copy
  1. private void initAnimation()  
  2.         //透明度控制动画效果 alpha  
  3.         animation_alpha=new AlphaAnimation(0.1f,1.0f);  
  4.         //第一个参数fromAlpha为 动画开始时候透明度  
  5.         //第二个参数toAlpha为 动画结束时候透明度  
  6.         animation_alpha.setRepeatCount(-1);//设置循环  
  7.         animation_alpha.setDuration(5000);//设置时间持续时间为 5000毫秒  
  8.           
  9.         // 旋转效果rotate  
  10.         animation_rotate new RotateAnimation(0-720 
  11.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f,  
  12.                 RotateAnimation.RELATIVE_TO_SELF, 0.5f);  
  13.           //第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度  
  14.           //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置  
  15.           //第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置  
  16.         animation_rotate.setRepeatCount(-1);  
  17.         animation_rotate.setDuration(5000);//设置时间持续时间为 5000毫秒  
  18.           
  19.         //尺寸伸缩动画效果 scale  
  20.         animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  21.         //第一个参数fromX为动画起始时 X坐标上的伸缩尺寸      
  22.         //第二个参数toX为动画结束时 X坐标上的伸缩尺寸       
  23.         //第三个参数fromY为动画起始时Y坐标上的伸缩尺寸      
  24.         //第四个参数toY为动画结束时Y坐标上的伸缩尺寸    
  25.           
  26.         //第五个参数pivotXType为动画在X轴相对于物件位置类型    
  27.         //第六个参数pivotXValue为动画相对于物件的X坐标的开始位置  
  28.         //第七个参数pivotXType为动画在Y轴相对于物件位置类型     
  29.         //第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置  
  30.         animation_scale.setRepeatCount(-1);  
  31.         animation_scale.setDuration(5000);//设置时间持续时间为 5000毫秒  
  32.           
  33.         //移动动画效果translate  
  34.         animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);  
  35.         //第一个参数fromXDelta为动画起始时 X坐标上的移动位置      
  36.         //第二个参数toXDelta为动画结束时 X坐标上的移动位置        
  37.         //第三个参数fromYDelta为动画起始时Y坐标上的移动位置   
  38.         //第三个参数toYDelta为动画结束时Y坐标上的移动位置   
  39.         animation_translate.setRepeatCount(-1);//设置动画执行多少次,如果是-1的话就是一直重复  
  40.         animation_translate.setDuration(5000);//设置时间持续时间为 5000毫秒  
  41.           
  42.         animationSet=new AnimationSet(true);  
  43.           
  44.         animationSet.addAnimation(animation_alpha);//透明度  
  45.         animationSet.addAnimation(animation_rotate);//旋转  
  46.         animationSet.addAnimation(animation_scale);//尺寸伸缩  
  47.         animationSet.addAnimation(animation_translate);//移动  
  48.         image.startAnimation(animationSet);//开始播放  
  49.      

 

AnimationSet这个类是:

Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.

意思是:代表一群动画应该一同玩耍。转变是由每个个体的动画在一起,成为一个单一的变换。如果AnimationSet设置任何属性,其子女也设置(例如,持续时间或fillBefore)的值AnimationSet推翻这孩子的价值观。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值