android学习——LayoutAnimationController 和 ViewGroup

1.main. xml 文件
显示 listview 是用渐入动画

    android:orientation=”vertical”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:id=”@+id/ly_view_group”
    >
   
android:id=”@+id/alpha_list”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:scrollbars=”vertical”

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

/>
android:id=”@+id/btn_add”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”添加”
/>
android:id=”@+id/btn_remove”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”移除”
/>
android:id=”@+id/img_face”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/n1″
android:layout_gravity=”center”
/>

item.xml   显示item 项的格式

    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”>

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

android:id=”@+id/user_gender”
android:layout_width=”180dip”
    android:layout_height=”30dip”
    android:textSize=”10pt”/>

定义一个  layoutAnimation        anim/list_anim_layout.xml

android:delay=”0.5″ 
android:animationOrder=”normal”
  android:animation=”@anim/list_anim” >

定义一个 动画 xml文件    anin/list_anin

        android:interpolator=”@android:anim/accelerate_interpolator”>

      android:toAlpha=”1.0″
    android:startOffset=”500″
    android:duration=”1000″
    />

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> list=new ArrayList>();

HashMap map1=new HashMap();
map1.put(“user_name”, “张三”);
map1.put(“user_gender”, “女”);
list.add(map1);

HashMap map2=new HashMap();
map2.put(“user_name”, “李四”);
map2.put(“user_gender”, “男”);
list.add(map2);

HashMap map3=new HashMap();
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;
}
}

       

运行前 图左     运行 后 图右

发表评论

电子邮件地址不会被公开。 必填项已用*标注

昵称 *