LayoutAnimationController动画效果,一次出现
一、布局文件使用
- 动画list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/list_item_alpha"
android:animationOrder="normal"
android:delay="0.8"
/>
- 每个列表项动画list_item_alpha.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>
- 主界面布局文件activity_main.xml
特别注意要在ListView中添加android:layoutAnimation="@anim/list_item_layout"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mazaiting.layoutanimationcontroller.MainActivity"
>
<ListView
android:id="@+id/listView"
android:layoutAnimation="@anim/list_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="测试"
android:onClick="test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="清空"
android:onClick="clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
- 列表布局文件item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/app_name"
android:textSize="20sp"
/>
- 主界面代码
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private static final String[] STRINGS = {
"BruceZhang", "Alpha", "Translate", "Blanklin", "Rotate", "GreenFrank"
};
private ArrayAdapter<String> mAdapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) this.findViewById(R.id.listView);
mAdapter = new ArrayAdapter<>(this, R.layout.item_list, STRINGS);
}
public void test(View view) {
mListView.setAdapter(mAdapter);
}
public void clear(View view) {
mListView.setAdapter(null);
}
}
二、代码配置
- 动画文件
<?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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mazaiting.layoutanimationcontroller.MainActivity"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="测试"
android:onClick="test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="清空"
android:onClick="clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
- 主界面动画设置
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private static final String[] STRINGS = {
"BruceZhang", "Alpha", "Translate", "Blanklin", "Rotate", "GreenFrank"
};
private ArrayAdapter<String> mAdapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) this.findViewById(R.id.listView);
mAdapter = new ArrayAdapter<>(this, R.layout.item_list, STRINGS);
// 1. 创建一个Animation对象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.list_item_alpha);
// 2. 使用如下代码创建LayoutAnimationController对象
LayoutAnimationController lac = new LayoutAnimationController(animation);
// 3. 设置控件显示的顺序
lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
// 4. 为ListView设置LayoutAnimationController尚需经
mListView.setLayoutAnimation(lac);
}
public void test(View view) {
mListView.setAdapter(mAdapter);
}
public void clear(View view) {
mListView.setAdapter(null);
}
}