[轉載+實踐理解]Android動畫---如何正確使用平移動畫(關於fillBefore和fillAfter的一點說明)(轉載)

紅色部分爲本身的實踐理解app

如何實現將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);this

mView.startAnimation(translate);spa

問題:當動畫結束後,View會跳回到原始位置。.net

 

改進:orm

AnimationSet set = new AnimationSet(true);blog

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);//這個必須設置在AnimationSet上面,設置在Animation上不起做用,網上不少寫法實際上是錯誤的

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的點擊位置會有問題,和看見的位置有誤差。

好比你把一個button用動畫從最左面移到最右面,雖然UI上顯示button到了最右面,但其實你在右面點button並不會觸發click事件,只有點最左面纔會觸發 

正確方法:

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向上平移自身高度一半的距離,而後播放動畫,從最初位置一直向上移動目標位置。

初看感受是在原始位置下1/2處開始動畫往上,最後到原始位置,其實不是這樣的,正如總結裏說的要倒過來看,是先把view的位置真正移上去自身一半高度,而後從移上去一半高度的位置算那個動畫初始位置,其實就是最初位置開始動畫,網上移一半高度,最後其實就是mView.offsetTopAndBottom(-mView.getHeight() / 2)的位置。

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放置到最終的目標位置,

而後倒過來,播放從原始位置到目標位置的動畫。

若是動畫結束後把view設成View.Gone的話,這段動畫直接放在onCreate()中的話,動畫不會啓動,所以作demo的時候總髮現怎麼不觸發,放到view的Click事件裏觸發就沒問題。包括-mView.getHeight() / 2在onCreate裏由於onMeasure等方法由於沒有執行完成,因此getHeight()是不正確的。所以若是學習的時候,只是作小demo看結果的話最好不要直接放在onCreate裏(具體緣由和解決方案看http://blog.csdn.net/johnny901114/article/details/7839512)

相關文章
相關標籤/搜索