为ViewGroup的子视图添加悦目的动画效果
2015-11-02 21:53
381 查看
Android提供了LayoutAnimationController类,用于为布局或者ViewGroup的子视图添加动画效果。有一点需要强调,开发者不可以为每个子视图分别指定不同的动画效果,但是LayoutAnimationController可以决定各个子视图显示动画效果的时间。使用LayoutAnimationController有两种方式:直接在代码中使用或者在XML文件中配置。
下面我们将结合透明度渐变动画(alpha animation)和位移动画(translate animation)演示如何给ListView的子视图添加动画效果的两种方法。效果如下:
方法一:
方法二:
list_animation.xml
为ViewGroup的子视图添加动画效果是比较简单的。动画会令应用程序看起来更专业更精练。我们其实还可以继续完善该程序。比如,开发者可以将默认Interpolator替换为BounceInterpolator。这样,当子视图滑落到预定位置时,会出现弹跳效果。此外,还可以控制子视图动画的显示顺序。
代码地址
下面我们将结合透明度渐变动画(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。这样,当子视图滑落到预定位置时,会出现弹跳效果。此外,还可以控制子视图动画的显示顺序。
代码地址
相关文章推荐
- 小旭的互联网营销之经典案例
- OpenSSL Heartbleed "心脏滴血"漏洞简单攻击示例
- perfview
- javascript继承实现
- 给函数返回值赋值
- const 限定符
- 一道面试题: C能申请的最大全局数组大小?
- 爬虫项目功能规格说明书
- hiho1246(数学求模)
- c++11特性之std::thread--初识
- C#学习笔记 变量和常量
- c++11特性之std::thread--初识
- JAVA课后作业
- AngularJs Angular数据类型判断
- 详解UML中的6大关系(关联、依赖、聚合、组合、泛化、实现)
- JAVA学习(二)
- LightOJ 1027 - A Dangerous Maze (期望)
- SSL编程(2).NET最简单的客户端
- 20131215-20140107XP、Win7下第一个Java程序(2014-01-18)
- 【PAT】1053. Path of Equal Weight (30)