標籤:
前言:
在 Android動畫學習(概述)中,如果你看過這篇文章,你應該會對緩動動畫和屬性動畫之間的區別產生疑問,因為在它們的應用中,你會感覺這兩種動畫有著一些相似的地方,為此,我打算把這兩種動畫之間的區別做一下說明
區別:
在這裡先附上官方文檔對於這兩種動畫的區別說明(我所說的緩動動畫對應在下文中的英文為:View Animation,屬性動畫對應在下文中的英文為:Property Animation):
How Property Animation Differs from View Animation
The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.
Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.
With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.
The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.
從文中的英文描述來看,緩動動畫和屬性動畫的卻別主要有三點:
1.緩動動畫的操作對象只能是View類型的對象,而屬性動畫的操作對象可以是View也可以不是View。因為緩動動畫的操作對象只能是View類型的對象,所以它能操作的屬性只能是View所具有的,這也導致了緩動動畫相比較屬性動畫可應用的面要窄。
2.緩動動畫在Translate屬性變化的時候,如果是個Button對象,那麼當它移動的時候,它的點擊地區並沒有隨著Button的移動而移動,你如果點擊它最初繪製的那個位置將會觸發Click事件,而點擊移動中的Button則不會觸發Click事件,這個時候就需要你自己來寫邏輯處理這種情況了。而屬性動畫則不會出現這種情況,它比緩動動畫要可靠很多。在更進階的需求下,例如:顏色,位置,尺寸以及同時使用多個動畫和插值器的地方,你最好使用屬性動畫。
最後,文中說明了一下為何要使用緩動動畫,因為緩動動畫只需要少量的代碼就可以實現動畫效果。如果緩動動畫能滿足你的需要,或者你現存的代碼已經使用了它,那麼你也無需去修改它,來使用屬性動畫。在一些情況下,可能會將緩動動畫和屬性動畫混合使用。
相關文檔:
屬性動畫和緩動動畫的區別
https://developer.android.com/guide/topics/graphics/prop-animation.html
Android動畫學習(緩動動畫與屬性動畫的區別)