What is Animation?
Animation
extends Object
implements Cloneable
Abstraction for an Animation that can be applied to Views, Surfaces, or other objects.
AlphaAnimation
extends Animation
An animation that controls the alpha level of an object. Useful for fading things in and out. This animation ends up changing the alpha property of aTransformation
AlphaAnimation(Context context, AttributeSet attrs)
Constructor used when an AlphaAnimation is loaded from a resource.
AlphaAnimation(float fromAlpha, float toAlpha)
Constructor to use when building an AlphaAnimation from code
AnimationSet
extends Animation
Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.
在代码中实现动画效果的方法:
ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(1000); alphaAnimation.setStartOffset(10000); animationSet.addAnimation(alphaAnimation); //animationSet.setStartOffset(10000); animationSet.setFillBefore(false); animationSet.setFillAfter(true); imageView.startAnimation(animationSet);
在XML文件中实现动画效果的方法:
① 在res目录下创建一个anim文件夹,在里面添加一个alpha.xml文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fillAfter="true" android:fillBefore="false"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="1000" android:duration="1000" /> </set>
② 在Activity中使用AnimationUtils获取Animation并进行设置:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
ScaleAnimation
extends Animation
An animation that controls the scale of an object. You can specify the point to use for the center of scaling.
ScaleAnimation(Context context, AttributeSet attrs)
Constructor used when a ScaleAnimation is loaded from a resource.
ScaleAnimation(float fromX, float toX, float fromY, float toY)
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
Constructor to use when building a ScaleAnimation from code
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Constructor to use when building a ScaleAnimation from code
在代码中实现动画效果:
ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f); animationSet.addAnimation(scaleAnimation); animationSet.setDuration(1000); imageView.startAnimation(animationSet);
在XML文件中实现动画效果的方法:
① 在res的anim文件夹下,创建一个scale.xml文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <scale android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="2000" /> </set>
② 在Activity中使用AnimationUtils获取Animation并进行设置:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale); imageView.startAnimation(animation);
RotateAnimation
extends Animation
An animation that controls the rotation of an object. This rotation takes place int the X-Y plane. You can specify the point to use for the center of the rotation, where (0,0) is the top left point. If not specified, (0,0) is the default rotation point.
RotateAnimation(Context context, AttributeSet attrs)
Constructor used when a RotateAnimation is loaded from a resource.
RotateAnimation(float fromDegrees, float toDegrees)
Constructor to use when building a RotateAnimation from code.
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
Constructor to use when building a RotateAnimation from code
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
Constructor to use when building a RotateAnimation from code
在代码中实现动画效果:
ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f); rotateAnimation.setDuration(1000); animationSet.addAnimation(rotateAnimation); imageView.startAnimation(animationSet);
在XML文件中实现动画效果的方法:
① 在res的anim文件夹下,创建一个rotate.xml文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <rotate android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%" android:pivotY="50%" android:duration="1000" /> </set>
② 在Activity中使用AnimationUtils获取Animation并进行设置:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate); imageView.startAnimation(animation);
TranslateAnimation
extends Animation
An animation that controls the position of an object.
TranslateAnimation(Context context, AttributeSet attrs)
Constructor used when a TranslateAnimation is loaded from a resource.
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
Constructor to use when building a TranslateAnimation from code
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
Constructor to use when building a TranslateAnimation from code
在代码中实现动画效果:
ImageView imageView = (ImageView) findViewById(R.id.imageView1); AnimationSet animationSet = new AnimationSet(true); TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f); translateAnimation.setDuration(1000); animationSet.addAnimation(translateAnimation); imageView.startAnimation(animationSet);
在XML文件中实现动画效果的方法:
① 在res的anim文件夹下,创建一个translate.xml文件:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="0%p" android:toXDelta="100%p" android:fromYDelta="0%p" android:toYDelta="100%p" android:duration="1000" /> </set>
其中100%p表示相对于父空间的位置
② 在Activity中使用AnimationUtils获取Animation并进行设置:
Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate); imageView.startAnimation(animation);
AnimationSet animationSet = new AnimationSet(false); AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f); ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animationSet.addAnimation(alpha); animationSet.addAnimation(scale); animationSet.setDuration(2000); animationSet.setStartOffset(1000); animationSet.setFillAfter(true); imageView.startAnimation(animationSet);
alpha.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true" android:fillAfter="true"> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="1000" android:fillAfter="true" android:duration="2000" /> <scale android:fromXScale="1.0" android:toXScale="0.5" android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%" android:pivotY="50%" android:startOffset="1000" android:duration="2000" /> </set>
Activity中的代码:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha); imageView.startAnimation(animation);
什么是Interpolator
Interpolator
extends Object
AccelerateDecelerateInterpolator:
AccelerateDecelerateInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change starts and ends slowly but accelerates through the middle.
AccelerateInterpolater:
AccelerateInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change starts out slowly and and then accelerates.
CycleInterpolator:
CycleInterpolator
extends Object
implements Interpolator
Repeats the animation for a specified number of cycles. The rate of change follows a sinusoidal pattern.
DecelerateInterpolator:
DecelerateInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change starts out quickly and and then decelerates.
LinearInterpolator:
LinearInterpolator
extends Object
implements Interpolator
An interpolator where the rate of change is constant.
XML文件定义在set标签里或每个动画标签
set标签中定义:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true" android:fillAfter="true">
每个动画标签中定义:
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="false" android:fillAfter="true"> <alpha android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="1000" android:fillAfter="true" android:duration="2000" /> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1.0" android:toXScale="0.5" android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%" android:pivotY="50%" android:startOffset="1000" android:duration="2000" /> </set>
在代码中设置:
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
或者分别为每个动画设置:
AnimationSet animationSet = new AnimationSet(false);
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setInterpolator(new AccelerateInterpolator());
ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale.setInterpolator(new AccelerateDecelerateInterpolator());
① 准备4张图片run1.png,run2.png,run3.png,run4.png分别放到res的三个drawable文件夹中
② 在res的drawable-ldpi目录下创建一个anim_run.xml文件:
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/run1" android:duration="100" /> <item android:drawable="@drawable/run2" android:duration="100" /> <item android:drawable="@drawable/run3" android:duration="100" /> <item android:drawable="@drawable/run4" android:duration="100" /> </animation-list>
③ 在Activity中使用xml文件设置ImageView控件imageView的背景源,并获取AnimationDrawable进行显示动画:
imageView.setBackgroundResource(R.drawable.anim_run); AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground(); animationDrawable.start();
什么是LayoutAnimationController?
LayoutAnimationController
extends Object
A layout animation controller is used to animated a layout's, or a view group's, children. Each child uses the same animation but for every one of them, the animation starts at a different time. A layout animation controller is used by ViewGroup to compute the delay by which each child's animation start must be offset. The delay is computed by using characteristics of each child, like its index in the view group. This standard implementation computes the delay by multiplying a fixed amount of miliseconds by the index of the child in its parent view group. Subclasses are supposed to override getDelayForView(android.view.View) to implement a different way of computing the delay. For instance, aGridLayoutAnimationController will compute the delay based on the column and row indices of the child in its parent view group. Information used to compute the animation delay of each child are stored in an instance of LayoutAnimationController.AnimationParameters, itself stored in theViewGroup.LayoutParams of the view.
在使用LayoutAnimationController控制ListView控件的样式效果的方法:
① 在res的anim文件夹中创建一个list_anim.xml文件用于控制ListView控件的动画:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="1000" /> </set>
② 创建一个布局文件item.xml用于设置ListView的item的样式:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip"> <TextView android:id="@+id/user_name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="10pt" android:singleLine="true" /> <TextView android:id="@+id/user_id" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="10pt" android:singleLine="true"/> </LinearLayout>
③ 在主Activity的布局文件main.xml中添加一个ListView
<ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:layoutAnimation="@anim/anim_layout" />
④ 创建一个MainActivity继承ListActivity,并在onCreate方法中添加如下代码:
ListView listView = getListView(); List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); HashMap<String, String> hm1 = new HashMap<String, String>(); hm1.put("user_name", "arthinking"); hm1.put("user_id", "001"); HashMap<String, String> hm2 = new HashMap<String, String>(); hm2.put("user_name", "Jason"); hm2.put("user_id", "002"); list.add(hm1); list.add(hm2); SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item, new String[] { "user_name", "user_id" }, new int[] { R.id.user_name, R.id.user_id }); listView.setAdapter(simpleAdapter); //通过Animation获取LayoutAnimationController对ListView进行设置 Animation animation = (Animation)AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim); LayoutAnimationController lac = new LayoutAnimationController(animation); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); lac.setDelay(0.5f); listView.setLayoutAnimation(lac);
这样,运行程序,显示的ListView就会按照xml文件中预置的动画效果显示了。
也可以通过xml文件进行设置动画:
① 在以上步骤的基础之上,在res/anim文件夹下创建一个anim_layout.xml文件:
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="1" android:animationOrder="normal" android:animation="@anim/list_anim" />
② main在布局文件的的ListView添加如下属性:
android:layoutAnimation="@anim/anim_layout"
这样就在把MainActivity的onCreate()方法中的
//通过Animation获取LayoutAnimationController对ListView进行设置
注释后的代码删除了,直接使用xml进行动画的控制。
Animation.AnimationListener
android.view.animation.Animation.AnimationListener
An animation listener receives notifications from an animation. Notifications indicate animation related events, such as the end or the repetition of the animation.
包含以下的三个方法:
Notifies the end of the animation.
onAnimationRepeat(Animation animation)
Notifies the repetition of the animation.
onAnimationStart(Animation animation)
Notifies the start of the animation.
① 可以为一个Button添加一个事件:
button.setOnClickListener(new TestAnimationListener());
② 接下来是编写这个TestAnimationListener类,继承AnimationListener,并覆盖里面的三个方法:
//这里获取控件组,R.id.layoutId为main.xml的整体布局标签的id属性值 ViewGroup viewGroup = (ViewGroup)findViewById(R.id.layoutId); private class RemoveAnimationListener implements AnimationListener{ //该方法在淡出效果执行结束之后被调用 @Override public void onAnimationEnd(Animation animation) { //假设这里要在动画执行完之后删除一个TextView viewGroup.removeView(textView); } @Override public void onAnimationRepeat(Animation animation) { System.out.println("onAnimationRepeat"); } @Override public void onAnimationStart(Animation animation) { System.out.println("onAnimationStart"); } }
③ 同样的,在动画效果中添加控件可以按照如下实现
ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, scale.setDuration(1000); scale.setStartOffset(100); TextView textView = new TextView(MainActivity.this); textView.setText("add"); viewGroup.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); textView.startAnimation(scale);
相关推荐
本文将深入探讨如何使用`Interpolator`和`AnimationSet`来实现这种动画效果。 首先,我们要理解`Interpolator`的概念。在Android的动画系统中,`Interpolator`(插值器)是一个接口,它定义了动画时间的变化方式。...
本文将深入探讨如何利用Interpolator实现更精细的动画控制,以及如何创建自定义的涟漪效果和使用百分比布局来优化UI设计。 首先,Interpolator在Android动画中扮演着至关重要的角色。Interpolator是动画时间插值器...
通过自定义`Interpolator`并结合`TranslateAnimation`,可以轻松地在Android应用中实现抛物线以及其他各种曲线的动画效果。这种方式不仅增强了应用的交互性,还提升了用户的体验感。开发者可以根据项目需求灵活调整...
在Android开发中,动画(Animation)是提升用户体验和视觉效果的重要工具。Android提供了多种动画机制,包括帧动画(Frame Animation)、补间动画(Tween Animation)、属性动画(Property Animation)。本篇将详细...
本文将深入探讨Interpolator在Android动画中的应用,以及如何自定义Interpolator来实现不同的动画效果。 首先,我们要理解Interpolator的基本原理。Interpolator,翻译为插值器或时间插补器,主要负责处理动画的...
本文将深入探讨如何使用插值器(Interpolator)来增强Android动画的效果。插值器是动画系统中的一个重要组件,它负责控制动画过程中时间的变化,使得动画看起来更加平滑、自然或富有动态感。 首先,我们需要了解插值...
在实际项目中,为了兼容低版本的Android系统,往往需要同时使用视图动画和属性动画。通过`NineOldAndroids`库,可以将属性动画API回溯到API level 8。此外,使用`Interpolator`可以自定义动画的速度曲线,如线性、...
总结,Android中的补间动画为我们提供了一种简单而强大的方式来实现各种视觉效果,包括向中心点吸入的效果。通过理解动画的基本原理和属性,开发者可以创建出更加吸引人的用户界面,提升应用的体验。同时,合理地...
博客文章《Android菜单展开的动画效果》(http://blog.csdn.net/baidu_nod/article/details/38404531)提供了具体的代码示例和详细解释,包括如何设置动画的插补器(Interpolator)以改变动画的速度曲线,以及如何...
在Android开发中,自定义View和Interpolator是实现复杂动画效果的重要工具。自定义View允许开发者扩展标准的Android组件,以创建独特的用户界面元素,并且能够根据需求实现特定的动画效果。Interpolator则是控制动画...
" Android 图片及动画的缩放和旋转实现" Android 动画有两种:Tween Animation 和 Frame Animation。本文主要介绍 Tween Animation 的实现, Tween Animation 是对视图对象中的内容进行一系列简单的转换,比如位置...
Android提供了两种主要的动画类型:帧动画和补间动画,这两种动画各有特点,适用于不同的场景。 **帧动画**是基于一系列连续图像播放来创建动画效果的方法。这类似于传统的卡通制作,每一帧都是一张独立的图片。在...
在Android开发中,属性动画(Property Animation)是一个强大的工具,它可以为UI元素创造出丰富的动态效果。本项目"Android 属性动画使控件沿贝塞尔曲线移动"就利用了这一特性,实现了让控件沿着自定义的贝塞尔曲线...
通过深入研究这些源码,开发者不仅可以学会如何在Android中实现控件的抖动效果,还能对Android动画系统有更全面的理解,提升自己的编程技能。此外,这些源码还可以作为模板,为其他类型的动态效果提供灵感和实现思路...
3. Android的动画框架还支持Tween动画和Frame动画: - Tween动画:在两个关键帧之间平滑地过渡视图属性。 - Frame动画:类似于电影胶片,通过连续播放一组静态图片来创建动画效果。 4. 动画的使用方法: - XML...
在Android开发中,属性动画(Property Animation)是一个强大的工具,它可以为用户界面带来生动和吸引人的动态效果。本文将深入探讨如何使用属性动画实现一个酷炫的登录界面,以响应群友分享的登录原型图。 首先,...
### Android的动画Animation详解 #### 一、动画概述 Android平台提供了丰富的动画支持,通过不同的方式可以实现多种视觉效果,从而提升用户体验。Android动画主要分为两大类:Tween动画(渐变动画)和Frame-by-...
本篇文章将详细探讨Android中的补间Animation,包括其基本概念、使用方法以及如何实现Activity切换动画。 1. 补间Animation的基本原理: 补间Animation(Tween Animation)基于帧动画,通过随着时间的推移改变View...
在Android开发中,属性动画(Property Animation)是一个强大的工具,用于创建丰富的动态效果。相比于旧式的视图动画(View Animation),属性动画系统允许开发者对对象的属性进行改变,并且这些改变能够在帧之间...