安卓动画示例

Android动画用于为用户界面提供丰富的外观和感觉。Android应用程序中的动画可以通过XML或Android代码实现。在本Android动画教程中,我们将使用XML代码将动画添加到我们的应用程序中。

安卓动画

在Android应用中,动画是创建运动和形状变化的过程。在本教程中,我们将介绍动画的基本方法:

    渐入动画
    渐出动画
    交叉淡入淡出动画
    闪烁动画
    放大动画
    缩小动画
    旋转动画
    移动动画
    向上滑动动画
    向下滑动动画
    弹跳动画
    序列动画
    同时动画

Android动画示例XML

我们在res文件夹下创建了一个资源目录,名为anim,用于存放包含动画逻辑的所有xml文件。下面是一个示例xml文件,展示了Android动画的代码逻辑。sample_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="https://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  android:duration="300"
  android:fillAfter="true"
  android:fromXScale="0.0"
  android:fromYScale="0.0"
  android:toXScale="1.0"
  android:toYScale="1.0" />
  • android:interpolator : It is the rate of change in animation. We can define our own interpolators using the time as the constraint. In the above xml code an inbuilt interpolator is assigned
  • android:duration : Duration of the animation in which the animation should complete. It is 300ms here. This is generally the ideal duration to show the transition on the screen. The start and end of the animation are set using:
    android:fromTRANSFORMATION
    android:toTRANSFORMATION

  • TRANSFORMATION : is the transformation that we want to specify. In our case we start with an x and y scale of 0 and end with an x and y scale of 1
  • android:fillAfter : property specifies whether the view should be visible or hidden at the end of the animation. We’ve set it visible in the above code. If it sets to false, the element changes to its previous state after the animation
  • android:startOffset : It is the waiting time before an animation starts. This property is mainly used to perform multiple animations in a sequential manner
  • android:repeatMode : This is useful when you want the animation to be repeat
  • android:repeatCount : This defines number of repetitions on animation. If we set this value to infinite then animation will repeat infinite times

当用户界面小部件被点击时,加载动画

我们的目标是在任何小部件(比如 TextView)被点击时显示动画。为此我们需要使用 Animation 类。通过调用 loadAnimation() 函数,使用 AnimationUtils 类加载包含动画逻辑的 xml 文件。以下代码片段展示了这个实现。

Animation animation;
animation = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.sample_animation);

为了开始动画,我们需要在下面的代码片段中调用startAnimation()函数来处理UI元素。

sampleTextView.startAnimation(animation);

我们通过将动画类型作为参数传递来对文本视图组件进行动画操作。

设置动画监听器

只有在我们希望监听事件,如开始、结束或重复时才需要这样做。为此,活动必须实现AnimationListener,并需要重写以下方法。

  • onAnimationStart : This will be triggered once the animation started
  • onAnimationEnd : This will be triggered once the animation is over
  • onAnimationRepeat : This will be triggered if the animation repeats

安卓动画项目结构。

如您所见,我们已包含了上述所有主要类型动画的XML文件。

Android动画示例的XML代码

这里我提供了大部分常见的Android动画的示例代码。

淡入动画

淡入.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

这里的alpha指的是一个物体的不透明度。具有较低alpha值的物体更透明,而具有较高alpha值的物体则较不透明,更不易穿透。淡入动画就是将alpha值从0增加到1。

渐变动画

渐变消失.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>

淡出动画与淡入动画完全相反,需要将透明度从1减少到0。

交叉淡入淡出动画

在MainActivity.java中讨论的代码中,交叉淡入淡出是在一个TextView上执行淡入动画,同时另一个TextView逐渐消失。可以通过在这两个TextView上使用fade_in.xml和fade_out.xml来实现。

闪烁动画

眨眼.xml.

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="600"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/>
</set>

每次都以无限反向模式执行淡入淡出。

缩放动画

扩大镜头.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <scale
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="3"
        android:toYScale="3" >
    </scale>

</set>

我们使用 pivotX=”50%” 和 pivotY=”50%” 来从元素中心执行缩放。

放大动画

缩小视角.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <scale
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" >
    </scale>

</set>

请注意,zoom_in.xml和zoom_out.xml中的android:from和android:to是相反的。

旋转动画

