效果图:
平时我们要实现ListView数据显示时的动画效果,能够使用LayoutAnimationController为ListView设置动画效果,并通过ListView的setLayoutAnimation方法来设置,最后startLayoutAnimation,启动动画。參看下面代码:
public class MainActivity extends Activity { private ListView lv_arrayadapter; private String[] str_name = new String[] { "jack", "debb", "robin", "kikt", "dog", "cat", "elep" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setData(); loadAnimation(); } private void loadAnimation() { /* * 创建动画的集合 */ AnimationSet set = new AnimationSet(false); Animation animation ; /* * 创建旋转动画 */ animation = new RotateAnimation(180, 10); animation.setDuration(1000); set.addAnimation(animation); LayoutAnimationController controller = new LayoutAnimationController( set, 1); controller .setInterpolator(this, android.R.anim.accelerate_interpolator); controller.setAnimation(set); lv_arrayadapter.setLayoutAnimation(controller); lv_arrayadapter.startLayoutAnimation(); } private void initView() { lv_arrayadapter = (ListView) findViewById(R.id.lv_arrayadapter); // 注冊监听事件 lv_arrayadapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, str_name[position], Toast.LENGTH_SHORT).show(); } }); } private void setData() { // 创建ArrayAdapter ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, str_name); // 绑定适配器 lv_arrayadapter.setAdapter(arrayAdapter); } }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/lv_arrayadapter" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>