视图动画

安卓中,一共有两种动画类型:

  • ViewAnimation(视图动画):包括TweenAnimation(补间动画)和FrameAniamtion(帧动画)
  • PropertyAnimation(属性动画):包括ValueAnimator和ObjectAnimator。

2.1视图动画标签

2.1.1 概述

视图动画有五种类型组成:alpha,scale,translate,rotate,set。

2.1.2 scale标签

scale标签用于缩放动画,动态的调整空间的尺寸

scale相关参数

  • android:fromXScale:动画开始时,控件在x轴方向相对自身缩放比例(1.0 无变化;0.5缩小一倍;2.0放大一倍)
  • android:toXScale:动画结束时,控件在x轴方向相对自身缩放比例
  • android:fromYScale:动画开始时,控件在y轴方向相对自身缩放比例
  • android:toYScale:动画结束时,控件在y轴方向相对自身缩放比例
  • android:pivotX:缩放起始点x轴坐标,可以是数值(50),百分数(50%),百分数p(50%p),如果是"50",当前视图左上角,即原点的地方+50px;如果是“50%”, 表示当前控件的左上角加上自己宽度的50%;如果是50%,表示当前控件左上角加上父控件宽度的50%
  • android:pivotY:缩放起点的y轴坐标
  • 无论pivotX,pivotY取任何值,影响的只是动画的起始位置,结束位置始终是不会变得

Animation继承属性

Animation类是所有动画的基类,该类内部实现了所有动画的基本属性,下面就介绍下各个属性的用法

  • android:fillAfter:设置为true,动画结束时,将保持动画结束时候的状态
  • android:duration:完成一次动画持续的时间,单位毫秒
  • android:fillBefore:设置为true,动画结束时,将还原到初始化状态
  • android:fillEnabled:与android:fillBefore效果相同
  • android:repeatCount:指定动画重复次数,取值“infinite”,表示无限循环
  • android:repeatMode:设定重复类型,有reverse和restart两个值。reverse表示倒序回放,restart表示重放;必须与repeatCount一起使用才能看到效果
  • android:interpolator:用于设定差值器,

2.1.3 alpha标签

用于实现渐变透明度动画效果,该标签下自身具备的属性

  • android:fromAlpha:动画开始时候的透明度,取值范围0.0~1.0;0.0表示全透明
  • android:toAlpha:动画结束时候的透明度,取值范围0.0~1.0;0.0表示全透明

2.1.4 rotate标签

用于实现移动旋转动画效果,该标签下自身具备的属性

  • android:fromDegrees:动画开始旋转时的角度位置,正值代表顺时针方向的度数,负值代表逆时针方向的度数
  • android:toDegrees:动画结束时候旋转到的角度位置,正值代表顺时针方向的度数,负值代表逆时针方向的度数
  • android:pivotY:与scale标签一致
  • android:pivotX:与scale标签一致

2.1.5 translate标签

用于实现画面变换位置移动动画效果,该标签下自身具备的属性

  • android:fromXDelta:起始点x轴坐标;可以是数值,百分数,百分数p
  • android:toXDelta:终点x轴坐标
  • android:fromYDelta:起始点y轴坐标;可以是数值,百分数,百分数p
  • android:toYDelta:终点y轴坐标

2.1.6 set标签

该标签是一个容器类标签,用于定义动画集将前面的动画标签效果组合起来,共同完成一个动画

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
    <scale
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.4"
        android:toYScale="1.4" />
    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720" />
</set>

注意:在set标签中,设置repeateCount属性是无效的,必须对每个动画单独设置才有用

2.2视图动画代码实现

2.2.1 概述

标签与所对应的动画类如下表所示

标签
scaleScaleAniamtion
alphaAlphaAniamtion
rotateRotateAniamtion
translateTranslateAniamtion
setAniamtionSet

2.2.2 ScaleAniamtion

常用的构造方法如下:

ScaleAnimation(float fromX, float toX, float fromY, float toY)
ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY)
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

scale标签中常用的属性通过构造函数传入,其中标签属性android:pivotX在构造函数中与pivotXType相对应; 其中三种取值样式,分别是数值->Animation.ABSOLUTE,百分数->Animation.RELATIVE_TO_SELF,百分数p->Animation.RELATIVE_TO_PARENT;

等价代码

//xml代码
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="50%"
    android:duration="700"
    android:pivotY="50%"
    android:toXScale="1.4"
    android:toYScale="1.4" />
//Java代码
 ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF , 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(700);
            viewById.startAnimation(scaleAnimation);

2.2.3 AlphaAnimation

常用的构造方法如下:

AlphaAnimation(float fromAlpha, float toAlpha)

alpha标签中常用的属性通过构造函数传入
等价代码

//xml代码
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.1"
    android:duration="700" />
//Java代码
	 AlphaAnimation animation = new AlphaAnimation(1.0f, 0.1f);
	animation.setDuration(700);
	viewById.startAnimation(animation);

2.2.4 RotateAniamtion

常用的构造方法如下:

RotateAnimation(float fromDegrees, float toDegrees)
RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

rotate 标签中常用的属性通过构造函数传入
等价代码

//xml代码
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="-650"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="true"
    android:duration="3000" />
//Java代码
	RotateAnimation animation = new RotateAnimation(0,-650,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f);
	animation.setDuration(700);
    animation.setFillAfter(true);
    viewById.startAnimation(animation);

