您的位置:首页 > 其它

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

2015-11-02 21:53 381 查看
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。这样,当子视图滑落到预定位置时,会出现弹跳效果。此外,还可以控制子视图动画的显示顺序。

代码地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: