📜  Android-动画

📅  最后修改于: 2021-01-05 05:05:51             🧑  作者: Mango


动画是创造运动和形状变化的过程

android中的动画可以通过多种方式实现。在本章中,我们将讨论一种简单且广泛使用的动画制作方法,称为补间动画。

补间动画

补间动画采用一些参数,例如开始值,结束值,大小,持续时间,旋转角度等,并对该对象执行所需的动画。它可以应用于任何类型的对象。因此,为了使用此功能,android为我们提供了一个名为Animation的类。

为了在android中执行动画,我们将调用AnimationUtils类的静态函数loadAnimation()。我们将在Animation Object的实例中接收结果。它的语法如下-

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

注意第二个参数。它是我们的动画xml文件的名称。您必须在res目录下创建一个名为anim的新文件夹,并在anim文件夹下创建一个xml文件。

这个动画类具有许多有用的功能,在下面列出-

Sr.No Method & Description
1

start()

This method starts the animation.

2

setDuration(long duration)

This method sets the duration of an animation.

3

getDuration()

This method gets the duration which is set by above method

4

end()

This method ends the animation.

5

cancel()

This method cancels the animation.

为了将此动画应用于对象,我们将只调用对象的startAnimation()方法。它的语法是-

ImageView image1 = (ImageView)findViewById(R.id.imageView1);
image.startAnimation(animation);

以下示例演示了Android中Animation的使用。您将能够从菜单中选择不同类型的动画,并且所选动画将应用于屏幕上的imageView上。

要试验该示例,您需要在仿真器或实际设备上运行它。

Steps Description
1 You will use Android studio IDE to create an Android application and name it as My Application under a package com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add animation code
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
4 Create a new folder under res directory and call it anim. Confim it by visiting res/anim
5 Right click on anim and click on new and select Android XML file You have to create different files that are listed below.
6 Create files myanimation.xml,clockwise.xml,fade.xml,move.xml,blink.xml,slide.xml and add the XML code.
7 No need to change default string constants. Android studio takes care of default constants at values/string.xml.
8 Run the application and choose a running android device and install the application on it and verify the results.

这是MainActivity.java的修改后的代码。

package com.example.sairamkrishna.myapplication;

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 {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   
   public void clockwise(View view){
      ImageView image = (ImageView)findViewById(R.id.imageView);
      Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), 
         R.anim.myanimation);
      image.startAnimation(animation);
   }
   
   public void zoom(View view){
      ImageView image = (ImageView)findViewById(R.id.imageView);
      Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), 
         R.anim.clockwise);
      image.startAnimation(animation1);
   }
   
   public void fade(View view){
      ImageView image = (ImageView)findViewById(R.id.imageView);
      Animation animation1 = 
         AnimationUtils.loadAnimation(getApplicationContext(), 
            R.anim.fade);
      image.startAnimation(animation1);
   }
   
   public void blink(View view){
      ImageView image = (ImageView)findViewById(R.id.imageView);
      Animation animation1 = 
         AnimationUtils.loadAnimation(getApplicationContext(), 
            R.anim.blink);
      image.startAnimation(animation1);
   }
   
   public void move(View view){
      ImageView image = (ImageView)findViewById(R.id.imageView);
      Animation animation1 = 
         AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
      image.startAnimation(animation1);
   }
   
   public void slide(View view){
      ImageView image = (ImageView)findViewById(R.id.imageView);
      Animation animation1 = 
         AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide);
      image.startAnimation(animation1);
   }
}

这是res / layout / activity_main.xml的修改后的代码。

此处abc表示有关tutorialspoint徽标的信息


   
   
      
   
      
   
      
   
      
   
      
   
      
   
      
   
      
   
      

这是res / anim / myanimation.xml的代码。




   
   
   
   
   
   

这是res / anim / clockwise.xml的代码。




   
   
   
   
   
   

这是res / anim / fade.xml的代码。



   
   
   
   
   
      


这是res / anim / blink.xml的代码。



   

这是res / anim / move.xml的代码。



   
   

这是res / anim / slide.xml的代码



   
   

这是res / values / 字符串.xml的修改后的代码。


   My Application

这是AndroidManifest.xml的默认代码。




   
      
      
      
         
            
            
         
      
      
    
   

让我们尝试运行您的应用程序。我假设您已将实际的Android Mobile设备与计算机连接。要从Android Studio运行该应用,请打开您项目的活动文件之一,然后点击运行Eclipse运行图标工具栏中的图标。 Android Studio将显示以下图像

环形相机教程

选择缩放按钮,它将显示以下屏幕-

动漫动画教程

现在选择幻灯片按钮,它将显示以下屏幕

动漫动画教程

现在选择移动按钮,它将显示以下屏幕

动漫动画教程

现在顺时针按钮,它将显示以下屏幕

动漫动画教程

现在淡入淡出按钮,它将显示以下屏幕

动漫动画教程

注-如果在模拟器中运行它,则可能无法获得流畅的动画效果。您必须在android手机中运行它才能体验流畅的动画。