1. LayoutAnimationController usage
What is LayoutAnimationController?
1. LayoutAnimationController is used as a control in layout or
Set animation effects for controls in ViewGroup
2. Each control has the same animation effect.
3. The animation effects of these controls are displayed at different times.
4. LayoutAnimationController can be set in the xml file or in the code.
2. Combination of ListView and Animations
Use LayoutAnimationController in the code
1. Create an Animation object:
You can create an Animation object by loading an xml file or directly using the Animation constructor.
2. Use the following code to create a LayoutAnimationController object:
LayoutAnimationController lac = new LayoutAnimationController
(Animation );
3. Set the display sequence of controls:
Lac. serOrder (LayoutAnimationController. ORDER_NORMAL)
4. Set the LayoutAnimationController attribute for ListView:
ListView. setLayoutAnimation (lac );
MainActivity. java
Package com. yx. animations04;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import com. yx. animations04.R;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. app. ListActivity;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. animation. AccelerateDecelerateInterpolator;
Import android. view. animation. AlphaAnimation;
Import android. view. animation. Animation;
Import android. view. animation. AnimationSet;
Import android. view. animation. AnimationUtils;
Import android. view. animation. LayoutAnimationController;
Import android. view. animation. RotateAnimation;
Import android. view. animation. ScaleAnimation;
Import android. view. animation. TranslateAnimation;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. ListAdapter;
Import android. widget. ListView;
Import android. widget. SimpleAdapter;
Public class MainActivity extends ListActivity {
Private Button button = null;
Private ListView listView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Button = (Button) findViewById (R. id. buttonId );
Button. setOnClickListener (new buttonListener ());
ListView = getListView ();
}
Private ListAdapter buildListAdapter (){
List > List = new ArrayList > ();
HashMap M1 = new HashMap ();
M1.put ("name", "James ");
M1.put ("gender", "female ");
HashMap M2 = new HashMap ();
M2.put ("name", "Li Si ");
M2.put ("gender", "female ");
HashMap M3 = new HashMap ();
M3.put ("name", "Wang Wu ");
M3.put ("gender", "male ");
List. add (m1 );
List. add (m2 );
List. add (m3 );
SimpleAdapter simpleAdapter = new SimpleAdapter (MainActivity. this, list,
R. layout. item, new String [] {"name", "gender"}, new int [] {R. id. name, R. id. gender });
Return simpleAdapter;
}
Private class buttonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
ListView. setAdapter (buildListAdapter ());
// Implemented in the Code. The list_anim_layout.xml file is not required here.
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. list_anim );
LayoutAnimationController lac = new LayoutAnimationController (animation );
Lac. setOrder (LayoutAnimationController. ORDER_NORMAL );
ListView. setLayoutAnimation (lac );
}
}
}
List_anim_layout.xml
Android: delay= "0.5"
Android: animationOrder = "normal"
Android: animation = "@ anim/list_anim"
>
List_anim.xml
Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: toAlpha = "0.0"
Android: duration = "2000">
Activity_main.xml
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical"
Tools: context = ". MainActivity">
Android: id = "@ id/android: list"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: scrollbars = "vertical"
>
Android: id = "@ + id/buttonId"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: text = "test animation effect"
/>
Item. xml
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "horizontal">
Android: id = "@ + id/name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
Android: id = "@ + id/gender"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>