View Animation
原文URL:http://developer.android.com/guide/topics/graphics/view-animation.html
You can use the view animation system to perform?tweened animation(補間動畫)?on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.
A tween animation can perform a series of?simple transformations (position, size, rotation, and transparency)?on the contents of a View object. So, if you have a?TextView?object, you can move, rotate, grow, or shrink the text. If it has a background image, the background image will be transformed along with the text. Theanimation package?provides all the classes used in a tween animation.
A sequence of animation instructions defines the tween animation,defined by either XML or Android code(可以使用XML文件或者Android 代碼的方式指定動畫).As with defining a layout, an XML file is recommended(和Layout一樣,推薦使用XML文件的方式)?because it's more readable, reusable, and swappable than hard-coding the animation. In the example below, we use XML. (To learn more about defining an animation in your application code, instead of XML, refer to the?AnimationSet?class and other?Animation?subclasses.)
The animation instructions define the transformations that you want to occur, when they will occur, and how long they should take to apply.Transformations can be sequential or simultaneous(動畫可以有序的發生,也可以同時發生,這個根據各個動作的起始時間而定)?- for example, you can have the contents of a TextView move from left to right, and then rotate 180 degrees, or you can have the text move and rotate simultaneously. Each transformation takes a set of parameters specific for that transformation (starting size and ending size for size change, starting angle and ending angle for rotation, and so on), and also a set of common parameters (for instance, start time and duration).?To make several transformations happen simultaneously, give them the same start time; to make them sequential, calculate the start time plus the duration of the preceding transformation.
(為了使幾個動作能夠同時發生,將它們的起始時間設為一樣;為了使它們能夠有序的發生,將后一個動作的起始時間設置為前一個動作的起始時間加上它的持續時間)
The animation XML file belongs in the?res/anim/?directory of your Android project(定義動畫的XML文件位于Android 項目的 res/anim 目錄下). The file must have a single root element:?this will be either a single?<alpha>,?<scale>,?<translate>,?<rotate>, interpolator element, or<set>?element that holds groups of these elements (which may include another?<set>). (該動畫XML文件的根節點,可以是以上節點之一)。By default, all animation instructions are applied simultaneously(默認,所有的動作是同時發生的). To make them occur sequentially, you must specify the?startOffset?attribute, as shown in the example below(為了使它們有序的發生,必須設置 startOffset 屬性,見下面示例).
The following XML from one of the ApiDemos is used to stretch, then simultaneously spin and rotate a View object.
<set android:shareInterpolator="false"><scaleandroid: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"><scaleandroid: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" /><rotateandroid:fromDegrees="0"android:toDegrees="-45"android:toYScale="0.0"android:pivotX="50%"android:pivotY="50%"android:startOffset="700"android:duration="400" /></set> </set>
Screen coordinates (not used in this example) are (0,0) at the upper left hand corner, and increase as you go down and to the right.
Some values, such as pivotX, can be specified relative to the object itself or relative to the parent. Be sure to use the proper format for what you want ("50" for 50% relative to the parent, or "50%" for 50% relative to itself).
You can determine how a transformation is applied over time by assigning an?Interpolator(可以指定動畫轉換的樣式,如旋轉的時候可以先快后慢,或者先慢后快等).?Android includes several Interpolator subclasses that specify various speed curves: for instance,?AccelerateInterpolatortells a transformation to start slow and speed up. Each one has an attribute value that can be applied in the XML.
With this XML saved as?hyperspace_jump.xml?in the?res/anim/?directoryof the project, the following code will reference it and apply it to an?ImageView?object from the layout.
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage); Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); spaceshipImage.startAnimation(hyperspaceJumpAnimation);As an alternative to?startAnimation(), you can define a starting time for the animation withAnimation.setStartTime(), then assign the animation to the View with?View.setAnimation()(作為startAnimation()方法的替代方法,我們也可以使用Animation.setStartTime()設置動畫的起始時間,然后使用View.setAnimation()將該動畫適用于一個View).
For more information on the XML syntax, available tags and attributes, see Animation Resources.
Note: Regardless of how your animation may move or resize, the bounds of the View that holds your animation will not automatically adjust to accommodate it(無論動畫如何變換,使用動畫的View的邊框都不會自適應改變 。--ps:也就是說,如果你在該view上設置了一個點擊事件,當動畫開進行的過程中,你點擊當前的view,是不會觸發上述點擊事件的。只有點擊動畫開始之前View的邊界內的區域,才會觸發該點擊事件。還有另一種情形就是,你想實時獲取動畫過程中當前View 的坐標,得到的也只是動畫之前的坐標。).Even so, the animation will still be drawn beyond the bounds of its View and will not be clipped. However, clipping will occur if the animation exceeds the bounds of the parent View.
總結
以上是生活随笔為你收集整理的View Animation的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android中级教程之--------
- 下一篇: 利用JDBC连接Oracle数据库