【Android】學習筆記(12)——Animation使用方法(下)

在上一篇學習筆記中,在AnimationSet中的Animation的效果是疊加的,我們對AnimationSet設置執行時間和延遲動畫等屬性後,整個AnimationSet內的動畫都是被設置了這些屬性,所以,在AnimationSet中的Animation的效果是疊加的。如果想要這些動畫效果不疊加,則需要進一步設置。

這裏要使用到LayoutAnimationController。這個類可以用在一個佈局文件中的layout內,對該layout內部的控件進行控制,也可以用在Java代碼中,實現同樣的效果。

1.在XML中使用LayoutAnimationController

首先在res/anim文件夾下創建新的文件list_controller.xml
其內容爲:
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <layoutAnimationxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:delay="0.5"//每個條目之間相隔是0.5秒

  4. android:animationOrder="normal"//條目顯示的順序,有normal,random,reverse,分別表示順序,隨機,反序

  5. android:animation="@anim/alpha"//每個條目使用alpha.xml內的動畫效果

  6. />

這個文件用來控制ListView內每個條目動畫的順序和所要執行的效果。
其中的alpha.xml的動畫效果爲:
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <setxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:interpolator="@android:anim/accelerate_interpolator"

  4. >

  5. <alpha

  6. android:fromAlpha="0.0"

  7. android:toAlpha="1.0"

  8. android:duration="2000"

  9. ></alpha>

  10. </set>


在main.xml的佈局文件中,我們要對ListView的控件進行設置動畫,如下:
  1. <?xmlversion="1.0"encoding="utf-8"?>

  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:orientation="vertical"

  4. android:layout_width="fill_parent"

  5. android:layout_height="fill_parent"

  6. >

  7. <ListView

  8. android:layout_height="wrap_content"

  9. android:layout_width="fill_parent"

  10. android:id="@id/android:list"

  11. android:layoutAnimation="@anim/list_controller"//設置動畫效果,引用的是list_controller.xml文件

  12. />

  13. </LinearLayout>

在Java代碼中,是一個以前寫的ListView的代碼,如下:
  1. package cxt.demo;

  2. import java.util.ArrayList;

  3. import java.util.HashMap;

  4. import android.app.ListActivity;

  5. import android.os.Bundle;

  6. import android.widget.SimpleAdapter;

  7. publicclass AnimationDemo3Activity extends ListActivity {

  8. private ArrayList<HashMap<String,Object>> list = null;

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

  10. @Override

  11. publicvoid onCreate(Bundle savedInstanceState) {

  12. super.onCreate(savedInstanceState);

  13. setContentView(R.layout.main);

  14. list = new ArrayList<HashMap<String,Object>>();

  15. HashMap<String,Object> m1 = new HashMap<String, Object>();

  16. HashMap<String,Object> m2 = new HashMap<String, Object>();

  17. HashMap<String,Object> m3 = new HashMap<String, Object>();

  18. m1.put("image", R.drawable.z11);

  19. m1.put("name", "Jack");

  20. m1.put("age","63");

  21. m2.put("image", R.drawable.z22);

  22. m2.put("name", "Bob");

  23. m2.put("age","15");

  24. m3.put("image", R.drawable.z33);

  25. m3.put("name", "Theron");

  26. m3.put("age","25");

  27. list.add(m1);

  28. list.add(m2);

  29. list.add(m3);

  30. SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"image","name","age"}, newint[]{R.id.image,R.id.name,R.id.age});

  31. setListAdapter(adapter);

  32. }

  33. }

上述的Java代碼是使用SimpleAdapter的ListView,在之前的學習筆記中已經提到過了。
看一下運行效果:

232141902.png

上面的圖是運行過程中的截圖,Jack已經顯示,Bob正在漸漸顯示,第3個Theron還木有顯示出來。這樣,就可以實現不同控件的不同順序的動畫顯示了。

2.在Java代碼中使用LayoutAnimationController