旋转.xml

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <rotate android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="600"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        android:interpolator="@android:anim/cycle_interpolator"/>

</set>

这里使用了一个from/toDegrees标签来指定角度,并使用了一个循环插值器。

移动动画

移动.xml

<set
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

滑动上移动画

滑上去.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="0.0" />

</set>

通过在缩放标记内设置android:fromYScale=”1.0″和android:toYScale=”0.0″来实现。

下滑动画

滑动向下.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

这正好与slide_up.xml相反。

弹跳动画

多项选择回弹.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/bounce_interpolator">

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

在这里,我们使用反弹插值器来实现以弹跳方式完成动画。

连续动画

顺序.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator" >

  
    <!-- Move -->
    <translate
        android:duration="800"
        android:fillAfter="true"
        android:fromXDelta="0%p"
        android:startOffset="300"
        android:toXDelta="75%p" />
    <translate
        android:duration="800"
        android:fillAfter="true"
        android:fromYDelta="0%p"
        android:startOffset="1100"
        android:toYDelta="70%p" />
    <translate
        android:duration="800"
        android:fillAfter="true"
        android:fromXDelta="0%p"
        android:startOffset="1900"
        android:toXDelta="-75%p" />
    <translate
        android:duration="800"
        android:fillAfter="true"
        android:fromYDelta="0%p"
        android:startOffset="2700"
        android:toYDelta="-70%p" />

    <!-- Rotate 360 degrees -->
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/cycle_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="3800"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>

这里使用了不同的android:startOffset来保持过渡之间的顺序。

一起动画

一起.xml

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator" >

    
    <!-- Move -->
    <scale
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:duration="4000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="4"
        android:toYScale="4" >
    </scale>

    <!-- Rotate 180 degrees -->
    <rotate
        android:duration="500"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>

这里移除了 android:startOffset,让它们同时发生。

代码

activity_main.xml 布局由 ScrollView 和 RelativeLayout 组成(我们将在下一个教程中讨论)。这个布局通过各自的按钮调用每种动画类型来改变文本。以下是 xml 文件的示例: activity_main.xml。

