android Animations 动画效果(四)

1.LayoutAnimationController的使用方法
什么是LayoutAnimationController
1.LayoutAnimationController用于为一个layout里面的控件,或者是一个

ViewGroup里面的控件设置动画效果
2.每一个控件都有相同的动画效果
3.这些控件的动画效果在不同的时间显示出来
4.LayoutAnimationController可以在xml文件中设置,也可以在代码中设置


2.ListView与Animations结合使用
在代码中使用LayoutAnimationController
1.创建一个Animation对象:
可以通过装载xml文件,或者直接使用Animation的构造函数创建Animation对象
2使用如下代码创建LayoutAnimationController对象:
LayoutAnimationController lac = new LayoutAnimationController

(animation);
3.设置控件显示顺序:
lac.serOrder(LayoutAnimationController.ORDER_NORMAL)
4.为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(lac);



MainActivity.java

package com.yx.animations04;


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

import com.yx.animations04.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {

    private Button button=null;
    private ListView listView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button = (Button) findViewById(R.id.buttonId);
        button.setOnClickListener(new buttonListener());
        
        listView = getListView();
    }

    private ListAdapter buildListAdapter(){
        List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        HashMap<String,String> m1 = new HashMap<String, String>();
        m1.put("name", "张三");
        m1.put("gender","女");
        
        HashMap<String,String> m2 = new HashMap<String, String>();
        m2.put("name", "李四");
        m2.put("gender","女");
        
        HashMap<String,String> m3 = new HashMap<String, String>();
        m3.put("name", "王五");
        m3.put("gender","男");
        
        list.add(m1);
        list.add(m2);
        list.add(m3);
        
        SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, list,
                R.layout.item, new String[]{"name","gender"}, new int[]{R.id.name,R.id.gender});
        return simpleAdapter;
    }
    
    private class buttonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            listView.setAdapter(buildListAdapter());
            
            //在代码中实现,这里不需要list_anim_layout.xml文件
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
            LayoutAnimationController lac = new LayoutAnimationController(animation);
            lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
            listView.setLayoutAnimation(lac);
        }
    }
}


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>

list_anim.xml

<?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="1.0"
        android:toAlpha="0.0"
        android:duration="2000"></alpha>
</set>

activity_main.xml
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <!-- 使用配置文件
    <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"
        ></ListView>
     -->
     
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        ></ListView>
    
    <Button
        android:id="@+id/buttonId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        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/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>



android Animations 动画效果(四),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。