Android Animation动画

前言

补间动画:给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。只能应用于View对象,而且只支持一部分属性,主要分为四大类移动,缩放,旋转,透明。

1. 使用方式

  • 在XML资源中定义,使用AnimationUtils.loadAnimation(Context, int)方式加载
    • duration,属性为动画持续时间,时间以毫秒为单位
    • fillAfter,当设置为true,该动画转化在动画结束后状态
    • fillBefore,当设置为true,该动画转化在动画开始前状态
    • interpolator,指定一个动画的插入器
    • repeatCount,动画的重复次数
    • repeatMode,定义重复的行为,"restart""reverse"
    • startOffset,动画之间的时间间隔,从上次动画停多少时间开始执行下个动画
  • 定义Animation的子类
    • TranslateAnimation,画面转换位置移动动画效果
    • ScaleAnimation,渐变尺寸伸缩动画效果
    • RotateAnimation,画面转移旋转动画效果
    • AlphaAnimation,渐变透明度动画效果
    • AnimationSet,动画效果集合

2. TranslateAnimation

主要配置

  • fromXDelta,属性为动画起始时X坐标上的位置,直接定义或者使用百分比
  • toXDelta,属性为动画结束时X坐标上的位置,直接定义或使用百分比
  • fromYDelta,属性为动画起始时Y坐标上的位置,直接定义或使用百分比
  • toYDelta,属性为动画结束时Y坐标上的位置,直接定义或使用百分比

百分比可以是"100%“,表示自身的100%,也就是从View自己的位置开始。也可以表示为"80%p”,表示父层View的80%,是以它父层View为参照的。

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:toXDelta="100%"
    android:duration="3000" />

代码实现

Animation animation = new TranslateAnimation(0, 0, Animation.RELATIVE_TO_SELF, 1.0f,
        0, 0, 0, 0);
animation.setDuration(3000);

XTypeYType

Animation.ABSOLUTE // 绝对值
Animation.RELATIVE_TO_SELF // 相对于自身位置
Animation.RELATIVE_TO_PARENT // 相对于父控件位置

这里写图片描述

3. ScaleAnimation

主要配置

  • pivotX,动画相对于物件的X坐标的开始位置,直接定义或使用百分比0%~100%
  • pivotY,动画相对于物件的Y坐标的开始位置,直接定义或使用百分比0%~100%
  • fromXScale,动画起始时X坐标上的伸缩尺寸
  • toXScale,动画结束时X坐标上的伸缩尺寸
  • fromYScale,动画起始时Y坐标上的伸缩尺寸
  • toYScale,动画结束时Y坐标上的伸缩尺寸

0.0表示收缩到没有,1.0表示正常无伸缩,值小于1.0表示收缩,值大于1.0表示放大

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromXScale="1"
    android:toXScale="0.5"
    android:fromYScale="1"
    android:toYScale="0.5"
    android:duration="3000" />

代码实现

Animation animation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);

这里写图片描述

4. RotateAnimation

主要配置

  • pivotX,动画相对于物件的X坐标的开始位置,直接定义或使用百分比0%~100%
  • pivotY,动画相对于物件的Y坐标的开始位置,直接定义或使用百分比0%~100%
  • fromDegrees,为动画起始时物件的角度
  • toDegrees,属性为动画结束时物件旋转的角度

当角度为负数表示逆时针旋转,当角度为正数表示顺时针旋转。

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:duration="3000" />

代码实现

Animation animation = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000);

这里写图片描述

5. AlphaAnimation

主要配置

  • fromAlpha,属性为动画起始时透明度
  • toAlpha,属性为动画结束时透明度

1为不透明,0为完全透明

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1"
    android:toAlpha="0"
    android:duration="3000" />

代码实现

Animation animation = new AlphaAnimation(1.0f, 0f);
animation.setDuration(3000);

这里写图片描述

6. AnimationSet

AnimationSet为动画集合,可同时运行多个动画。

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000">
    <scale
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromXScale="0.5"
        android:toXScale="1"
        android:fromYScale="0.5"
        android:toYScale="1" />

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

代码实现

AnimationSet animation = new AnimationSet(true);

Animation alphaAnim = new AlphaAnimation(0f, 1.0f);
animation.addAnimation(alphaAnim);

Animation scaleAnim = new ScaleAnimation( 0.5f, 1.0f, 0.5f, 1.0f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
animation.addAnimation(scaleAnim);

animation.setDuration(3000);

这里写图片描述

7. 控制动画

开始动画

  • 调用View.startAnimation(Animation)方法开始。
  • 调用View.setAnimation(Animation)Animation.start()方法开始。

取消动画

  • 调用View.clearAnimation()方法。
  • 调用Animation.cancel()方法。

ViewstartAnimation(Animation)方法中,设置了新的Animation,并且刷新界面。

public void startAnimation(Animation animation) {
    animation.setStartTime(Animation.START_ON_FIRST_FRAME);
    setAnimation(animation);
    invalidateParentCaches();
    invalidate(true);
}

ViewGroupdispatchDraw(Canvas)方法里面调用drawChild(Canvas, View, long)方法,该方法最终会调用Animation.getTransformation(long, Transformation, float)使动画生效。

protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    return child.draw(canvas, this, drawingTime);
}

boolean draw(Canvas canvas, ViewGroup parent, long drawingTime) {
    final Animation a = getAnimation();
    if (a != null) {
        more = applyLegacyAnimation(parent, drawingTime, a, scalingRequired);
    }
}

private boolean applyLegacyAnimation(ViewGroup parent, long drawingTime,
        Animation a, boolean scalingRequired) {
        ... ...
    boolean more = a.getTransformation(drawingTime, t, 1f);
}

AnimationListener是对Animation设置监听器,在Animation动画效果开始执行前,执行完毕和重复执行时可以触发监听器。

  • onAnimationStart(Animation animation),动画开始
  • onAnimationEnd(Animation animation),动画结束
  • onAnimationRepeat(Animation animation),动画重复

8. Interpolator

Interpolator 被用来修饰动画效果,定义动画的变化率。

  • AccelerateDecelerateInterpolator,在动画开始与结束的地方速率改变比较慢,在中间的时候加速
  • AccelerateInterpolator,在动画开始的地方速率改变比较慢,然后开始加速
  • AnticipateInterpolator,开始的时候向后然后向前甩
  • AnticipateOvershootInterpolator,开始的时候向后然后向前甩一定值后返回最后的值
  • BounceInterpolator,动画结束的时候弹起
  • CycleInterpolator,动画循环播放特定的次数,速率改变沿着正弦曲线
  • DecelerateInterpolator,在动画开始的地方快然后慢
  • LinearInterpolator,以常量速率改变
  • OvershootInterpolator,向前甩一定值后再回到原来位置。

相关文章
Animation动画
帧动画
属性动画
组件动画
Transition动画

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值