TranslateAnimation

来源:互联网 发布:嘉兴学院客户端 mac 编辑:程序博客网 时间:2024/05/22 04:27

如何实现将View向上平移自身高度一半的距离?

TranslateAnimation translate = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,

Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);

mView.startAnimation(translate);

问题:当动画结束后,View会跳回到原始位置。

改进:

AnimationSet set = new AnimationSet(true);

TranslateAnimation translate = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,

Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);

set.addAnimation(translate);

set.setFillAfter(true);

mView.startAnimation(set);

setFillAfter文档说明:

If fillAfter is true, the transformation that this animation performed

will persist when it is finished. Defaults to false if not set.

设为true之后,界面会停留在动画播放完时的界面。

问题:动画结束后界面显示正确,但是View上各控件的实际位置和看上去的位置不对应,

实际位置还在View的原始位置,因此button的点击位置会有问题,和看见的位置有偏差。

正确方法:

AnimationSet set = new AnimationSet(true);

TranslateAnimation translate = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,

Animation.RELATIVE_TO_SELF, 0.5, Animation.RELATIVE_TO_SELF, 0);

set.addAnimation(translate);

set.setFillAfter(true);

mView.offsetTopAndBottom(-mView.getHeight() / 2);

mView.startAnimation(set);

先将View向上平移自身高度一半的距离,然后播放动画,从最初位置一直向上移动目标位置。

setFillBefore文档说明:

If fillBefore is true, this animation will apply its transformation

before the start time of the animation. Defaults to true if

setFillEnabled(boolean) is not set to true.

对TranslateAnimation,setFillBefore默认为true,也就是说在动画开始前,先将transformation

apply到View,这也就是为什么offsetTopAndBottom()后,View依然从原始位置开始运动。

如果setFillBefore设为false,动画播放时会有一个跳动,可以看到View从目标位置跳到原始位置。

总结:

使用Animation、AnimationSet框架实现的动画效果,必须先将View放置到最终的目标位置,

然后倒过来,播放从原始位置到目标位置的动画。

fillBefore是指动画结束时画面停留在此动画的第一帧; 默认值为true

fillAfter是指动画结束是画面停留在此动画的最后一帧。默认值为false

关于TranslateAnimation几个构造函数的参数意义,曾困惑我不少时间,参考官方文档和网上的讲解,通过试验总结出一些自己的理解,如果有误敬请指正。
TranslateAnimation共有三个构造函数,分别是:

其中,我们最常用的是后两个,现在主要想说明一下我对后两个函数参数的理解。
先说第二个构造函数:TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, floattoYDelta)。
Delta,顾名思义表示的是一个后一个和前一个的差值。XDelta即表示在X方向上的差值,同理YDelta表示在Y方向的差值。若XDelta>0,则说明控件向右侧发生移动,否则向左侧移动,Y轴方向是相同的道理。现在来说下各个参数的意思:
fromXDelta:控件的开始移动前的位置,为什么是Delta呢?因为在此之前,该控件可能已经发生过了位移,因此它已经偏离了控件最初始的位置。因此采用了距离最初始位置的偏移量。
toXDelta:相同道理,想要移动的终点位置距离最初始位置的偏移量。记住,一定不要混淆的是,不要把这个最初始位置当成是移动开始前控件的位置,否则将会发生错误移动。
后面两个参数表示Y方向上的,和X方向上的同理。需要说明的是,这个是绝对偏移量,是以像素为单位进行计算的。
再来说说第三个构造函数。
当X方向或者Y方向上的Type选择为Animation.ABSOLUTE时候,表示为绝对像素,此时XValue和YValue参数的含义和第二个构造函数相同。
而当X方向或者Y方向上的Type选择为Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT时候,则表示相位移量了,举个例子来说,如果在X方向上选择Animation.RELATIVE_TO_SELF,那么当XValue=1.0f时,则偏移量为一个自身宽度。如果在X方向上选择Animation.RELATIVE_TO_PARENT时,则偏移量为一个父控件宽度。Y方向是相同的道理,只是把宽度换成了高度而已。

0 0