我们有时候通过动画,去把一个视图包装的精美一些。让其增加好感。 android动画包含视图动画和属性动画。在这里对视图动画做一下常用到属性的记录。摘要 : 本文记录了视图动画<alpha>, <scale>, <translate>, <rotate> 动画属性的大致意思和逐帧动画的大致使用。
从上图中可以看出 Animation 包含 alpha、scale、translate、rotate。我们可以通过 xml的方式 和 代码的的方式去设置一个动画或者一个动画的集合。
Animation
可以看到 AlphaAnimation、TranslateAnimation、RotateAnimation、ScaleAnimation、AnimationSet 都是继承与Animation 。对于它的属性子类就可以使用。
其定义的属性有 :
<declare-styleable name="Animation">
<!-- Defines the interpolator used to smooth the animation movement in time. -->
<attr name="interpolator" />
<!-- When set to true, the value of fillBefore is taken into account. -->
<attr name="fillEnabled" format="boolean" />
<!-- When set to true or when fillEnabled is not set to true, the animation transformation
is applied before the animation has started. The default value is true. -->
<attr name="fillBefore" format="boolean" />
<!-- When set to true, the animation transformation is applied after the animation is
over. The default value is false. If fillEnabled is not set to true and the
animation is not set on a View, fillAfter is assumed to be true.-->
<attr name="fillAfter" format="boolean" />
<!-- Amount of time (in milliseconds) for the animation to run. -->
<attr name="duration" />
<!-- Delay in milliseconds before the animation runs, once start time is reached. -->
<attr name="startOffset" format="integer" />
<!-- Defines how many times the animation should repeat. The default value is 0. -->
<attr name="repeatCount" format="integer">
<enum name="infinite" value="-1" />
</attr>
<!-- Defines the animation behavior when it reaches the end and the repeat count is
greater than 0 or infinite. The default value is restart. -->
<attr name="repeatMode">
<!-- The animation starts again from the beginning. -->
<enum name="restart" value="1" />
<!-- The animation plays backward. -->
<enum name="reverse" value="2" />
</attr>
<!-- Allows for an adjustment of the Z ordering of the content being
animated for the duration of the animation. The default value is normal. -->
<attr name="zAdjustment">
<!-- The content being animated be kept in its current Z order. -->
<enum name="normal" value="0" />
<!-- The content being animated is forced on top of all other
content for the duration of the animation. -->
<enum name="top" value="1" />
<!-- The content being animated is forced under all other
content for the duration of the animation. -->
<enum name="bottom" value="-1" />
</attr>
<!-- Special background behind animation. Only for use with window
animations. Can only be a color, and only black. If 0, the
default, there is no background. -->
<attr name="background" />
<!-- Special option for window animations: if this window is on top
of a wallpaper, don't animate the wallpaper with it. -->
<attr name="detachWallpaper" format="boolean" />
</declare-styleable>
可能用到属性的大致意思 :
属性 | 含义 |
---|---|
android:interpolator | 插值器,可以设置动画速率的变化,比如加速、减速、与加速等等。需要指定 Interpolator资源 |
android:fillEnabled | 设置为true时,android:fillBefore的值才有效,否则android:fillBefore会被忽略 |
android:fillBefore | 设置为true时,待动画执行完成后,会回到动画执行前的状态。默认为 true |
android:fillAfter | 设置为 true时,待动画执行完成后,会停留在最后的状态视图。默认为false;如果是动画集合<set> 需要在<set> 标签中设置该属性才有效 |
android:duration | 动画从开始到执行完成所花费的时间(单位:毫秒值) |
android:startOffset | 设置动画在执行之前需要等待的时间(单位:毫秒值),若是重复执行,则每次执行都需要等待 |
android:repeatCount | 设置动画重复执行的次数。默认值为0.既不重复;可设置为 -1 或 INFINITE (int INFINITE = -1),表示无线重复 |
android:repeatMode | 设置动画的重复执行的模式,可以设置为以下两个值
|
android:zAdjustment | 表示被设置动画的内容在动画运行时在Z轴上的位置,取值为以下三个值之一:
|
android:detachWallpaper | 设置是否在壁纸上运行,只对设置了壁纸背景的窗口动画(window animation)有效。设为true,则动画只在窗口运行,壁纸背景保持不变 |
android:interpolator
通过interpolator可以定义动画速率变化的方式,比如加速、减速、匀速等,每种interpolator都是 Interpolator 类的子类,Android系统已经实现了多种interpolator,对应也提供了公共的资源ID,如下表:
Interpolator class | Resource ID | Description |
---|---|---|
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 在动画开始与结束的地方速率变换比较慢,在中间的时候加速 |
AccelerateInterpolator | @android:anim/accelerate_interpolator | 在动画开始的时候速率比较慢,然后开始加速 |
AnticipateInterpolator | @android:anim/anticipate_interpolator | 开始的时候向后然后向前甩 |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 开始的时候向后然后向前甩一定值后返回最后的值 |
BounceInterpolator | @android:anim/bounce_interpolator | 动画结束的时候弹起 |
CycleInterpolator | @android:anim/bounce_interpolator | 动画循环播放特定的次数,速率改变沿着正弦曲线 |
DecelerateInterpolator | @android:anim/decelerate_interpolator | 在动画开始的地方快然后慢 |
LinearInterpolator | @android:anim/decelerate_interpolator | 以常量速率改变 |
OvershootInterpolator | @android:anim/overshoot_interpolator | 向前甩一定值后再回到原来位置 |
如果系统提供的以上 Interpolator不符合效果的话,也可以通过自定义。有如下二种方式 :
- 直接继承 Interpolator 或者其子类
- 通过自定义Xml文件,可以更改上面 Interpolator属性。
-注意,自定的 Xml文件需要存放于res/anim/目录下。跟标签与上面相应。
Interpolator class | Description |
---|---|
< accelerateDecelerateInterpolator > | 在动画开始与结束时速率改变比较慢,在中间的时候加速。没有可以更改设置属性。 |
< accelerateInterpolator > | 在动画开始时速率比较慢,然后开始加速。有一个属性可以设置加速的速率
|
< anticipateInterpolator > | 动画开始的时候向后然后往前抛。有一个属性设置向后拉的值
|
< anticipateOvershootInterpolator > | 动画开始的时候向后然后向前抛,会抛超过目标值后再返回到最后的值。可设置两个属性
|
< bounceInterpolator > | 动画结束的时候会弹跳。没有可更改设置的属性 |
< cycleInterpolator > | 动画循环做周期运动,速率改变沿着正弦曲线。有一个属性设置循环次数
|
< decelerateInterpolator > | 在动画开始时速率改变比较快,然后开始减速。有一个属性设置减速的速率
|
< linearInterpolator > | 动画匀速播放。没有可更改设置的属性 |
< overshootInterpolator > | 动画向前抛,会抛超过最后值,然后再返回。有一个属性
|
通过setAnimationListener()可以对动画进行监听。从而实现多个动画的接连播放。
public void setAnimationListener(AnimationListener listener) ;
//三种时间状态下的监听回调
public static interface AnimationListener {
//开始
void onAnimationStart(Animation animation);
//结束
void onAnimationEnd(Animation animation);
//重复
void onAnimationRepeat(Animation animation);
}
AlphaAnimation
AlphaAnimation对应Xml的 < alpha> 标签,可以实现透明度渐变的动画效果,也就是淡入淡出的效果。其定义的属性有:
<declare-styleable name="AlphaAnimation">
<attr name="fromAlpha" format="float" />
<attr name="toAlpha" format="float" />
</declare-styleable>
属性 | 含义 |
---|---|
android:fromAlpha | 动画开始时的透明度,0.0为全透明,1.0为不透明,默认为1.0 |
android:toAlpha | 动画结束时的透明度,0.0为全透明,1.0为不透明,默认为1.0 |
setAlphaType(1, 0);
private void setAlphaType(float fromAlpha, float toAlpha){
AlphaAnimation animation = new AlphaAnimation(fromAlpha, toAlpha);
animation.setDuration(1800) ;
animation.setRepeatMode(Animation.REVERSE) ;
animation.setRepeatCount(2) ;
tv.startAnimation(animation) ;
}
如果是加载写好的 Xml
****.startAnimation(AnimationUtils.loadAnimation(this, R.anim.****));
运行如下 :
TranslateAnimation
TranslateAnimation对应Xml的 < translate> 标签,可以实现位置移动的动画效果,可以是垂直方向的移动,也可以是水平方向的移动。其定义属性有:
<declare-styleable name="TranslateAnimation">
<attr name="fromXDelta" format="float|fraction" />
<attr name="toXDelta" format="float|fraction" />
<attr name="fromYDelta" format="float|fraction" />
<attr name="toYDelta" format="float|fraction" />
</declare-styleable>
属性 | 含义 |
---|---|
android:fromXDelta | 起始位置的X坐标的偏移量 |
android:toXDelta | 结束位置的X坐标的偏移量 |
android:fromYDelta | 起始位置的Y坐标的偏移量 |
android:toYDelta | 结束位置的Y坐标的偏移量 |
类中字段 | xml中字段 | 描述 |
---|---|---|
Animation.ABSOLUTE | 直接写数字 如 :100 | 表示相对于 View本身的具体像素值 |
Animation.RELATIVE_TO_SELF | 以 % 结束 如:50% | 表示相对于 View本身的百分比位置 |
Animation.RELATIVE_TO_PARENT | 以 p%结束 如:50P% | 表示相对于View的父View的百分比位置 |
setTranslateType(Animation.ABSOLUTE,0,100,0,100);
setTranslateType(Animation.RELATIVE_TO_SELF,0,3,0,3);
setTranslateType(Animation.RELATIVE_TO_PARENT,0,1,0,1);
运行结果如下 :
RotateAnimation
AlphaAnimation对应Xml的 < rotate> 标签,可以实现旋转的动画效果。其定义属性有 :
<declare-styleable name="RotateAnimation">
<attr name="fromDegrees" />
<attr name="toDegrees" />
<attr name="pivotX" />
<attr name="pivotY" />
</declare-styleable>
属性 | 含义 |
---|---|
android:fromDegrees | 旋转开始的角度 |
android:toDegrees | 旋转结束的角度 |
android:pivotX | 旋转中心点的X坐标 |
android:pivotY | 旋转中心点的Y坐标 |
类中字段 | xml中字段 | 描述 |
---|---|---|
Animation.ABSOLUTE | 直接写数字 如 :100 | 表示相对于 View本身的具体像素值 |
Animation.RELATIVE_TO_SELF | 以 % 结束 如:50% | 表示相对于 View本身的百分比位置 |
Animation.RELATIVE_TO_PARENT | 以 p%结束 如:50P% | 表示相对于View的父View的百分比位置 |
setRotateType(0, 1080, Animation.ABSOLUTE, 0.5f, Animation.ABSOLUTE, 0.5f) ;
setRotateType(0, 1080, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) ;
setRotateType(0, 1080, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f) ;
运行结果如下 :
ScaleAnimation
AlphaAnimation对应Xml的 < scale> 标签,可以实现缩放的动画效果,其定义属性有:
<declare-styleable name="ScaleAnimation">
<attr name="fromXScale" format="float|fraction|dimension" />
<attr name="toXScale" format="float|fraction|dimension" />
<attr name="fromYScale" format="float|fraction|dimension" />
<attr name="toYScale" format="float|fraction|dimension" />
<attr name="pivotX" />
<attr name="pivotY" />
</declare-styleable>
属性 | 含义 |
---|---|
android:fromXScale | 动画开始时X坐标上的缩放尺寸 |
android:toXScale | 动画结束时X坐标上的缩放尺寸 |
android:fromYScale | 动画开始时Y坐标上的缩放尺寸 |
android:toYScale | 动画结束时Y坐标上的缩放尺寸 |
android:pivotX | 缩放时的固定不变的X坐标 |
android:pivotY | 缩放时的固定不变的Y坐标 |
类中字段 | xml中字段 | 描述 |
---|---|---|
Animation.ABSOLUTE | 直接写数字 如 :100 | 表示相对于 View本身的具体像素值 |
Animation.RELATIVE_TO_SELF | 以 % 结束 如:50% | 表示相对于 View本身的百分比位置 |
Animation.RELATIVE_TO_PARENT | 以 p%结束 如:50P% | 表示相对于View的父View的百分比位置 |
setScaleType(0, 2, 0, 2, Animation.ABSOLUTE, 0.5f, Animation.ABSOLUTE, 0.5f) ;
setScaleType(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) ;
setScaleType(0, 2, 0, 2, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f) ;
运行结果如下 :
AnimationSet
AnimationSet对应Xml的 < set> 标签,可以将多个动画组合起来,变成一个动画集。比如想将一张图片缩放的同时也做移动,这时候就要用标签组合缩放动画和移动动画了。其定义属性如下 :
<declare-styleable name="AnimationSet">
<attr name="shareInterpolator" format="boolean" />
<attr name="fillBefore" />
<attr name="fillAfter" />
<attr name="duration" />
<attr name="startOffset" />
<attr name="repeatMode" />
</declare-styleable>
以上的属性在 Animation中有,描述一下 shareInterpolator的属性 :android:shareInterpolator
shareInterpolator取值true或false,取true时,指在AnimationSet中定义一个插值器(interpolater),它下面的所有动画共同。如果设为false,则表示它下面的动画自己定义各自的插值器。
<set>标签在视图动画中除了可以组合<alpha>, <scale>, <translate>, <rotate>这四种标签,也可以嵌套其他<set>标签。 也可以addAnimation() 方法加入动画
public void addAnimation (Animation a)
AnimationDrawable控制逐帧动画
通过< animation-list > 标签,可以实现逐帧动画 。一般使用步骤有以下 3步:
- 在 res/drawable 文件夹下新建一个xml文件,该文件详细定义了动画播放时所用的图片、切换每张图片
- 在代码中,将该动画布局文件,赋值给特定的图片展示控件,如本例子中的ImageView。
- 通过 iv.getBackGround()获取相应的AnimationDrawable对象,然后通过该对象的方法进行控制动画
<?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/azs"
android:duration="200" />
<item
android:drawable="@drawable/azt"
android:duration="200" />
省略若干图片 ...
</animation-list>
<!--
根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
根标签下,通过item标签对动画中的每一个图片进行声明
android:duration 表示展示所用的该图片的时间长度
-->
在Java中的代码 :
private void startAnimationDrawable() {
iv.setBackgroundResource(R.drawable.animation_list);
a=(AnimationDrawable) iv.getBackground() ;
a.start() ;
}
private void stopAnimationDrawable() {
if(a.isRunning()){
a.stop() ;
}
}
运行结果:
可能会用到的方法 :
setOneShot(boolean flag) 和在配置文件中进行配置一样,可以设置动画是否播放一次,false为连续播放;
addFrame (Drawable frame, int duration) 动态的添加一个图片进入该动画中