<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
                android:id="@+id/btnFadeIn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="Fade In" />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Fade In"
                android:id="@+id/txt_fade_in"
            android:layout_alignBottom="@+id/btnFadeIn"
            android:layout_alignLeft="@+id/txt_fade_out"
            android:layout_alignStart="@+id/txt_fade_out" />


        <Button
                android:id="@+id/btnFadeOut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnFadeIn"
                android:text="Fade Out" />


        <Button
                android:id="@+id/btnCrossFade"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnFadeOut"
                android:text="Cross Fade" />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Cross Fade In"
                android:id="@+id/txt_out"
                android:visibility="gone"
                android:layout_gravity="center_horizontal"
                android:layout_alignTop="@+id/txt_in"
                android:layout_alignLeft="@+id/txt_in"
                android:layout_alignStart="@+id/txt_in" />

        <Button
                android:id="@+id/btnBlink"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnCrossFade"
                android:text="Blink" />

        <Button
                android:id="@+id/btnZoomIn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnBlink"
                android:text="Zoom In" />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Blink"
                android:id="@+id/txt_blink"
                android:layout_gravity="center_horizontal"
            android:layout_alignBottom="@+id/btnBlink"
            android:layout_alignLeft="@+id/txt_zoom_in"
            android:layout_alignStart="@+id/txt_zoom_in" />

        <Button
                android:id="@+id/btnZoomOut"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnZoomIn"
                android:text="Zoom Out" />

        <Button
                android:id="@+id/btnRotate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnZoomOut"
                android:text="Rotate" />

        <Button
                android:id="@+id/btnMove"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnRotate"
                android:text="Move" />

        <Button
                android:id="@+id/btnSlideUp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnMove"
                android:text="Slide Up" />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Fade Out"
                android:id="@+id/txt_fade_out"
                android:layout_gravity="center_horizontal"
            android:layout_alignBottom="@+id/btnFadeOut"
            android:layout_alignLeft="@+id/txt_in"
            android:layout_alignStart="@+id/txt_in" />

        <Button
                android:id="@+id/btnSlideDown"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnSlideUp"
                android:text="Slide Down" />

        <Button
                android:id="@+id/btnBounce"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnSlideDown"
                android:text="Bounce" />

        <Button
                android:id="@+id/btnSequential"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_below="@id/btnBounce"
                android:text="Sequential Animation" />

        <Button
                android:id="@+id/btnTogether"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/btnSequential"
                android:layout_margin="5dp"
                android:text="Together Animation" />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Cross Fade Out"
                android:id="@+id/txt_in"
                android:layout_gravity="center_horizontal"
            android:layout_alignBottom="@+id/btnCrossFade"
            android:layout_alignLeft="@+id/txt_blink"
            android:layout_alignStart="@+id/txt_blink" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Zoom In"
            android:id="@+id/txt_zoom_in"
            android:layout_alignBottom="@+id/btnZoomIn"
            android:layout_alignLeft="@+id/txt_zoom_out"
            android:layout_alignStart="@+id/txt_zoom_out" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Zoom Out"
            android:id="@+id/txt_zoom_out"
            android:layout_alignBottom="@+id/btnZoomOut"
            android:layout_toRightOf="@+id/btnSequential"
            android:layout_toEndOf="@+id/btnSequential" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Rotate"
            android:id="@+id/txt_rotate"
            android:layout_above="@+id/btnMove"
            android:layout_toRightOf="@+id/btnSequential"
            android:layout_toEndOf="@+id/btnSequential" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Move"
            android:id="@+id/txt_move"
            android:layout_alignBottom="@+id/btnMove"
            android:layout_alignLeft="@+id/txt_slide_up"
            android:layout_alignStart="@+id/txt_slide_up" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Slide Up"
            android:id="@+id/txt_slide_up"
            android:layout_alignBottom="@+id/btnSlideUp"
            android:layout_toRightOf="@+id/btnSequential"
            android:layout_toEndOf="@+id/btnSequential" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Slide Down"
            android:id="@+id/txt_slide_down"
            android:layout_alignBottom="@+id/btnSlideDown"
            android:layout_alignLeft="@+id/txt_slide_up"
            android:layout_alignStart="@+id/txt_slide_up" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Bounce"
            android:id="@+id/txt_bounce"
            android:layout_alignBottom="@+id/btnBounce"
            android:layout_alignLeft="@+id/txt_slide_down"
            android:layout_alignStart="@+id/txt_slide_down" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Sequential"
            android:id="@+id/txt_seq"
            android:layout_alignBottom="@+id/btnSequential"
            android:layout_alignLeft="@+id/txt_bounce"
            android:layout_alignStart="@+id/txt_bounce" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Together"
            android:id="@+id/txt_tog"
            android:layout_alignBottom="@+id/btnTogether"
            android:layout_toRightOf="@+id/btnSequential"
            android:layout_toEndOf="@+id/btnSequential" />

    </RelativeLayout>

</ScrollView>

总结一下,RelativeLayout是一种相对于其他UI组件进行排列的布局方式。MainActivity.java文件包含了与每个按钮相关的动画类型的onClick监听器。其源代码如下所示。

