Use of Animations in android Development (IV)
Use of Animations in android Development (IV)This blog post focuses on the use of animation in layout. This document uses the ListView control as an example.There are two methods to implement layout,The first is to directly use the layoutAnimation tag in the xml file.The second is to use the code implementation and the layoutAnimationController object,The Code is as follows:MainActivity. java:
Package com. example. animationtest4;Import java. util. ArrayList;
Import java. util. HashMap;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. animation. Animation;
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;
/*
* This class mainly implements the use of the Animation effect of Animation in the layout control.
* In this example, the anim_list animation effect is applied to the listview control.
*/
Public class MainActivity extends Activity {
Private Button testButton = null;
Private ListView listView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TestButton = (Button) findViewById (R. id. myButton );
ListView = (ListView) findViewById (R. id. myList );
TestButton. setOnClickListener (new buttonListener ());
}
Class buttonListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
// Use the Layout file to set the animation effect of the Layout Control
// View the layout_anim_list.xml File
// ListView. setAdapter (buildListAdapter ());
// When using an xml file, you must add the following statement to the corresponding layout control:
Android: layoutAnimation = "@ anim/layout_anim_list"
// Use code to set the animation effect of the layout Control
ListView. setAdapter (buildListAdapter ());
Animation animation = AnimationUtils. loadAnimation (MainActivity. this, R. anim. anim_list );
// Create the LayoutAnimationController object and set the latency to 0.5 s.
LayoutAnimationController lac = new LayoutAnimationController (animation, 0.5f );
// Set the display order to normal.
Lac. setOrder (LayoutAnimationController. ORDER_NORMAL );
ListView. setLayoutAnimation (lac );
}
}
// Create a listAdapter object
Private ListAdapter buildListAdapter (){
ArrayList > List = new ArrayList > ();
HashMap Map1 = new HashMap ();
HashMap Map2 = new HashMap ();
HashMap Map3 = new HashMap ();
Map1.put ("user", "James ");
Map1.put ("sex", "male ");
Map2.put ("user", "Li Si ");
Map2.put ("sex", "female ");
Map3.put ("user", "Wang Wu ");
Map3.put ("sex", "male ");
List. add (map1 );
List. add (map2 );
List. add (map3 );
SimpleAdapter adapter = new SimpleAdapter (MainActivity. this, list, R. layout. item, new String [] {"user", "sex"}, new int [] {R. id. user, R. id. sex });
Return adapter;
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
}
Anim_list.xml:
Android: interpolator = "@ android: anim/accelerate_interpolator">
Android: duration= "2000"
Android: fromalphi = "0.0"
Android: toAlpha = "1.0"/>
Layout _anim_list.xml:
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: delay= "0.5"
Android: animationOrder = "normal"
Android: animation = "@ anim/anim_list">
Main. xml:
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: paddingBottom = "@ dimen/activity_vertical_margin"
Android: paddingLeft = "@ dimen/activity_horizontal_margin"
Android: paddingRight = "@ dimen/activity_horizontal_margin"
Android: paddingTop = "@ dimen/activity_vertical_margin"
Tools: context = ". MainActivity">
Android: id = "@ + id/myText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
Android: id = "@ + id/myList"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/myText"
// Android: layoutAnimation = "@ anim/layout_anim_list"
/>
Android: id = "@ + id/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/myList"
Android: text = "test"/>
The layout file item. xml of the listview entry:
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "horizontal">
Android: id = "@ + id/user"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
Android: id = "@ + id/sex"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>