您的位置:首页 > 其它

视图动画View Animation入门

2015-05-22 16:24 381 查看

View Animation

 你可以使用 view animation system 对一个view实现补间动画. A tween animation can perform a series of simple transformations (position, size, rotation, and transparency) on the contents of a View object.. The
animation
package
provides all the classes used in a tween animation. 补间动画能够对一个view执行一系列的转置操作,比如位置,大小旋转和移动,animation package包提供了所有补间动画的类和方法A sequence of animation instructions defines the tween animation, defined by either XML or Android code. As with defining a layout, an XML file is recommended 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.) 定义补间动画的一系列指令可以是XML,也可以是Android代码,不过首选XML文件的方式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 fromleft 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 anglefor 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 starttime plus the duration of the preceding transformation.动画指令定义了动画何时发生和持续的时间。一个转置可以顺序执行,也可以同时执行,例如你可以让一个TextView先从左移到右,再旋转180度,也可以让移动和旋转同时发生The animation XML file belongs in the
res/anim/
directory of your Android project. The file must have a single root element: this will be eithera single
<alpha>
,
<scale>
,
<translate>
,
<rotate>
, interpolator element, or
<set>
element that holds groups of these elements (which may include another
<set>
).Bydefault, all animation instructions are applied simultaneously. To make them occur sequentially, you must specify the
startOffset
attribute,as shown in the example below. animation的xml文件放在 项目的
res/anim/
目录下,这些文件必须有一个<alpha>
<scale>
,
<translate>
<rotate>
,interpolator element, or 
<set>
这样的根元素,
切指令是同时执行的,要想顺序执行的话,必须设置startOffset
 [/code]
[/code]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>
屏幕的坐标是左上角的(0,0),然后向下和向右扩展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.pivotX:50代表相对于父view,50%相对于自己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,
AccelerateInterpolator
tells 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/
directory of 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 with
Animation.setStartTime()
,
then assign the animation to the View with
View.setAnimation()
.For more information on the XML syntax, available tags and attributes, seeAnimation 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. Even so, the animation will still be drawn beyond the bounds of its View and willnot be clipped. However, clipping will occur if the animation exceeds the bounds of the parent View.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息