package com.Olivia.animations;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button btnFadeIn, btnFadeOut, btnCrossFade, btnBlink, btnZoomIn,
            btnZoomOut, btnRotate, btnMove, btnSlideUp, btnSlideDown,
            btnBounce, btnSequential, btnTogether;
    Animation animFadeIn,animFadeOut,animBlink,animZoomIn,animZoomOut,animRotate
            ,animMove,animSlideUp,animSlideDown,animBounce,animSequential,animTogether,animCrossFadeIn,animCrossFadeOut;
    TextView txtFadeIn,txtFadeOut,txtBlink,txtZoomIn,txtZoomOut,txtRotate,txtMove,txtSlideUp,
                txtSlideDown,txtBounce,txtSeq,txtTog,txtIn,txtOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnFadeIn = (Button) findViewById(R.id.btnFadeIn);
        btnFadeOut = (Button) findViewById(R.id.btnFadeOut);
        btnCrossFade = (Button) findViewById(R.id.btnCrossFade);
        btnBlink = (Button) findViewById(R.id.btnBlink);
        btnZoomIn = (Button) findViewById(R.id.btnZoomIn);
        btnZoomOut = (Button) findViewById(R.id.btnZoomOut);
        btnRotate = (Button) findViewById(R.id.btnRotate);
        btnMove = (Button) findViewById(R.id.btnMove);
        btnSlideUp = (Button) findViewById(R.id.btnSlideUp);
        btnSlideDown = (Button) findViewById(R.id.btnSlideDown);
        btnBounce = (Button) findViewById(R.id.btnBounce);
        btnSequential = (Button) findViewById(R.id.btnSequential);
        btnTogether = (Button) findViewById(R.id.btnTogether);
        txtFadeIn=(TextView)findViewById(R.id.txt_fade_in);
        txtFadeOut=(TextView)findViewById(R.id.txt_fade_out);
        txtBlink=(TextView)findViewById(R.id.txt_blink);
        txtZoomIn=(TextView)findViewById(R.id.txt_zoom_in);
        txtZoomOut=(TextView)findViewById(R.id.txt_zoom_out);
        txtRotate=(TextView)findViewById(R.id.txt_rotate);
        txtMove=(TextView)findViewById(R.id.txt_move);
        txtSlideUp=(TextView)findViewById(R.id.txt_slide_up);
        txtSlideDown=(TextView)findViewById(R.id.txt_slide_down);
        txtBounce=(TextView)findViewById(R.id.txt_bounce);
        txtSeq=(TextView)findViewById(R.id.txt_seq);
        txtTog=(TextView)findViewById(R.id.txt_tog);
        txtIn=(TextView)findViewById(R.id.txt_in);
        txtOut=(TextView)findViewById(R.id.txt_out);
        animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in);
		
        animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in);
        // fade in
        btnFadeIn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                txtFadeIn.setVisibility(View.VISIBLE);
                txtFadeIn.startAnimation(animFadeIn);
            }
        });

        animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_out);

        // fade out
        btnFadeOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtFadeOut.setVisibility(View.VISIBLE);
                txtFadeOut.startAnimation(animFadeOut);
            }
        });
        animCrossFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in);
        animCrossFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_out);
        // cross fade
        btnCrossFade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtOut.setVisibility(View.VISIBLE);
                // start fade in animation
                txtOut.startAnimation(animCrossFadeIn);

                // start fade out animation
                txtIn.startAnimation(animCrossFadeOut);
            }
        });
        animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.blink);
        // blink
        btnBlink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtBlink.setVisibility(View.VISIBLE);
                txtBlink.startAnimation(animBlink);
            }
        });

        animZoomIn = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.zoom_in);
        // Zoom In
        btnZoomIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtZoomIn.setVisibility(View.VISIBLE);
                txtZoomIn.startAnimation(animZoomIn);
            }
        });
        animZoomOut = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.zoom_out);
        // Zoom Out
        btnZoomOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtZoomOut.setVisibility(View.VISIBLE);
                txtZoomOut.startAnimation(animZoomOut);
            }
        });
        animRotate = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.rotate);

        // Rotate
        btnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtRotate.startAnimation(animRotate);
            }
        });
        animMove = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.move);
        // Move
        btnMove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtMove.startAnimation(animMove);
            }
        });
        animSlideUp = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.slide_up);
        // Slide Up
        btnSlideUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtSlideUp.startAnimation(animSlideUp);
            }
        });
        animSlideDown = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.slide_down);
        // Slide Down
        btnSlideDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtSlideDown.startAnimation(animSlideDown);
            }
        });
        animBounce = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.bounce);
        // Slide Down
        btnBounce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtBounce.startAnimation(animBounce);
            }
        });
        animSequential = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.sequential);
        // Sequential
        btnSequential.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                        txtSeq.startAnimation(animSequential);
            }
        });
        animTogether = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.together);

        // Together
        btnTogether.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtTog.startAnimation(animTogether);
            }
        });

    }
}

如前所述,每个TextView的动画是通过调用相应的动画对象来启动的,其中动画逻辑是通过AnimationUtils.loadAnimation()方法加载的。淡入淡出动画包含两个TextView,其中一个逐渐消失,另一个逐渐出现。下面是一个短视频展示我们应用中的所有动画。合并动画可在上面的图片中看到。请注意,当在模拟器上运行这些动画时可能不太流畅,因此建议在正常设备上运行应用程序。这就结束了Android动画示例教程。您可以从以下链接下载Android动画示例项目。

下载安卓动画XML项目

广告
将在 10 秒后关闭
bannerAds