首先我們不再需要上面的list_controller.xml文件了,因爲文件中的內容我們將移至Java代碼中,同時,main.xml中的ListView控件上也不用設置android:layoutAnimation屬性了。
只需要在Java代碼後加上如下代碼:
  1. Animation animation = AnimationUtils.loadAnimation(AnimationDemo4Activity.this, R.anim.alpha);//創建一個Animation對象,設置動畫效果

  2. LayoutAnimationController controller = new LayoutAnimationController(animation);//創建LayoutAnimationController對象

  3. controller.setOrder(LayoutAnimationController.ORDER_NORMAL);//設置條目顯示的順序,這裏也有3種順序,ORDER_NORMAL,ORDER_RANDOM,ORDER_REVERSE

  4. listView.setLayoutAnimation(controller);//將LayoutAnimationController對象設置給ListView,就OK了。

運行效果和上面的效果一模一樣。
補充:
AnimationListener,我們可以爲Animation對象設置監聽器。
在上述的所有動畫結束後,會回到原來的狀態,如果一張圖片,我們想要它漸漸消失,然後就不希望它再出現了;或者是某個控件從無到有,想讓這個空間就停在結束的地方。那麼就要給Animation設置一個監聽器,代碼如下

  1. privateclass AnimationImpl implements AnimationListener{

  2. @Override

  3. publicvoid onAnimationEnd(Animation arg0) {

  4. // TODO Auto-generated method stub

  5. //這裏是這個動畫效果結束後,會運行的方法

  6. //如果想讓某個控件消失,可以在這裏寫代碼

  7. }

  8. @Override

  9. publicvoid onAnimationRepeat(Animation arg0) {

  10. // TODO Auto-generated method stub

  11. //這裏是這個動畫效果重複運行時,會運行的方法

  12. }

  13. @Override

  14. publicvoid onAnimationStart(Animation arg0) {

  15. // TODO Auto-generated method stub

  16. //這裏是這個動畫效果開始時,會運行的方法

  17. }

  18. }

Frame-By-Frame Animations

Frame-By-Frame Animations是另一類Animations,和Tweened Animations不同。

它其實是多張圖片不斷切換,形成的動畫效果,個人覺得實際應用性不強。
實現方法:
首先需要幾張圖片,我們要用這些圖片組成動畫效果,在res/drawable類的文件夾,建立一個xml文件,內容如下:
  1. <?xmlversion="1.0"encoding="UTF-8"?>

  2. <animation-listxmlns:android="http://schemas.android.com/apk/res/android"

  3. android:oneshot="false">

  4. <itemandroid:drawable="@drawable/p1"android:duration="500"></item>

  5. <itemandroid:drawable="@drawable/p2"android:duration="500"></item>

  6. <itemandroid:drawable="@drawable/p3"android:duration="500"></item>

  7. <itemandroid:drawable="@drawable/p4"android:duration="500"></item>

  8. </animation-list>

很簡單,就是創建一個animation-list,內部是我們的圖的資源和顯示的時間。
Java文件中的代碼:
  1. publicclass AnimationDemo5Activity extends Activity {

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

  3. Button start = null;//佈局文件中定義了一個Button控件

  4. ImageView imageView = null;//佈局文件中定義了一個ImageView控件

  5. @Override

  6. publicvoid onCreate(Bundle savedInstanceState) {

  7. super.onCreate(savedInstanceState);

  8. setContentView(R.layout.main);

  9. imageView = (ImageView)findViewById(R.id.image);

  10. start = (Button)findViewById(R.id.start);

  11. start.setOnClickListener(new OnClickListener() {

  12. @Override

  13. publicvoid onClick(View arg0) {

  14. // TODO Auto-generated method stub

  15. imageView.setBackgroundResource(R.drawable.move);//爲ImageView對象設置圖片的資源

  16. AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();//創建AnimationDrawable對象

  17. animationDrawable.start();//啓動動畫

  18. }

  19. });

  20. }

  21. }


以上是Frame-By-Frame Animations的簡單使用方法。

附件是上面3個例子的示例代碼,僅供參考。

如果我的文章給與了你幫助,就不妨請我喝杯咖啡吧,點擊->btn-index.png


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章