Android动画之Tween动画

来源:互联网 发布:蜗蜗网络董事长秀才 编辑:程序博客网 时间:2024/05/16 19:11

Tween动画是补间动画,系统根据指定的初始状态以及结束状态绘制中间过程的View图像,实现动画。

顾名思义,Tween动画只能作用于View上面,可以通过java代码方式和xml方式进行设置。对应的类是Animation类。

XML方式

在res/anim/目录下创建动画文件,文件应该只有一个根元素,可以是<alpha><scale><translate><rotate><set>或者interpolator,<set>之中又可以包含<set>。默认所有动画都是同时进行的,可以通过设置startOffset属性决定延迟时间。比如下面的例子所示:

<set android:shareInterpolator="false">    <scale        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromXScale="1.0"        android:toXScale="1.4"        android:fromYScale="1.0"        android:toYScale="0.6"        android:pivotX="50%"        android:pivotY="50%"        android:fillAfter="false"        android:duration="700" />    <set android:interpolator="@android:anim/decelerate_interpolator">        <scale           android:fromXScale="1.4"           android:toXScale="0.0"           android:fromYScale="0.6"           android:toYScale="0.0"           android:pivotX="50%"           android:pivotY="50%"           android:startOffset="700"           android:duration="400"           android:fillBefore="false" />        <rotate           android:fromDegrees="0"           android:toDegrees="-45"           android:toYScale="0.0"           android:pivotX="50%"           android:pivotY="50%"           android:startOffset="700"           android:duration="400" />    </set></set>

一些值可以用来指定相对于父布局或者相对于自身来说。比如<rotate>的pivotX,”50”是相对于自身左边界的50px,”50%”是相对于自身左边界的50%,”50%p”是相对于父布局左边界的50%。

动画文件的使用:

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);spaceshipImage.startAnimation(hyperspaceJumpAnimation);

注意:无论动画如何变换,View的位置还是在原来的地方,只是动画可以把View绘制到View边界外的地方且不会被裁切,但是如果超出了父布局边界,还是会被裁切的。

原创粉丝点击