A. normal animation:
can be set by code or set by XML . Use animationSet and XXXanimation ,very easy:
package com.animation_test_02; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.*; import android.widget.Button; import android.widget.ImageView; public class AnimationTest02 extends Activity { /** Called when the activity is first created. */ private Button scaleBtn; private Button rotateBtn; private Button translateBtn; private Button alphaBtn; private Button animationSetBtn; private ImageView imgView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); scaleBtn=(Button)this.findViewById(R.id.scaleButtonId); rotateBtn=(Button)this.findViewById(R.id.rotateButtonId); translateBtn=(Button)this.findViewById(R.id.translateButtonId); alphaBtn=(Button)this.findViewById(R.id.alphaButtonId); animationSetBtn=(Button)this.findViewById(R.id.animationSetButtonId); imgView=(ImageView)this.findViewById(R.id.imageViewId); scaleBtn.setOnClickListener(clickListener); rotateBtn.setOnClickListener(clickListener); translateBtn.setOnClickListener(clickListener); alphaBtn.setOnClickListener(clickListener); animationSetBtn.setOnClickListener(clickListener); } private View.OnClickListener clickListener=new View.OnClickListener(){ public void onClick(View v) { // throw new UnsupportedOperationException("Not supported yet."); if(v.getId()==R.id.scaleButtonId){ //code method // // // Log.i("l","scale click"); // AnimationSet animationSet=new AnimationSet(true); // ScaleAnimation scaleAnimation=new ScaleAnimation( // // x direction from to // 1,0.1f, // // y direction from to // 1,0.1f, // //axis x coord // Animation.RELATIVE_TO_SELF,0.5f, // //axis y coord // Animation.RELATIVE_TO_SELF,0.5f); // scaleAnimation.setDuration(2000); // // animationSet.addAnimation(scaleAnimation); // // //if back to the original state; // animationSet.setFillBefore(false); // animationSet.setFillAfter(true); // // //delay how long to start // animationSet.setStartOffset(1000); // // //set the repeat count // animationSet.setRepeatCount(10); // // imgView.startAnimation(animationSet); //XML method Animation animation=AnimationUtils.loadAnimation(AnimationTest02.this, R.anim.scale); imgView.startAnimation(animation); } else if(v.getId()==R.id.rotateButtonId){ //code method // // Log.i("l","rotate click"); // AnimationSet animationSet=new AnimationSet(true); // RotateAnimation rotateAnimation=new RotateAnimation( // //angle from to // 0,360, // //axis x coords (0-1) // Animation.RELATIVE_TO_SELF,0.5f, // //axis y coords (0-1) // Animation.RELATIVE_TO_SELF,0.5f); // rotateAnimation.setDuration(2000); // animationSet.addAnimation(rotateAnimation); // // imgView.startAnimation(animationSet); //XML method Animation animation=AnimationUtils.loadAnimation(AnimationTest02.this, R.anim.rotate); imgView.startAnimation(animation); } else if(v.getId()==R.id.translateButtonId){ //code method // Log.i("l","translate click"); // AnimationSet animationSet=new AnimationSet(true); // TranslateAnimation translateAnimation=new TranslateAnimation( // //x, from // Animation.RELATIVE_TO_SELF,0, // //x, to // Animation.RELATIVE_TO_SELF,0.5f, // //y from // Animation.RELATIVE_TO_SELF,0, // //y to // Animation.RELATIVE_TO_SELF,1); // translateAnimation.setDuration(2000); // animationSet.addAnimation(translateAnimation); // // imgView.startAnimation(animationSet); //XML method Animation animation=AnimationUtils.loadAnimation(AnimationTest02.this, R.anim.translate); imgView.startAnimation(animation); } else if(v.getId()==R.id.alphaButtonId){ //code method // Log.i("l","alpha click"); // AnimationSet animationSet=new AnimationSet(true); // AlphaAnimation alphaAnimation=new AlphaAnimation(1,0); // alphaAnimation.setDuration(1000); // animationSet.addAnimation(alphaAnimation); // // imgView.startAnimation(animationSet); //xml method Animation animation=AnimationUtils.loadAnimation(AnimationTest02.this, R.anim.alpha); imgView.startAnimation(animation); } else if(v.getId()==R.id.animationSetButtonId){ //xml method Animation animationSet=AnimationUtils.loadAnimation(AnimationTest02.this, R.anim.set); animationSet.setInterpolator(new DecelerateInterpolator()); imgView.startAnimation(animationSet); //frame animation // imgView.setBackgroundResource(R.drawable.anim_list); // AnimationDrawable animationDrawable=(AnimationDrawable)imgView.getBackground(); // animationDrawable.start(); } } }; }
if use XML , new an anim folder in res
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="50%" android:toXDelta="100%" android:fromYDelta="50%p" android:toYDelta="50%p" android:duration="2000" /> <alpha android:fromAlpha="1" android:toAlpha="0" android:startOffset="500" android:duration="500" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="1" android:toAlpha="0" android:startOffset="500" android:duration="500" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="3000" /> <!-- android:pivotX="50" absolute android:pivotX="50%" child android:pivotV="50%p" parent--> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <scale android:fromXScale="1" android:toXScale="0" android:fromYScale="1" android:toYScale="0" android:pivotX="50%" android:pivotY="50%" android:duration="2000" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="50%" android:toXDelta="100%" android:fromYDelta="50%p" android:toYDelta="50%p" android:duration="2000" /> </set>
B. much more important , the LayoutAnimation , LayoutAnimationCOntroller , and AnimationListener
1. if use XML to set layoutAnimation , no need to use LayoutAnimationController.
set an layoutAnimation,and indicate the animation set in it;
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/list_anim" />
<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000"/> </set>
2. iif use LayoutAnimationController, no need to indicate the layoutAnimation.xml in the res/anim
just new it in the code :
Animation animation=(Animation)AnimationUtils.loadAnimation( AnimationTest03.this, R.anim.list_anim); animation.setAnimationListener(animationListener); //pass the animation item to the lac; LayoutAnimationController lac= new LayoutAnimationController(animation); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); lac.setDelay(0.1f); listView.setLayoutAnimation(lac);
note that how to convert an animationSet XML to an animation instance by AnimationUtils:
Animation animation=(Animation)AnimationUtils.loadAnimation( AnimationTest03.this, R.anim.list_anim);
3. animationListener, very simple:
animation.setAnimationListener(animationListener);
Full Code:
package com.animation_test_03; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.view.animation.LayoutAnimationController; import android.widget.Button; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class AnimationTest03 extends ListActivity { private Button btn; private ListView listView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView=this.getListView(); btn=(Button)this.findViewById(R.id.buttonId); btn.setOnClickListener(clickListener); } private OnClickListener clickListener=new OnClickListener(){ public void onClick(View arg0) { // throw new UnsupportedOperationException("Not supported yet."); listView.setAdapter(buildListAdapter()); //convert an id into a animation item; Animation animation=(Animation)AnimationUtils.loadAnimation( AnimationTest03.this, R.anim.list_anim); animation.setAnimationListener(animationListener); //pass the animation item to the lac; LayoutAnimationController lac= new LayoutAnimationController(animation); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); lac.setDelay(0.1f); listView.setLayoutAnimation(lac); } }; private AnimationListener animationListener=new AnimationListener(){ public void onAnimationStart(Animation arg0) { // throw new UnsupportedOperationException("Not supported yet."); Log.i("l","animation start"); } public void onAnimationEnd(Animation arg0) { // throw new UnsupportedOperationException("Not supported yet."); // listView.setAdapter(null); Log.i("l","animation end"); Animation animation=(Animation)AnimationUtils.loadAnimation( AnimationTest03.this, R.anim.list_anim); animation.setAnimationListener(animationListener); //pass the animation item to the lac; LayoutAnimationController lac= new LayoutAnimationController(animation); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); lac.setDelay(0.1f); listView.setLayoutAnimation(lac); } public void onAnimationRepeat(Animation arg0) { // throw new UnsupportedOperationException("Not supported yet."); Log.i("l","animation repeat"); } }; private ListAdapter buildListAdapter(){ List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>(); HashMap<String,String> m1=new HashMap<String,String>(); m1.put("user_name","Zhangsan"); m1.put("user_gender","female"); HashMap<String,String> m2=new HashMap<String,String>(); m2.put("user_name","Lisi"); m2.put("user_gender","female"); HashMap<String,String> m3=new HashMap<String,String>(); m3.put("user_name","Wanfwu"); m3.put("user_gender","male"); list.add(m1); list.add(m2); list.add(m3); SimpleAdapter simpleAdapter=new SimpleAdapter( this, list, R.layout.item, new String[]{"user_name","user_gender"}, new int[]{R.id.user_name,R.id.user_gender}); return simpleAdapter; } }
<?xml version="1.0" encoding="UTF-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="2000"/> </set>
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/list_anim" />
<?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" > <!-- <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:layoutAnimation="@anim/list_anim_layout" />--> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> <Button android:id="@+id/buttonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="test" /> </LinearLayout>