Android开发UI之给ListView设置布局动画效果

1.通过JAVA代码添加,资源文件基本上不修改

XML文件,只添加了一个ListView,就不贴XML文件的代码了。

java代码:

 1 public class MainActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7 
 8         ScaleAnimation sa=new ScaleAnimation(0, 1, 0, 1);
 9         sa.setDuration(1000);
10         LayoutAnimationController lac=new LayoutAnimationController(sa, 0.5f);
11         lac.setOrder(LayoutAnimationController.ORDER_REVERSE);
12 
13          ListView lv=(ListView) findViewById(R.id.lv);
14          ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,new String[]{"北京","西安","上海","南京"});
15          lv.setAdapter(adapter);
16          lv.setLayoutAnimation(lac);
17         }
18 }

 

2.通过资源文件添加动画效果

新建一个Android XML fils,在新建页面,Resource Type选择为Tween Animation,Root element选择为alpha。(也可以设置多个动画效果一起)

scale_anim.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <scale xmlns:android="http://schemas.android.com/apk/res/android"
3     android:fromXScale="0"
4     android:toXScale="1"
5     android:fromYScale="0"
6     android:toYScale="1"
7     android:duration="1000">
8 </scale>

新建一个Android XML fils

listview_anim.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
3     android:animation="@anim/scale_anim"
4     android:delay="0.5">    
5 </layoutAnimation>

在main-activity.xml文件中,设置listview的属性

1     <ListView android:id="@+id/lv"
2         android:layout_width="match_parent"
3         android:layout_height="match_parent"
4         android:layoutAnimation="@anim/listview_anim"></ListView>

 

posted @ 2015-07-16 16:41  熠然  阅读(1125)  评论(0编辑  收藏  举报