为ViewGroup的子视图添加悦目的动画效果

本文介绍如何使用Android中的LayoutAnimationController为ListView的子视图添加透明度渐变和位移动画效果。通过两种方法实现:直接在Java代码中创建动画集或在XML文件中定义动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android提供了LayoutAnimationController类,用于为布局或者ViewGroup的子视图添加动画效果。有一点需要强调,开发者不可以为每个子视图分别指定不同的动画效果,但是LayoutAnimationController可以决定各个子视图显示动画效果的时间。使用LayoutAnimationController有两种方式:直接在代码中使用或者在XML文件中配置。

下面我们将结合透明度渐变动画(alpha animation)和位移动画(translate animation)演示如何给ListView的子视图添加动画效果的两种方法。效果如下:

这里写图片描述

方法一:

package com.example.huangfei.switcherdemo;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView listView = getListView();

        /**
         * 创建默认动画集合对象
         * 传入AnimationSet构造函数的布尔型参数决定每个动画是不是使用同一个Interpolator,在本例中,
         * 使用默认Interpolator。
         */
        AnimationSet set = new AnimationSet(true);

        //创建透明度渐变动画
        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(200);
        set.addAnimation(animation);

        /**
         * 创建位移动画
         *
         *TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
         * int fromYType, float fromYValue, int toYType, float toYValue)
         *
         * TranslateAnimation的构造函数需传入x轴和y轴的起始与结束坐标以及坐标的参照
         * Animation提供了三个选项用于指定计算坐标的参照:
         * Animation.ABSOLUTE
         * Animation.RELATIVE_TO_SELF
         * Animation.RELATIVE_TO_PARENT
         */
        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF,
                0.0f);
        animation.setDuration(200);
        set.addAnimation(animation);

        //创建LayoutAnimationController对象并设置子视图动画效果持续时间
        LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);

        //将LayoutAnimationController对象设置到ListView中
        listView.setLayoutAnimation(controller);

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                Countries.COUNTRIES));
    }
}

方法二:

package com.example.huangfei.switcherdemo;

import android.app.ListActivity;
import android.os.Bundle;
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.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListView listView = getListView();

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.list_animation);
        LayoutAnimationController controller = new LayoutAnimationController(animation, 0.5f);

        listView.setLayoutAnimation(controller);

        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                Countries.COUNTRIES));
    }
}

list_animation.xml

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

    <alpha
        android:duration="200"
        android:fromAlpha="0"
        android:toAlpha="1"/>

    <translate
        android:duration="200"
        android:fromYDelta="-100%"
        android:toYDelta="0%"/>
</set>

为ViewGroup的子视图添加动画效果是比较简单的。动画会令应用程序看起来更专业更精练。我们其实还可以继续完善该程序。比如,开发者可以将默认Interpolator替换为BounceInterpolator。这样,当子视图滑落到预定位置时,会出现弹跳效果。此外,还可以控制子视图动画的显示顺序。

代码地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值