Android动画教程

时间:2020-02-23 14:29:25  来源:igfitidea点击:

本教程通过一个例子解释了什么是动画以及如何在Android应用程序中使用动画。

动画

你知道什么是动画吗?动画实际上是运动的错觉。这是一种技术,在这种技术中,连续的图像差别很小,产生了运动的效果。动画在移动应用程序中非常有用。动画在教育中是用来解释理论和概念的。应用程序中的动画用于显示一些指导原则或者程序。在Android应用程序中添加动画可以增加用户的添加性。它增加了乐趣和平滑的用户体验。在本教程中,我们将学习如何通过在Android应用程序中添加动画来增加用户体验。

Android动画

Android提供了一个名为Animations的类。这个类提供了许多不同的方法。以下是其中的几个:

loadAnimation(context,layout):此方法用于加载动画。它有两个参数。

start():用于启动动画。

setDuration(long duration):此方法设置Android中动画的持续时间。

getDuration():用于获取Android动画的持续时间。

end():用于结束动画。

cancel():用于取消动画。

Android动画示例

让我们开始在Android中创建动画示例。我将讨论四种类型的动画,即闪烁、淡出、顺时针和滑动。打开你的Android工作室,创建一个新的活动。在主活动中有一个图像视图和一个按钮。我用的是足球的形象。这是活动代码_主.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="368dp"
  android:layout_height="495dp"
  tools:layout_editor_absoluteX="8dp"
  tools:layout_editor_absoluteY="8dp"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:android="http://schemas.android.com/apk/res/android">

  <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="theitroad"
          android:id="@+id/textView2"
          android:textColor="#ff3eff0f"
          android:textSize="35dp"
          android:layout_centerHorizontal="true" 

      <ImageView
          android:layout_width="200dp"
          android:layout_height="200dp"
          android:id="@+id/imageView"
          android:src="@drawable/football"
          android:layout_below="@+id/textView2"
          android:layout_alignRight="@+id/textView2"
          android:layout_alignEnd="@+id/textView2"
          android:layout_marginTop="50dp" 

  <Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/imageView"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="59dp"
      android:backgroundTint="@color/colorAccent"
      android:onClick="Animation"
      android:text="Animation" 

  </RelativeLayout>

现在为闪烁动画、淡入动画、顺时针动画和幻灯片动画创建四个布局文件。这是眨眼的代码_动画.xml

<?xml version="1.0" encoding="utf-8"?>

  <set xmlns:android="http://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

<?xml version="1.0" encoding="utf-8"?>

  <set xmlns:android="http://schemas.android.com/apk/res/android"
      android:interpolator="@android:anim/accelerate_interpolator" >

      <alpha
          android:fromAlpha="0"
          android:toAlpha="1"
          android:duration="2000" >
      </alpha>

      <alpha
          android:startOffset="2000"
          android:fromAlpha="1"
          android:toAlpha="0"
          android:duration="2000" >
      </alpha>

  </set>

这是顺时针方向的代码_动画.xml

<?xml version="1.0" encoding="utf-8"?>

     <set xmlns:android="http://schemas.android.com/apk/res/android">

      <rotate xmlns:android="http://schemas.android.com/apk/res/android"
          android:fromDegrees="0"
          android:toDegrees="360"
          android:pivotX="50%"
          android:pivotY="50%"
          android:duration="5000" >
      </rotate>

      <rotate xmlns:android="http://schemas.android.com/apk/res/android"
          android:startOffset="5000"
          android:fromDegrees="360"
          android:toDegrees="0"
          android:pivotX="50%"
          android:pivotY="50%"
          android:duration="5000" >
      </rotate>

  </set>

这是幻灯片的代码_动画.xml

<?xml version="1.0" encoding="utf-8"?>

  <set xmlns:android="http://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>

现在创建一个java类主活动.java并粘贴以下代码

package com.example.admin.androidanimations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
  int count=0;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }

  public void Animation(View view){
      if(count==0){
          ImageView image = (ImageView)findViewById(R.id.imageView);
          Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),
          R.anim.clockwise_animation);
          image.startAnimation(animation);
      }
      if(count==1){
          ImageView image = (ImageView)findViewById(R.id.imageView);
          Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(),
          R.anim.fade_animation);
          image.startAnimation(animation1);
      }
      if(count==2){
          ImageView image = (ImageView)findViewById(R.id.imageView);
          Animation animation1 =
                  AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.blink_animation);
          image.startAnimation(animation1);
      }
      if(count==3){
          ImageView image = (ImageView)findViewById(R.id.imageView);
          Animation animation1 =
                  AnimationUtils.loadAnimation(getApplicationContext(),
                          R.anim.slide_animation);
          image.startAnimation(animation1);
          count=0;
      }
      count++;

  }

}

当第一次单击动画按钮时,用户将按顺时针方向旋转图像。当用户第二次单击count=1时,图像将淡出。当用户第三次单击count=2时,图像将开始闪烁。第四次是count=3,图像将显示幻灯片动画。