Animation动画

Animation:  1,AlphaAnimation, 透明度动画,

                   2, RotateAnimation, 旋转动画,

                   3,ScaleAnimation, 缩放动画

                   4,TranslateAnimation 位移动画

              通过setDuration()设置动画时长,setAnimation()将想要的动画添加到你所想要的图片ImageView上。

note:如果想让一张图片具有多个动画的话,使用AnimationSet动画集合类,用addAnimation()添加想要的动画。

简单的Animation demo截图:

Animation动画

以下是XML布局和Java代码:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:paddingBottom="@dimen/activity_vertical_margin"

 6     android:paddingLeft="@dimen/activity_horizontal_margin"

 7     android:paddingRight="@dimen/activity_horizontal_margin"

 8     android:paddingTop="@dimen/activity_vertical_margin"

 9     tools:context="com.example.android_animation.MainActivity" >

10 

11     <ImageView

12         android:id="@+id/imageView1"

13         android:layout_width="wrap_content"

14         android:layout_height="wrap_content"

15         android:layout_alignParentTop="true"

16         android:layout_centerHorizontal="true"

17         android:layout_marginTop="28dp"

18         android:src="@drawable/heihei" />

19 

20     <Button

21         android:id="@+id/button1"

22         android:layout_width="match_parent"

23         android:layout_height="wrap_content"

24         android:layout_alignLeft="@+id/button2"

25         android:layout_below="@+id/imageView1"

26         android:layout_marginTop="36dp"

27         android:text="透明度动画" />

28 

29     <Button

30         android:id="@+id/button2"

31         android:layout_width="match_parent"

32         android:layout_height="wrap_content"

33         android:layout_alignLeft="@+id/button3"

34         android:layout_below="@+id/button1"

35         android:layout_marginTop="14dp"

36         android:text="旋转动画" />

37 

38     <Button

39         android:id="@+id/button3"

40         android:layout_width="match_parent"

41         android:layout_height="wrap_content"

42         android:layout_alignLeft="@+id/button4"

43         android:layout_below="@+id/button2"

44         android:text="位移动画" />

45 

46     <Button

47         android:id="@+id/button4"

48         android:layout_width="match_parent"

49         android:layout_height="wrap_content"

50         android:layout_alignLeft="@+id/button5"

51         android:layout_below="@+id/button3"

52         android:text="缩放动画" />

53 

54     <Button

55         android:id="@+id/button5"

56         android:layout_width="match_parent"

57         android:layout_height="wrap_content"

58         android:layout_below="@+id/button4"

59         android:layout_centerHorizontal="true"

60         android:text="多种动画" />

61 

62 </RelativeLayout>
Animation XML布局
 1 package com.example.android_animation;

 2 

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.view.View;

 6 import android.view.View.OnClickListener;

 7 import android.view.animation.AlphaAnimation;

 8 import android.view.animation.Animation;

 9 import android.view.animation.AnimationSet;

10 import android.view.animation.RotateAnimation;

11 import android.view.animation.ScaleAnimation;

12 import android.view.animation.TranslateAnimation;

13 import android.widget.Button;

14 import android.widget.ImageView;

15 

16 public class MainActivity extends Activity implements OnClickListener {

17     private ImageView imageview;

18     private Button button1;

19     private Button button2;

20     private Button button3;

21     private Button button4;

22     private Button button5;

23     private Animation animation;

24     private Animation alphaanimation;

25     private Animation translateanimation;

26 

27     @Override

28     protected void onCreate(Bundle savedInstanceState) {

29         super.onCreate(savedInstanceState);

30         setContentView(R.layout.activity_main);

31         imageview = (ImageView) findViewById(R.id.imageView1);

32         // imageview.setImageResource(R.drawable.mao);

33         button1 = (Button) findViewById(R.id.button1);

34         button2 = (Button) findViewById(R.id.button2);

35         button3 = (Button) findViewById(R.id.button3);

36         button4 = (Button) findViewById(R.id.button4);

37         button5 = (Button) findViewById(R.id.button5);

38         button1.setOnClickListener(this);

39         button2.setOnClickListener(this);

40         button3.setOnClickListener(this);

41         button4.setOnClickListener(this);

42         button5.setOnClickListener(this);

43     }

44 

45     @Override

46     public void onClick(View v) {

47         // TODO Auto-generated method stub

48         switch (v.getId()) {

49         case R.id.button1:

50             // 透明度动画动画

51             animation = new AlphaAnimation(0.1f, 1.0f);

52             animation.setDuration(3000);

53             imageview.setAnimation(animation);

54             break;

55         case R.id.button2:

56             // 旋转动画

57             animation = new RotateAnimation(0, 360);

58             animation.setDuration(3000);

59             imageview.setAnimation(animation);

60             break;

61         case R.id.button3:

62             // 位移动画

63             animation = new TranslateAnimation(0.1f, 1.0f, 1.0f, 100f);

64             animation.setDuration(3000);

65             imageview.setAnimation(animation);

66             break;

67         case R.id.button4:

68             // 缩放动画

69             animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);

70             animation.setDuration(3000);

71             imageview.setAnimation(animation);

72             break;

73         case R.id.button5:

74             // 多种动画

75             alphaanimation = new AlphaAnimation(0.1f, 1.0f);

76             translateanimation = new TranslateAnimation(0.1f, 1.0f, 1.0f, 100f);

77             animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);

78             // 定义一个动画集合

79             AnimationSet set = new AnimationSet(true);

80             set.addAnimation(alphaanimation);

81             set.addAnimation(animation);

82             set.addAnimation(translateanimation);

83             break;

84         default:

85             break;

86         }

87     }

88 }
Animation Java代码

你可能感兴趣的:(animation)