【Android基礎知識】【Android動畫】

項目來源:http://blog.csdn.net/super_spy/article/details/9700815java

這個資源簡直是沒法更讚了。android

 

這個動畫分析效果圖以下:app

 

源碼分析及註釋:ide

 

 

 

 

 

注:overridePendingTransition只支持android 2.0以上版本
Android的動畫效果分爲兩種,一種是tweened animation(補間動畫),第二種是frame by frame animation。通常咱們用的是第一種。補間動畫又分爲AlphaAnimation,透明度轉換 RotateAnimation,旋轉轉換 ScaleAnimation,縮放轉換 TranslateAnimation 位置轉換(移動)。
動畫效果在anim目錄下的xml文件中定義,在程序中用AnimationUtils.loadAnimation(Context context,int ResourcesId)載入成Animation對象,在須要顯示動畫效果時,執行須要動畫的View的startAnimation方法,傳入Animation,便可。切換Activity也能夠應用動畫效果,在startActivity方法後,執行overridePendingTransition方法,兩個參數分別是切換前的動畫效果,切換後的動畫效果,下面的例子中傳入的是兩個alpha動畫,以實現切換Activity時淡出淡入,漸隱漸現效果。
下面貼出代碼:
兩個Activity的佈局文件 main.xml:源碼分析

1佈局

2動畫

3this

4spa

5.net

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:id="@+id/ll"

    android:background="@drawable/white"

    >

<TextView  android:id="@+id/tv"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="translate動畫效果"

    />

<TextView  android:id="@+id/tv2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="scale動畫效果"

    />

<TextView  android:id="@+id/tv3"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="alpha動畫效果"

    />

<TextView  android:id="@+id/tv4"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="rotate動畫效果"

    />

<Button android:id="@+id/bt3"

          android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="動畫演示"

/>

<Button android:id="@+id/bt"

          android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="切換"

/>

</LinearLayout>

activity2.xml:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:id="@+id/ll2"

    >

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="Activity2"

    />

<Button android:id="@+id/bt2"

          android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="返回main"

/>

</LinearLayout>

動畫效果XML文件,所有存放在anim目錄下:
a1.xml 淡出效果

1

2

3

4

5

6

7

8

9

10

11

12

13

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

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

         <alpha

         android:fromAlpha="1.0"

         android:toAlpha="0.0"

         android:duration="500"

         />

</set>

<!--

fromAlpha:開始時透明度

toAlpha:結束時透明度

duration:動畫持續時間

 -->

a2.xml 淡入效果:

1

2

3

4

5

6

7

8

<?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:duration="500"

         />

</set>

rotate.xml 旋轉效果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

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

<rotate

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromDegrees="300"

android:toDegrees="-360"

android:pivotX="10%"

android:pivotY="100%"

android:duration="10000" />

</set>

<!--

fromDegrees開始時的角度

toDegrees動畫結束時角度

pivotX,pivotY不太清楚,看效果應該是定義旋轉的圓心的

 -->

scale.xml 縮放效果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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

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

          <scale 

     android:interpolator= "@android:anim/decelerate_interpolator"   

     android:fromXScale="0.0"   

     android:toXScale="1.5"   

     android:fromYScale="0.0"   

     android:toYScale="1.5"   

     android:pivotX="50%"   

     android:pivotY="50%"   

     android:startOffset="0"   

     android:duration="10000"

     android:repeatCount="1" 

     android:repeatMode="reverse"

     />

</set>

<!--

interpolator指定動畫插入器,常見的有加速減速插入器accelerate_decelerate_interpolator,加速插入器accelerate_interpolator,減速插入器decelerate_interpolator

fromXScale,fromYScale,動畫開始前X,Y的縮放,0.0爲不顯示,1.0爲正常大小

toXScaletoYScale,動畫最終縮放的倍數,1.0爲正常大小,大於1.0放大

pivotXpivotY動畫起始位置,相對於屏幕的百分比,兩個都爲50%表示動畫從屏幕中間開始

startOffset,動畫屢次執行的間隔時間,若是隻執行一次,執行前會暫停這段時間,單位毫秒

duration,一次動畫效果消耗的時間,單位毫秒,值越小動畫速度越快

repeatCount,動畫重複的計數,動畫將會執行該值+1

repeatMode,動畫重複的模式,reverse爲反向,當第偶次執行時,動畫方向會相反。restart爲從新執行,方向不變

 -->

translate.xml 移動效果:

1

2

3

4

5

6

7

8

9

10

11

12

13

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

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

<translate

android:fromXDelta="320"

android:toXDelta="0"

android:fromYDelta="480"

android:toYDelta="0"

android:duration="10000" />

</set>

<!--

fromXDelta,fromYDelta起始時XY座標,屏幕右下角的座標是X:320,Y:480

toXDeltatoYDelta動畫結束時X,Y的座標

 -->

下面是程序代碼,main.java:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

package com.pocketdigi.animation;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.Button;

import android.widget.TextView;

 

public class main extends Activity {

    /** Called when the activity is first created. */

         TextView tv,tv2,tv3,tv4;

         Button bt3;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button bt=(Button)findViewById(R.id.bt);

        tv=(TextView)findViewById(R.id.tv);

        tv2=(TextView)findViewById(R.id.tv2);

        tv3=(TextView)findViewById(R.id.tv3);

        tv4=(TextView)findViewById(R.id.tv4);

 

 

        bt3=(Button)findViewById(R.id.bt3);

        bt.setOnClickListener(new OnClickListener(){

 

                          @Override

                          public void onClick(View v) {

                                   // TODO Auto-generated method stub

                                   Intent intent=new Intent(main.this,activity2.class);

                                   startActivity(intent);

                                   overridePendingTransition(R.anim.a2,R.anim.a1);

                                   //淡出淡入動畫效果

                          }

 

        });

        bt3.setOnClickListener(new OnClickListener(){

 

                          @Override

                          public void onClick(View v) {

                                   // TODO Auto-generated method stub

                         Animation translate=AnimationUtils.loadAnimation(main.this, R.anim.translate);

                         Animation scale=AnimationUtils.loadAnimation(main.this, R.anim.scale);

                         Animation rotate=AnimationUtils.loadAnimation(main.this, R.anim.rotate);

                         Animation alpha=AnimationUtils.loadAnimation(main.this, R.anim.a1);

                         //載入XML文件成Animation對象

                         tv.startAnimation(translate);

                         tv2.startAnimation(scale);

                         tv3.startAnimation(alpha);

                         tv4.startAnimation(rotate);

                         //應用動畫

 

                          }});

    }

}

activity2.java:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

package com.pocketdigi.animation;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

 

public class activity2 extends Activity {

         Button bt2;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity2);

        bt2=(Button)findViewById(R.id.bt2);

        bt2.setOnClickListener(new OnClickListener(){

 

                          @Override

                          public void onClick(View v) {

                                   // TODO Auto-generated method stub

                                   Intent intent=new Intent(activity2.this,main.class);

                                   startActivity(intent);

                                   overridePendingTransition(R.anim.a2,R.anim.a1);

                          }

 

        });

    }

}

注:動畫切換Activity只有在新啓動Activity纔有效,若是Activity已經啓動,而且intent加了FLAG_ACTIVITY_REORDER_TO_FRONT,這樣不會新啓動Activity,也就沒有動畫效果。

相關文章
相關標籤/搜索