标签:
Android SDK介绍了2种Animation:
Tween Animation(渐变动画):通过对特定的对象做图像变换如平移、缩放、旋转、淡出/淡入等产生动画效果
Frame Animation(帧动画):创建一个Drawable序列,这些Drawable可以按照指定的时间间隔一个一个的显示,也就是顺序播放事先做好的图像。
1、Tween Animation动画:
(1)Tween Animation有4种样式:
Alpha:渐变透明度动画效果
Scale:渐变尺寸伸缩动画效果
Translate:移动动画效果
Rotate:旋转动画效果
(2)Tween Animation有 2种使用方法:
a、在XML资源中定义Animation,使用AnimationUtils中的loadAnimation()函数加载动画;
b、使用Animation子类的构造函数来初始化Animation对象。
在XML资源中定义Animation:
Alpha:
-
<alpha android:interpolator= “@android:anim/accelerate_decelerate_interpolator”
-
android:fromAlpha="1.0"
-
android:toAlpha="0.0"
-
android:duration="3000"
-
></alpha>
-
<!--
-
interpolator:指定一个动画的插入器,用来控制动画的速度变化
-
fromAlpha:动画起始时透明度
-
0.0表示完全透明
-
1.0表示完全不透明
-
以上值取0.0-1.0之间的float数据类型的数字
-
toAlpha:动画结束时透明度
-
duration:持续时间 -->
Scale:
-
<scale
-
android:interpolator= “@android:anim/accelerate_decelerate_interpolator”
-
android:fromXScale=”0.0″
-
android:toXScale=”1.4″
-
android:fromYScale=”0.0″
-
android:toYScale=”1.4″
-
android:pivotX=”50%”
-
android:pivotY=”50%”
-
android:fillAfter=”false”
-
android:startOffset=“700”
-
android:duration=”700″
-
android:repeatCount=”10″ />
-
<!--
-
fromXScale[float]:为动画起始时,X坐标上的伸缩尺寸,0.0表示收缩到没有
-
fromYScale[float]:为动画起始时,Y坐标上的伸缩尺寸,0.0表示收缩到没有
-
1.0表示正常无伸缩
-
值小于1.0表示收缩
-
值大于1.0表示放大
-
toXScale[float]:为动画结束时,X坐标上的伸缩尺寸
-
toYScale[float]:为动画结束时,X坐标上的伸缩尺寸
-
pivotX[float]:为动画相对于物件的X坐标的开始位置
-
pivotY[float]:为动画相对于物件的X、Y坐标的开始位置
-
50,50%,50%p。这三种写法就分别代表了ABSOLUTE,RELATIVE_TO_SELF和RELATIVE_TO_PARENT。
-
属性值说明:从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置
-
fillAfter[boolean]:当设置为true ,该动画转化在动画结束后被应用
-
startOffset[long]:动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
-
repeatCount[int]:动画的重复次数 -->
Translate:
-
<translate
-
android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
-
android:fromXDelta=”30″
-
android:toXDelta=”-80″
-
android:fromYDelta=”30″
-
android:toYDelta=”300″
-
android:duration=”2000″ />
-
t;!--
-
fromXDelta:为动画起始时 X坐标上的位置
-
toXDelta: 为动画结束时 X坐标上的位置
-
fromYDelta: 为动画起始时 Y坐标上的位置
-
toYDelta:为动画结束时 Y坐标上的位置 -->
Rotate:
-
<rotate
-
android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
-
android:fromDegrees=”0″
-
android:toDegrees=”+350″
-
android:pivotX=”50%”
-
android:pivotY=”50%”
-
android:duration=”3000″ />
-
t;!--
-
fromDegrees:动画起始时物件的角度
-
toDegrees:动画结束时物件旋转的角度 可以大于360度
-
当角度为负数——表示逆时针旋转
-
当角度为正数——表示顺时针旋转
-
(负数from——to正数:顺时针旋转)
-
(负数from——to负数:逆时针旋转)
-
(正数from——to正数:顺时针旋转)
-
(正数from——to负数:逆时针旋转)
-
-
pivotX;:为动画相对于物件的X、Y坐标的开始位置
-
pivotY: 为动画相对于物件的X、Y坐标的开始位置
-
50%为物件的X或Y方向坐标上的中点位置 -->
使用Animation子类的构造函数来初始化Animation对象:
在Android SDK中提供了相应的类,Animation类派生出了AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation分别实现了平移、旋转、渐变尺寸和透明度等动画。
Tween Animation通过对 View 的内容完成一系列的图形变换 (包括平移、缩放、旋转、改变透明度)来实现动画效果。具体来讲,预先定义一组指令,这些指令指定了图形变换的类型、触发时间、持续时间。这些指令可以是以 XML 文件方式定义,也可以是以源代码方式定义。程序沿着时间线执行这些指令就可以实现动画效果。
(3)Android SDK中通过interpolator控制动画的运行
interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等。
Interpolator是基类,Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:
AccelerateDecelerateInterpolator 在动画开始与结束的时候减速,在中间的时候加速
AccelerateInterpolator 在动画开始的时候减速,然后开始加速
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的时候加速,然后开始减速
LinearInterpolator 在动画的以均匀的速率改变
(4)动画的运行模式
动画的运行模式有两种:
独占模式:即程序主线程进入一个循环,根据动画指令不断刷新屏幕,直到动画结束;
中断模式:即有单独一个线程对时间计数,每隔一定的时间向主线程发通知,主线程接到通知后更新屏幕
2、Frame Animation动画:
前面已经说过,Frame Animation是顺序播放事先做好的图像,与电影类似。Android SDK提供了类AnimationDrawable来定义、使用Frame Animation。
Frame Animation可以在XML Resource定义,也可以使用AnimationDrawable中的API定义。由于Tween Animation与Frame Animation有着很大的不同,因此XML定义的格式也完全不一样,其格式是:首先是animation-list根节点,animation-list根节点中包含多个item子节点,每个item节点定义一帧动画:当前帧的drawable资源和当前帧持续的时间。
以下是属性动画
1、概述
Android提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。View
Animation相当简单,不过只能支持简单的缩放、平移、旋转、透明度基本的动画,且有一定的局限性。比如:你希望View有一个颜色的切换动画;你希望可以使用3D旋转动画;你希望当动画停止时,View的位置就是当前的位置;这些View Animation都无法做到。这就是Property Animation产生的原因,本篇博客详细介绍Property Animation的用法。至于Drawable Animation,嗯,略~
2、相关API
Property Animation故名思议就是通过动画的方式改变对象的属性了,我们首先需要了解几个属性:
Duration动画的持续时间,默认300ms。
Time interpolation:时间差值,乍一看不知道是什么,但是我说LinearInterpolator、AccelerateDecelerateInterpolator,大家一定知道是干嘛的了,定义动画的变化率。
Repeat count and behavior:重复次数、以及重复模式;可以定义重复多少次;重复时从头开始,还是反向。
Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。
Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默认为10ms,但最终依赖系统的当前状态;基本不用管。
相关的类
ObjectAnimator 动画的执行类,后面详细介绍
ValueAnimator 动画的执行类,后面详细介绍
AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
AnimatorInflater 用户加载属性动画的xml文件
TypeEvaluator 类型估值,主要用于设置动画操作属性的值。
TimeInterpolator 时间插值,上面已经介绍。
总的来说,属性动画就是,动画的执行类来设置动画操作的对象的属性、持续时间,开始和结束的属性值,时间差值等,然后系统会根据设置的参数动态的变化对象的属性。
3、ObjectAnimator实现动画
之所以选择ObjectAnimator为第一个~~是因为,这个实现最简单~~一行代码,秒秒钟实现动画,下面看个例子:
布局文件:
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:id="@+id/id_container" >
-
-
<ImageView
-
android:id="@+id/id_ball"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_centerInParent="true"
-
android:src="@drawable/mv"
-
android:scaleType="centerCrop"
-
android:onClick="rotateyAnimRun"
-
/>
-
-
</RelativeLayout>
很简单,就一张妹子图片~
Activity代码:
-
package com.example.zhy_property_animation;
-
-
import android.animation.ObjectAnimator;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
-
public class ObjectAnimActivity extends Activity
-
{
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.xml_for_anim);
-
}
-
-
public void rotateyAnimRun(View view)
-
{
-
ObjectAnimator
-
.ofFloat(view, "rotationX", 0.0F, 360.0F)
-
.setDuration(500)
-
.start();
-
}
-
-
}
效果:
![技术分享](http://img.blog.csdn.net/20140724135858296)
是不是一行代码就能实现简单的动画~~
对于ObjectAnimator
1、提供了ofInt、ofFloat、ofObject,这几个方法都是设置动画作用的元素、作用的属性、动画开始、结束、以及中间的任意个属性值。
当对于属性值,只设置一个的时候,会认为当然对象该属性的值为开始(getPropName反射获取),然后设置的值为终点。如果设置两个,则一个为开始、一个为结束~~~
动画更新的过程中,会不断调用setPropName更新元素的属性,所有使用ObjectAnimator更新某个属性,必须得有getter(设置一个属性值的时候)和setter方法~
2、如果你操作对象的该属性方法里面,比如上例的setRotationX如果内部没有调用view的重绘,则你需要自己按照下面方式手动调用。
-
anim.addUpdateListener(new AnimatorUpdateListener()
-
{
-
@Override
-
public void onAnimationUpdate(ValueAnimator animation)
-
{
-
-
-
}
-
});
3、看了上面的例子,因为设置的操作的属性只有一个,那么如果我希望一个动画能够让View既可以缩小、又能够淡出(3个属性scaleX,scaleY,alpha),只使用ObjectAnimator咋弄?
想法是不是很不错,可能会说使用AnimatorSet啊,这一看就是一堆动画塞一起执行,但是我偏偏要用一个ObjectAnimator实例实现呢~下面看代码:
-
public void rotateyAnimRun(final View view)
-
{
-
ObjectAnimator anim = ObjectAnimator
-
.ofFloat(view, "zhy", 1.0F, 0.0F)
-
.setDuration(500);
-
anim.start();
-
anim.addUpdateListener(new AnimatorUpdateListener()
-
{
-
@Override
-
public void onAnimationUpdate(ValueAnimator animation)
-
{
-
float cVal = (Float) animation.getAnimatedValue();
-
view.setAlpha(cVal);
-
view.setScaleX(cVal);
-
view.setScaleY(cVal);
-
}
-
});
-
}
把设置属性的那个字符串,随便写一个该对象没有的属性,就是不管~~咱们只需要它按照时间插值和持续时间计算的那个值,我们自己手动调用~
效果:
![技术分享](http://img.blog.csdn.net/20140724145443375)
这个例子就是想说明一下,有时候换个思路不要被API所约束,利用部分API提供的功能也能实现好玩的效果~~~
比如:你想实现抛物线的效果,水平方向100px/s,垂直方向加速度200px/s*s ,咋实现呢~~可以自己用ObjectAnimator试试~
4、其实还有更简单的方式,实现一个动画更改多个效果:使用propertyValuesHolder
-
public void propertyValuesHolder(View view)
-
{
-
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
-
0f, 1f);
-
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
-
0, 1f);
-
PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
-
0, 1f);
-
ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();
-
}
4、ValueAnimator实现动画
和ObjectAnimator用法很类似,简单看一下用view垂直移动的动画代码:
-
public void verticalRun(View view)
-
{
-
ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight
-
- mBlueBall.getHeight());
-
animator.setTarget(mBlueBall);
-
animator.setDuration(1000).start();
-
}
给你的感觉是不是,坑爹啊,这和ValueAnimator有毛线区别~但是仔细看,你看会发现,没有设置操作的属性~~也就是说,上述代码是没有任何效果的,没有指定属性~
这就是和ValueAnimator的区别之处:ValueAnimator并没有在属性上做操作,你可能会问这样有啥好处?我岂不是还得手动设置?
好处:不需要操作的对象的属性一定要有getter和setter方法,你可以自己根据当前动画的计算值,来操作任何属性,记得上例的那个【我希望一个动画能够让View既可以缩小、又能够淡出(3个属性scaleX,scaleY,alpha)】吗?其实就是这么个用法~
实例:
布局文件:
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:id="@+id/id_container"
-
-
>
-
-
<ImageView
-
android:id="@+id/id_ball"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:src="@drawable/bol_blue" />
-
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:orientation="horizontal" >
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:onClick="verticalRun"
-
android:text="垂直" />
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:onClick="paowuxian"
-
android:text="抛物线" />
-
-
</LinearLayout>
-
-
</RelativeLayout>
左上角一个小球,底部两个按钮~我们先看一个自由落体的代码:
-
-
-
-
-
public void verticalRun( View view)
-
{
-
ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight
-
- mBlueBall.getHeight());
-
animator.setTarget(mBlueBall);
-
animator.setDuration(1000).start();
-
-
animator.addUpdateListener(new AnimatorUpdateListener()
-
{
-
@Override
-
public void onAnimationUpdate(ValueAnimator animation)
-
{
-
mBlueBall.setTranslationY((Float) animation.getAnimatedValue());
-
}
-
});
-
}
与ObjectAnimator不同的就是我们自己设置元素属性的更新~虽然多了几行代码,但是貌似提高灵活性~
下面再来一个例子,如果我希望小球抛物线运动【实现抛物线的效果,水平方向100px/s,垂直方向加速度200px/s*s 】,分析一下,貌似只和时间有关系,但是根据时间的变化,横向和纵向的移动速率是不同的,我们该咋实现呢?此时就要重写TypeValue的时候了,因为我们在时间变化的同时,需要返回给对象两个值,x当前位置,y当前位置:
代码:
-
-
-
-
-
public void paowuxian(View view)
-
{
-
-
ValueAnimator valueAnimator = new ValueAnimator();
-
valueAnimator.setDuration(3000);
-
valueAnimator.setObjectValues(new PointF(0, 0));
-
valueAnimator.setInterpolator(new LinearInterpolator());
-
valueAnimator.setEvaluator(new TypeEvaluator<PointF>()
-
{
-
-
@Override
-
public PointF evaluate(float fraction, PointF startValue,
-
PointF endValue)
-
{
-
Log.e(TAG, fraction * 3 + "");
-
-
PointF point = new PointF();
-
point.x = 200 * fraction * 3;
-
point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
-
return point;
-
}
-
});
-
-
valueAnimator.start();
-
valueAnimator.addUpdateListener(new AnimatorUpdateListener()
-
{
-
@Override
-
public void onAnimationUpdate(ValueAnimator animation)
-
{
-
PointF point = (PointF) animation.getAnimatedValue();
-
mBlueBall.setX(point.x);
-
mBlueBall.setY(point.y);
-
-
}
-
});
-
}
可以看到,因为ofInt,ofFloat等无法使用,我们自定义了一个TypeValue,每次根据当前时间返回一个PointF对象,(PointF和Point的区别就是x,y的单位一个是float,一个是int;RectF,Rect也是)PointF中包含了x,y的当前位置~然后我们在监听器中获取,动态设置属性:
效果图:
![技术分享](http://img.blog.csdn.net/20140724153628441)
有木有两个铁球同时落地的感觉~~对,我应该搞两个球~~ps:物理公式要是错了,就当没看见哈
自定义TypeEvaluator传入的泛型可以根据自己的需求,自己设计个Bean。
好了,我们已经分别讲解了ValueAnimator和ObjectAnimator实现动画;二者区别;如何利用部分API,自己更新属性实现效果;自定义TypeEvaluator实现我们的需求;但是我们并没有讲如何设计插值,其实我觉得把,这个插值默认的那一串实现类够用了~~很少,会自己去设计个超级变态的~嗯~所以:略。
5、监听动画的事件
对于动画,一般都是一些辅助效果,比如我要删除个元素,我可能希望是个淡出的效果,但是最终还是要删掉,并不是你透明度没有了,还占着位置,所以我们需要知道动画如何结束。
所以我们可以添加一个动画的监听:
-
public void fadeOut(View view)
-
{
-
ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f);
-
-
anim.addListener(new AnimatorListener()
-
{
-
-
@Override
-
public void onAnimationStart(Animator animation)
-
{
-
Log.e(TAG, "onAnimationStart");
-
}
-
-
@Override
-
public void onAnimationRepeat(Animator animation)
-
{
-
-
Log.e(TAG, "onAnimationRepeat");
-
}
-
-
@Override
-
public void onAnimationEnd(Animator animation)
-
{
-
Log.e(TAG, "onAnimationEnd");
-
ViewGroup parent = (ViewGroup) mBlueBall.getParent();
-
if (parent != null)
-
parent.removeView(mBlueBall);
-
}
-
-
@Override
-
public void onAnimationCancel(Animator animation)
-
{
-
-
Log.e(TAG, "onAnimationCancel");
-
}
-
});
-
anim.start();
-
}
这样就可以监听动画的开始、结束、被取消、重复等事件~但是有时候会觉得,我只要知道结束就行了,这么长的代码我不能接收,那你可以使用AnimatorListenerAdapter
-
anim.addListener(new AnimatorListenerAdapter()
-
{
-
@Override
-
public void onAnimationEnd(Animator animation)
-
{
-
Log.e(TAG, "onAnimationEnd");
-
ViewGroup parent = (ViewGroup) mBlueBall.getParent();
-
if (parent != null)
-
parent.removeView(mBlueBall);
-
}
-
});
AnimatorListenerAdapter继承了AnimatorListener接口,然后空实现了所有的方法~
效果图:
![技术分享](http://img.blog.csdn.net/20140724162144202)
animator还有cancel()和end()方法:cancel动画立即停止,停在当前的位置;end动画直接到最终状态。
6、AnimatorSet的使用
实例:
布局文件:
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:id="@+id/id_container"
-
-
>
-
-
<ImageView
-
android:id="@+id/id_ball"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_centerInParent="true"
-
android:src="@drawable/bol_blue" />
-
-
<LinearLayout
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:layout_alignParentBottom="true"
-
android:orientation="horizontal" >
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:onClick="togetherRun"
-
android:text="简单的多动画Together" />
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:onClick="playWithAfter"
-
android:text="多动画按次序执行" />
-
-
-
</LinearLayout>
-
-
</RelativeLayout>
继续玩球~
代码:
-
package com.example.zhy_property_animation;
-
-
import android.animation.AnimatorSet;
-
import android.animation.ObjectAnimator;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.view.animation.LinearInterpolator;
-
import android.widget.ImageView;
-
-
public class AnimatorSetActivity extends Activity
-
{
-
private ImageView mBlueBall;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.anim_set);
-
-
mBlueBall = (ImageView) findViewById(R.id.id_ball);
-
-
}
-
-
public void togetherRun(View view)
-
{
-
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",
-
1.0f, 2f);
-
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",
-
1.0f, 2f);
-
AnimatorSet animSet = new AnimatorSet();
-
animSet.setDuration(2000);
-
animSet.setInterpolator(new LinearInterpolator());
-
-
animSet.playTogether(anim1, anim2);
-
animSet.start();
-
}
-
-
public void playWithAfter(View view)
-
{
-
float cx = mBlueBall.getX();
-
-
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",
-
1.0f, 2f);
-
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",
-
1.0f, 2f);
-
ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,
-
"x", cx , 0f);
-
ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,
-
"x", cx);
-
-
-
-
-
-
AnimatorSet animSet = new AnimatorSet();
-
animSet.play(anim1).with(anim2);
-
animSet.play(anim2).with(anim3);
-
animSet.play(anim4).after(anim3);
-
animSet.setDuration(1000);
-
animSet.start();
-
}
-
}
写了两个效果:
第一:使用playTogether两个动画同时执行,当然还有playSequentially依次执行~~
第二:如果我们有一堆动画,如何使用代码控制顺序,比如1,2同时;3在2后面;4在1之前等~就是效果2了
有一点注意:animSet.play().with();也是支持链式编程的,但是不要想着狂点,比如
animSet.play(anim1).with(anim2).before(anim3).before(anim5); 这样是不行的,系统不会根据你写的这一长串来决定先后的顺序,所以麻烦你按照上面例子的写法,多写几行:
效果图:
![技术分享](http://img.blog.csdn.net/20140724162112581)
Android中的动画
标签:
原文地址:http://blog.csdn.net/jzkandgjr/article/details/51683288