android学习——LayoutAnimationController 和 ViewGroup

来源:互联网 发布:网络分为几种类型 编辑:程序博客网 时间:2024/06/05 19:31
1.main. xml 文件
显示 listview 是用渐入动画
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/ly_view_group"
    >
    <!-- android:layoutAnimation="@anim/list_anim_layout" -->
<ListView 
android:id="@+id/alpha_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"


/>
<Button 
android:id="@+id/btn_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示list"


/>
<Button 
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加"
/>
<Button 
android:id="@+id/btn_remove"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="移除"
/>
<ImageView 
android:id="@+id/img_face"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/n1"
android:layout_gravity="center"
/>
</LinearLayout>


item.xml   显示item 项的格式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="2dip"
    android:paddingBottom="2dip">

<TextView
android:id="@+id/user_name"
android:layout_width="180dip"
    android:layout_height="30dip"
    android:textSize="10pt"/>

<TextView
android:id="@+id/user_gender"
android:layout_width="180dip"
    android:layout_height="30dip"
    android:textSize="10pt"/>
</LinearLayout>


定义一个  layoutAnimation        anim/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"
  android:animation="@anim/list_anim" >
</layoutAnimation>


定义一个 动画 xml文件    anin/list_anin
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/accelerate_interpolator">


  <alpha android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:startOffset="500"
    android:duration="1000"
    />
</set>


java 文件
package demo.alphalist;


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


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class AlphaLis extends Activity {

private ListView listView;
private ImageView imageView;
private ViewGroup viewGroup;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //得到ImageView
        imageView=(ImageView)findViewById(R.id.img_face);
        viewGroup=(ViewGroup)findViewById(R.id.ly_view_group);
        
        listView=(ListView)findViewById(R.id.alpha_list);
        
        findViewById(R.id.btn_show).setOnClickListener(new ShowListButtonClickListener());
        findViewById(R.id.btn_remove).setOnClickListener(new RemoveButtonListener());
        findViewById(R.id.btn_add).setOnClickListener(new AddveButtonListener());


    }
    
    class ShowListButtonClickListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
listView.setAdapter(buildListAdapter());

Animation animation = (Animation) AnimationUtils.loadAnimation(
AlphaLis.this, R.anim.list_anim);
LayoutAnimationController lac=new LayoutAnimationController(animation);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);
}    
    }
    
    private class AddveButtonListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation animation=new AlphaAnimation(0,1);
animation.setDuration(1000);
animation.setStartOffset(500);

ImageView addImageView = new ImageView(AlphaLis.this);
//設置圖片
addImageView.setImageResource(R.drawable.n2);
//添加imageview 并设置参数
viewGroup.addView(addImageView, new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


addImageView.startAnimation(animation);
}
   
    }
    private class RemoveButtonListener implements OnClickListener{


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Animation animation =new AlphaAnimation(1, 0);
//设置动画持续时间
animation.setDuration(1000);
//设置动画偏移量
animation.setStartOffset(500);
//为animation设置监听事件
animation.setAnimationListener(new RemoveAnimationListener());
imageView.startAnimation(animation);
}
   
    }
    
    
    private class RemoveAnimationListener implements AnimationListener{


@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
viewGroup.removeView(imageView);
}


@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}


@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}
   
    }
    
    private ListAdapter buildListAdapter() {
List<Map<String,String>> list=new ArrayList<Map<String, String>>();

HashMap<String, String> map1=new HashMap<String, String>();
map1.put("user_name", "张三");
map1.put("user_gender", "女");
list.add(map1);

HashMap<String, String> map2=new HashMap<String, String>();
map2.put("user_name", "李四");
map2.put("user_gender", "男");
list.add(map2);

HashMap<String, String> map3=new HashMap<String, String>();
map3.put("user_name", "小三");
map3.put("user_gender", "女");
list.add(map3);

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;
}
}

       

运行前 图左     运行 后 图右