2.2.5 TranslateAniamtion

常用的构造方法如下:

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

translate标签中常用的属性通过构造函数传入,其中第一个函数使用的是绝对值对应的Type,全部是Animation.ABSOLUTE。第二个构造函数中针对每个值对应的type与上面取值情况相同。

等价代码

//xml代码
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:toXDelta="-80"
    android:fromYDelta="0"
    android:toYDelta="-80"
    android:fillBefore="true"
    android:duration="2000" />
//Java代码
    TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,
                    -80,Animation.ABSOLUTE,0,Animation.ABSOLUTE,-80);
            animation.setDuration(2000);
            animation.setFillBefore(true);
            viewById.startAnimation(animation);

2.2.6 AniamtionSet

常用的构造方法如下:

AnimationSet(boolean shareInterpolator)

translate标签中shareInterpolator取值有两个true和false,当为true时,在AnimationSet中定义一个差值器,下面所有动画共用该差值器,当为false时,表示下面所有动画定义各自的差值器;
等价代码

//xml代码
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="3000"
     android:fillAfter="true">
    <alpha
            android:fromAlpha="0.0"
            android:toAlpha="1.0"/>
    <scale
            android:fromXScale="0.0"
            android:toXScale="1.4"
            android:fromYScale="0.0"
            android:toYScale="1.4"
            android:pivotX="50%"
            android:pivotY="50%"/>
    <rotate
            android:fromDegrees="0"
            android:toDegrees="720"
            android:pivotX="50%"
            android:pivotY="50%"/>
</set>
//Java代码
     Animation alpha_Anim = new AlphaAnimation(1.0f, 0.1f);
     Animation scale_Anim = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
     Animation rotate_Anim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

     AnimationSet setAnim = new AnimationSet(true);
     setAnim.addAnimation(alpha_Anim);
     setAnim.addAnimation(scale_Anim);
     setAnim.addAnimation(rotate_Anim);

     setAnim.setDuration(3000);
     setAnim.setFillAfter(true);
     tv.startAnimation(alphaAnim);

2.2.7 Aniamtion

其他常用的API

animation.cancel();

取消动画

animation.reset();

将控件重置到动画开始前状态

animation.setAnimationListener();

设置动画监听,其中 Animation.AnimationListener回调方法如下:

//动画开始时函数回调
void onAnimationStart(Animation animation);
//动画结束时函数回调
void onAnimationEnd(Animation animation) ;
//动画重复时函数回调
void onAnimationRepeat(Animation animation);

2.3差值器初探

作用:主要用来设置动画的变化速率

java类xml资源id
AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator
AccelerateInterpolator@android:anim/accelerate_interpolator
DecelerateInterpolator@android:anim/decelerate_interpolator
LinearInterpolator@android:anim/linear_interpolator
AnticipateInterpolator@android:anim/anticipate_interpolator
AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator
OvershootInterpolator@android:anim/overshoot_interpolator
BounceInterpolator@android:anim/bounce_interpolator
CycleInterpolator@android:anim/cycle_interpolatorr

2.3.1 AccelerateDecelerateInterpolator

加速减速差值器:在开始和结束的地方速率改变比较慢,在中间的时候加速。
在这里插入图片描述

2.3.2 AccelerateInterpolator

加速差值器:在开始的地方速率改变比较慢,然后开始加速。
在这里插入图片描述

2.3.3 DecelerateInterpolator

减速差值器:在开始的时候加速到最大值,然后逐渐变慢。
在这里插入图片描述

2.3.4 LinearInterpolator

线性差值器(匀速差值器):它的速率是保持恒定的。
在这里插入图片描述

2.3.5 BounceInterpolator

弹跳差值器;模拟了自由落体后回弹的效果
在这里插入图片描述

2.3.6 AnticipateInterpolator

初始偏移差值器,表示在动画开始的时候向前偏移一段距离,然后应用动画。(就像跳远一样,先向后退几步,增大助跑距离)
在这里插入图片描述
其中AnticipateInterpolator还有一个构造函数

public AnticipateInterpolator(float tension)

参数tension对应的xml属性为android:tension,表示张力值,默认为2,值越大,初始的偏移量越大,而且速度越快。

2.3.7 OvershootInterpolator

结束偏移差值器,表示在动画结束的时候,沿动画的方向继续运动一段距离后在结束动画。(就像跑步速度过快一样,没有留住,跑过终点了,然后再往回跑,回到终点)
在这里插入图片描述

public OvershootInterpolator(float tension)

2.3.8 AnticipateOvershootInterpolator

是AnticipateInterpolator与OvershootInterpolator的合体;在动画开始的时候向前偏移一段距离,在动画结束的时候向后偏移一段距离。
在这里插入图片描述

public AnticipateOvershootInterpolator(float tension) 
public AnticipateOvershootInterpolator(float tension, float extraTension) 
  • 参数extraTension表示额外的张力值,对应xml属性为android:extraTension, 默认为1.5

2.3.9 CycleInterpolator

循环差值器,表示动画循环播放特定的次数,速率沿正弦曲线变化
在这里插入图片描述

public CycleInterpolator(float cycles) 
  • 参数cycles,对应xml中属性:android:cycles ;cycles是周期值,默认为1,cycles=2表示动画会执行两次
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值