android Animation动画

Animation是一个用于View,Surfaces和其它对象实现动画效果的抽象类,其中常用的类是TranslateAnimation用于控制位置的改变.


以下列出一些重要的属性和方法:


Xml属性

android:duration:运行动画的时间

android:interpolator:定义用于平滑动画运动时的加速曲线(速率)

android:repeatCount:定义动画重复的时间


方法:

set:RepeatCount(int ):定义动画重复的次数

setRepeatMode(int):设置动画重复时候的行为

setStartOffset(long):以毫秒为单位的动画运行前的延迟时间

Cancel():取消动画

hasStarted():判断动画是否已在运行

initialize(int width, int height, int parentWidth, int parentHeight):初始化动画

reset():重置动画

Start()启动动画


其中还有一些常量

RESTART:重新运行

INFINITE:永无终止地运行


一:补间动画实现(xml中定义和代码中定义)


透明动画效果(alpha  AlphaAnimation)


<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android

android:duration="2000"

android:fromAlpha="0.0"

android:repeatCount="2"

android:repeatMode="reverse"

android:fillAfter="true"

android:toAlpha="0.8" >

</alpha>


AlphaAnimation类对象构造方法
AlphaAnimation(float fromAlpha, float toAlpha)

setDuration(3000) 设置持续时间
第一个参数fromAlpha为 动画开始时候透明度
第二个参数toAlpha为 动画结束时候透明度


尺寸渐变动画效果(scale  ScaleAnimation)


<?xml version="1.0" encoding="utf-8"?> 
<scale xmlns:android=" http://schemas.android.com/apk/res/android
android:interpolator=  "@android:anim/accelerate_decelerate_interpolator"
android:fromXScale = "0.0"
android:toXScale = "1.4"
android:fromYScale = "0.0"
android:toYScale = "1.4"
android:pivotX = "50%"
android:pivotY = "50%"
android:fillAfter="false
android:duration = "700">

</scale>

ScaleAnimation类对象构造方法

ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
第一个参数fromX为动画起始时,X坐标上的伸缩尺寸   
第二个参数toX为动画结束时,X坐标上的伸缩尺寸     
第三个参数fromY为动画起始时,Y坐标上的伸缩尺寸   
第四个参数toY为动画结束时,Y坐标上的伸缩尺寸  
说明:
                    以上四种属性值   
                    0.0表示收缩到没有
                    1.0表示正常无伸缩     
                    值小于1.0表示收缩  
                    值大于1.0表示放大

第五个参数pivotXType为动画在X轴相对于物件的位置类型  
第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
第七个参数pivotXType为动画在Y轴相对于物件位置类型   
第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置


平移动画效果(translate  TranslateAnimation)

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android=" http://schemas.android.com/apk/res/android
android:fromXDelta="0"
android:fromYDelta="0"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXDelta="0"
android:toYDelta="3" >

</translate>

TranslateAnimation类对象构造
TranslateAnimation(float fromXDelta, float toXDelta,
                       float fromYDelta, float toYDelta)
第一个参数fromXDelta为动画起始时 X坐标上的移动位置
第二个参数toXDelta为动画结束时 X坐标上的移动位置
第三个参数fromYDelta为动画起始时Y坐标上的移动位置
第四个参数toYDelta为动画结束时Y坐标上的移动位置


旋转动画效果(rotate RotateAnamation)


<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android=" http://schemas.android.com/apk/res/android"

android:fromDegrees="30"
android:pivotX="0.5"
android:pivotY="0.5"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="270" >

</rotate>

RotateAnimation(float fromDegrees, float toDegrees,
            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
第一个参数fromDegrees为动画起始时的旋转角度
第二个参数toDegrees为动画旋转到的角度
第三个参数pivotXType为动画在X轴相对于物件位置类型  
第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
第五个参数pivotXType为动画在Y轴相对于物件位置类型
第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
               Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);


二:如何使用java代码中的动画效果


使用从View父类继承过来的方法startAnimation()来为View或是子类View等添加一个动画效果
public void startAnimation (Animation animation)


三:如何使用XML中定义的动画

public static Animation loadAnimation (Contextcontext, int id)

//第一个参数Context为程序的上下文

//第二个参数id为动画XML文件的引用


myAnimation=AnimationUtils.loadAnimation(this,R.anim.my_action);

//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件


四:Activity界面切换动画实现方法

在startIntentActivity()后面加上OverriderPendingTransition(int enterAnim,int outAnim);

由于2个动画会同时执行,可设置后一个动画启动延时


五:逐帧动画(Frame By Frame)
Frame by frame 指将一幅幅图片按序播放,效果像gif动画:

在layout/myframeanimation.xml里定义动画。


<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android=" http://schemas.android.com/apk/res/android" >


<item
android:drawable="@drawable/girl_1"
android:duration="200"/>
<item
android:drawable="@drawable/girl_2"
android:duration="200"/>
<item
android:drawable="@drawable/girl_3"
android:duration="200"/>
<item
android:drawable="@drawable/girl_4"
android:duration="200"/>
<item
android:drawable="@drawable/girl_5"
android:duration="400"/>
<item
android:drawable="@drawable/girl_6"
android:duration="500"/>
<item
android:drawable="@drawable/girl_7"
android:duration="600"/>
<item
android:drawable="@drawable/girl_8"
android:duration="700"/>
<item
android:drawable="@drawable/girl_9"
android:duration="900"/>
<item
android:drawable="@drawable/girl_10"
android:duration="200"/>
<item
android:drawable="@drawable/girl_11"
android:duration="200"/>

</animation-list>

六.Interpolator的几个属性值:

@android:anim/accelerate_interpolator:越来越快

@android:anim/decelerate_interpolator:越来越慢

@android:anim/accelerate_decelerate_interpolator:先快后慢

@android:anim/anticipate_interpolator: 先后退一小步然后向前加速

@android:anim/overshoot_interpolator:快速到达终点超出一小步然后回到终点

@android:anim/anticipate_overshoot_interpolator:到达终点超出一小步然后回到终点

@android:anim/bounce_interpolator:到达终点产生弹球效果,弹几下回到终点

@android:anim/linear_interpolator:均匀速度。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值