LayoutAnimationController
什么是LayoutAnimationController?
1.LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果;
2.让每一个控件都有相同的动画效果;
3.可以让这些控件的动画效果在不同的时间显示出来;
4.LayoutAnimationController可以在xml文件当中设置,也可以在代码当中进行设置;
在XML当中使用LayoutAnimationController:
1.在res/anim文件夹中创建两个xml文件,分别用来设置动画效果和布局动画效果
2.新建layout文件设置条目显示格式并在activity_main主布局文件中添加ListView标签
3.在ListView的标签中设置layoutAnimation属性为新建的布局动画效果xml文件
4.在MainActivity.java文件中生成适配器对象并绑定至ListView对象
Ex:
效果:
list_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="1"
android:animationOrder="normal"
android:animation="@anim/list_anim">
</layoutAnimation>
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_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/user_gender"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mycompany.layoutanimationcontroller.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout" />
<Button
android:id="@+id/button"
android:text="测试"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity.java:
package com.mycompany.layoutanimationcontroller;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button button;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
}
/*
* 该函数的作用是生成适配器对象
* */
private ListAdapter buildListAdapter() {
// 创建List对象,用来为ListView提供数据
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
// 创建三个HashMap对象,分别对应三条数据
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", "男");
// 将三个HashMap对象添加至List对象
list.add(m1);
list.add(m2);
list.add(m3);
// 设置适配器
// 第一个参数是上下文对象
// 第二个参数是提供数据的List对象
// 第三个参数是提供每个条目显示格式的布局文件
// 第四个参数是HashMap对象中的键
// 第五个参数是显示数据的View
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.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 View.OnClickListener{
@Override
public void onClick(View v) {
listView.setAdapter(buildListAdapter());
}
}
}
在代码当中使用LayoutAnimationController:
除了在xml文件中使用LayoutAnimationController之外,还可以在代码中使用。使用方法和前种方法一致,不过不再需要在主布局文件的ListView标签中引用布局动画效果文件。然后在代码文件中进行以下步骤即可:
1.创建一个Animation对象,可以通过AnimationUtils装载xml文件,也可以直接使用Animation的构造函数创建Animation对象:
Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
2.创建LayoutAnimationController对象:
LayoutAnimationController lac = new LayoutAnimationController(animation);
3.设置控件的显示顺序:
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
4.为ListView设置LayoutAnimationController属性:
listView.setLayoutAnimation(lac);