在上一篇學習筆記中,在AnimationSet中的Animation的效果是疊加的,我們對AnimationSet設置執行時間和延遲動畫等屬性後,整個AnimationSet內的動畫都是被設置了這些屬性,所以,在AnimationSet中的Animation的效果是疊加的。如果想要這些動畫效果不疊加,則需要進一步設置。
1.在XML中使用LayoutAnimationController
<?xmlversion="1.0"encoding="UTF-8"?>
<layoutAnimationxmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"//每個條目之間相隔是0.5秒
android:animationOrder="normal"//條目顯示的順序,有normal,random,reverse,分別表示順序,隨機,反序
android:animation="@anim/alpha"//每個條目使用alpha.xml內的動畫效果
/>
<?xmlversion="1.0"encoding="UTF-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
></alpha>
</set>
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@id/android:list"
android:layoutAnimation="@anim/list_controller"//設置動畫效果,引用的是list_controller.xml文件
/>
</LinearLayout>
package cxt.demo;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
publicclass AnimationDemo3Activity extends ListActivity {
private ArrayList<HashMap<String,Object>> list = null;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> m1 = new HashMap<String, Object>();
HashMap<String,Object> m2 = new HashMap<String, Object>();
HashMap<String,Object> m3 = new HashMap<String, Object>();
m1.put("image", R.drawable.z11);
m1.put("name", "Jack");
m1.put("age","63");
m2.put("image", R.drawable.z22);
m2.put("name", "Bob");
m2.put("age","15");
m3.put("image", R.drawable.z33);
m3.put("name", "Theron");
m3.put("age","25");
list.add(m1);
list.add(m2);
list.add(m3);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"image","name","age"}, newint[]{R.id.image,R.id.name,R.id.age});
setListAdapter(adapter);
}
}
2.在Java代碼中使用LayoutAnimationController
Animation animation = AnimationUtils.loadAnimation(AnimationDemo4Activity.this, R.anim.alpha);//創建一個Animation對象,設置動畫效果
LayoutAnimationController controller = new LayoutAnimationController(animation);//創建LayoutAnimationController對象
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);//設置條目顯示的順序,這裏也有3種順序,ORDER_NORMAL,ORDER_RANDOM,ORDER_REVERSE
listView.setLayoutAnimation(controller);//將LayoutAnimationController對象設置給ListView,就OK了。
privateclass AnimationImpl implements AnimationListener{
@Override
publicvoid onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
//這裏是這個動畫效果結束後,會運行的方法
//如果想讓某個控件消失,可以在這裏寫代碼
}
@Override
publicvoid onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
//這裏是這個動畫效果重複運行時,會運行的方法
}
@Override
publicvoid onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
//這裏是這個動畫效果開始時,會運行的方法
}
}
Frame-By-Frame Animations是另一類Animations,和Tweened Animations不同。
<?xmlversion="1.0"encoding="UTF-8"?>
<animation-listxmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<itemandroid:drawable="@drawable/p1"android:duration="500"></item>
<itemandroid:drawable="@drawable/p2"android:duration="500"></item>
<itemandroid:drawable="@drawable/p3"android:duration="500"></item>
<itemandroid:drawable="@drawable/p4"android:duration="500"></item>
</animation-list>
publicclass AnimationDemo5Activity extends Activity {
/** Called when the activity is first created. */
Button start = null;//佈局文件中定義了一個Button控件
ImageView imageView = null;//佈局文件中定義了一個ImageView控件
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.image);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setBackgroundResource(R.drawable.move);//爲ImageView對象設置圖片的資源
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();//創建AnimationDrawable對象
animationDrawable.start();//啓動動畫
}
});
}
}
以上是Frame-By-Frame Animations的簡單使用方法。