android_View Animation

包沈义
2023-12-01

http://developer.android.com/guide/topics/graphics/view-animation.html#tween-animation

View Animation(视图动画)

你可以使用view animation system来对view执行渐变动画。渐变动画计算动画的起点、终点、尺寸、旋转和其他的一些作为动画共有的属性。

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.

一个渐变动画能够对view的内容执行一系列简单的渐变(位置、尺寸、旋转和透明度)。所以,如果有一个TextView,你可以移动、旋转、放大或者缩小文字。如果这个TextView有一个背景图片,那么背景图片将跟随者文字一起渐变。Animation package 为我们提供了所有的会在渐变动画中使用到的类。

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.

在XML文件或者android文件中,通过一系列的指令,可以定义渐变动画。像定义布局一样,更推荐使用xml来定义动画,因为它跟硬编码的动画比较起来,更易读、更能重复使用、更能被替换。在下面的例子中,我们使用xml(如果想要进一步了解在application code里面定义动画,而不是xml,你可以参考AnimationSet类和其他Animation子类

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.)

动画指令定义了你想要发生的渐变,这个渐变发生的时间和持续多久。渐变可以次序发生或者同时发生,例如,你可以让TextView的内容从左移动到右,然后旋转180度;或者你也可以让移动和旋转同时进行。每种渐变对应着特定的渐变参数(起始大小和最终大小对应着尺寸改变,起始角度和最终角度对应着旋转,等等),当然也有些属性石公共的(例如开始时间和持续时间)。让各种渐变同时进行,可以给他们赋予相同的开始时间;让他们顺序进行的话,需要计算开始时间加上渐变的持续时间。

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.

Animation xml文件位于android工程下的res/anim目录下。文件必须有一个根节点,可以是<alpha>、<scale>、<translate>、<rotate>、 interpolator element, or<set> 节点(能以其他节点为它的子节点,或者包含<set>).在默认情况下,动画指令是同时使用的。如果要让动画顺序进行,你必须要设置startOffset属性,如下面的例子所示:

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 either a single <alpha><scale><translate><rotate>, interpolator element, or<set> element that holds groups of these elements (which may include another <set>). By default, all animation instructions are applied simultaneously. To make them occur sequentially, you must specify thestartOffset attribute, as shown in the example below.

下面的xml来自apidemo,它拉伸同时旋转一个view。

The following XML from one of the ApiDemos is used to stretch, then simultaneously spin and rotate a View object.

<set android:shareInterpolator="false">
    <scale
        android: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">
        <scale
           android: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" />
        <rotate
           android: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指依赖于父的50%,而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).

通过指定一个Interpolator,你可以定义在一段时间里渐变怎样执行。Android定义了很多Interpolator子类,这些子类定义了各种速度弧线:例如AccelerateInterpolator告诉渐变开始时缓慢,然后加速。我们可以通过在xml中引用它们。

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.

在hyperspace_jump.xml定义了view animation,该文件位于res/anim/目录下,下面的代码将会访问它,并且将它使用于ImageView

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);

你也可以为animation设置开始时间,然后将View.setAnimation()

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().

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. 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.


 类似资料:

相关阅读

相关文章

相关问答