Android实战简易教程-第十七枪(LayoutAnimation布局动画)

我们对Tween和Frame动画比较熟悉,下面我们通过一个实例来了解一下LayoutAnimation(布局动画)

首先我们要建立一个动画文件zoom_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="1000"
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

    <alpha
        android:duration="1000"
        android:fromAlpha="0"
        android:toAlpha="1.0" />

</set>


main.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="1000"
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

    <alpha
        android:duration="1000"
        android:fromAlpha="0"
        android:toAlpha="1.0" />

</set>

MainActivity.java:

package com.example.layoutanimationdemo;

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

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.R.anim;
import android.content.Loader.ForceLoadContentObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
	private List list = new ArrayList();
	private ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		listView = (ListView) findViewById(R.id.list);

		for (int i = 0; i < 20; i++) {
			list.add("测试条目" + i);
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				MainActivity.this, android.R.layout.simple_list_item_1, list);
		listView.setAdapter(adapter);
		LayoutAnimationController layoutAnimationController = new LayoutAnimationController(
				AnimationUtils.loadAnimation(this, R.anim.zoom_in));
		layoutAnimationController
				.setOrder(LayoutAnimationController.ORDER_NORMAL);
		listView.setLayoutAnimation(layoutAnimationController);
		listView.startLayoutAnimation();

	}
}


运行实例:

Android实战简易教程-第十七枪(LayoutAnimation布局动画)_第1张图片

总结:

1.LayoutAnimationController 控制器,引入动画文件;

2.setOrder(LayoutAnimationController.ORDER_NORMAL);设置显示顺序,多种显示风格可选;

 
int ORDER_NORMAL Distributes the animation delays in the order in which view were added to their view group.//正常动画,由上往下
int ORDER_RANDOM Randomly distributes the animation delays.//随机动画
int ORDER_REVERSE Distributes the animation delays in the reverse order in which view were added to their view group.//倒序动画

3.listView.setLayoutAnimation(layoutAnimationController);//设定动画

listView.startLayoutAnimation();//启动动画

 

你可能感兴趣的:(android,layoutanimation)