LayoutAnimationController--mars第2季第11集

一 、实现内容:在Activity中显示一个ListView,一行一行的显示出来(Alpha的效果)

如果我们需要一个界面中的多个控件按照相同的动画方式但是每个控件完成该动画的时刻不同的话,就可采用本节讲的LayoutAnimationController来方便的完成了


二、新知识点

1、如何使用ListView,如何添加list,使用ListView的getListView()方法,要继承ListActivity

2、如何用xml实现LayoutAnimationController? 下面步骤1、2

3,如何使用代码实现LayoutAnimationController?下面步骤3


三、实现步骤

1、在res/anim文件夹中创建一个list_anim.xml文件,设置动画效果;在创建一个list_anim_layout.xml,使用<layoutAnimation />标签,控制前面动画效果的执行

list_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true"
     xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha 
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="2000"
        />

</set>
list_anim_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:delay="0.5"
    android:animationOrder="normal"            //normal是顺序执行。random是随机执行
    android:animation="@anim/list_anim"/>      //关联上list_anim.xml,控制动画效果的执行



2、在layout文件夹下创建一个activity_main.xml,里面的<ListView/>标签里要关联上前面的动画执行文件  android:laytoutAnimation="@anim/list_anim_layout"; 还要创建一个item.xml,用于设置listView里List的样式

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"                                 //match_parent和fill_parent一样效果
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"                             //
        android:layoutAnimation="@anim/list_anim_layout"         //关联上list_anim_layout.xml

        />
    <Button 
        android:id="@+id/buttonId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试"
        />

</LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView 
        android:id="@+id/user_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
    <TextView 
        android:id="@+id/user_gender"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
         android:layout_weight="1"
        />

</LinearLayout>



3、在MainActivity.java里,MainActivity要继承ListActivity,(这样才能使用getListView()方法,

package com.example.s02_e11_animationlayoutcontroller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
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;

public class MainActivity extends ListActivity {
private ListView listview;
private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		listview=getListView();
		
		button=(Button)findViewById(R.id.buttonId);
		button.setOnClickListener(new ButtonListener());
			
	}
	
//新建ListView里要显示的列表
	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", "玉华");
		m1.put("user_gender", "女");
		
		HashMap<String,String> m2=new HashMap<String,String>();
		m2.put("user_name", "吉轩");
		m2.put("user_gender", "男");
		
		HashMap<String,String> m3=new HashMap<String,String>();
		m3.put("user_name", "葫芦");
		m3.put("user_gender", "男");
		
		list.add(m1);
		list.add(m2);
		list.add(m3);
		//创建SimpleAdapter对象,关联上列表
		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;
	
	}

	class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
		//给listView设置前面创建的adapter
		listview.setAdapter(buildListAdapter());
		//通过代码设置LayoutAnimationController的方法(下面1、2两步)
		//1在activity_main的ListView标签下去掉android:layoutAnimation="@anim/list_anim_layout"
		//2加入下面代码
		/*Animation animation=(Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
		LayoutAnimationController lac=new LayoutAnimationController(animation);
		lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
		listview.setLayoutAnimation(lac);*/
		
		}
		
	}
	@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;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值