«

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

时间:2024-3-2 18:26     作者:韩俊     分类: Android


我们对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();

    }
}


运行实例:

总结:

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

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

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

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

标签: android

热门推荐