Android 补间动画、帧动画和属性动画开发大全

本文详细介绍了Android中的三种动画类型:补间动画、帧动画和属性动画。补间动画包括平移、缩放、旋转、透明度等效果,而属性动画则突破了补间动画的局限,能够作用于任意对象的属性。文章通过实例讲解了如何实现这些动画,包括使用TimeInterpolator控制动画速度,以及ObjectAnimator和ViewPropertyAnimator的使用。此外,还探讨了Lottie和腾讯PAG两个动画框架的优缺点和应用场景。
摘要由CSDN通过智能技术生成

一、前言

为了使用户的交互更加流畅自然,动画也就成为了一个应用中必不可少的元素之一。在 Android 中常用的动画分类无外乎三种,最早的 帧动画 、补间动画 以及 3.0 之后加入的 属性动画,是它们组成了 Android 中各种炫酷亮眼的动画效果。这里是官方的动画相关开发文档链接

二、动画

2.1 动画分类

Android中有三种动画,分别是补间动画(Tween Animation),帧动画(Frame Animation)和属性动画(Property Animation)。

2.2 补间动画

这类动画比较简单,一般就是平移、缩放、旋转、透明度,或者其组合,可以用代码或者xml文件的形式。
四个动画效果实现类:TranslateAnimation、ScaleAnimation、RotateAnimation、AlphaAnimation、组合动画实现类:AnimationSet,对应的的XML标签为translate、 scale、 rotate、alpha、set,其中set里还可以放set,然后放在放置在res/anim/目录下。

2.2.1 平移(TranslateAnimation)

共有属性:
duration表示这一次动画持续的时间
fillAfter表示动画结束时,是否保持最后一帧的样子
fillBefore表示动画结束时,是否保持第一帧的样子
repeatCount表示动画循环的次数,默认为 0 次不循环,-1 为无限循环。
repeatMode表示是循环的模式,reverse 是从一次动画结束开始,restart 是从动画的开始处循环
interpolator是一个插值器资源,它可以控制动画的播放速度
shareInterpolator表示是否与 set 中其他动画共享插值器,false为各自使用各自的插值器

平移动画属性:
fromXDelta
fromYDelta
起始时,X/Y 方向的位置
toXDelta
toYDelta
终止时,X/Y 方向的位置
这四个属性都支持同样的单位,是三种表达方式,浮点数、num% 和 num%p

  • 浮点数 位置为 View 的左边距/上边距 + 此数值 正数为右,负数为左
  • num% 位置为 View 的左边距/上边距 + View宽的百分之num 正数为右,负数为左
  • num%p 位置为 View 的左边距/上边距 + 父容器的百分之num 正数为右,负数为左

动画效果:
在这里插入图片描述

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="300"
    android:toYDelta="300"
    android:repeatCount="-1"
    android:repeatMode="reverse"/>
Animation translateAnim = AnimationUtils.loadAnimation(this, R.anim.view_translate);
view.startAnimation(translateAnim);

2.2.2 缩放(ScaleAnimation)

属性:
fromXScale
fromYScale
代表缩放时,X/Y 坐标起始大小,浮点值,0.5代表自身的一半,2.0代表自身的两倍大小。
toXScale
toYScale
代表缩放时,X/Y 缩放结束时候大小。
pivotX浮点数。在对象缩放时要保持不变的 X 坐标。
pivotY浮点数。在对象缩放时要保持不变的 Y 坐标。

动画效果:
在这里插入图片描述

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0.1"
    android:fromYScale="0.1"
    android:pivotX="0"
    android:pivotY="0"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:repeatCount="-1"
    android:repeatMode="reverse"/>
Animation scaleAnim = AnimationUtils.loadAnimation(this, R.anim.view_scale);
view.startAnimation(scaleAnim);

2.2.3 旋转(RotateAnimation)

属性:
fromDegrees 起始角度 单位度 浮点值
toDegrees 结尾角度 单位度 浮点值
pivotX旋转中心点的 X 坐标,这个数值有三种表达方式
pivotY旋转中心点的 Y 坐标,这个数值有三种表达方式

  • 纯数字 例如 20 ,代表相对于自身左边缘或顶边缘 + 20 像素
  • num% 代表 相对于自身左边缘或顶边缘 + 自身宽 的百分之 num
  • num%p 代表相对于自身左边缘或顶边缘 + 父容器 的百分之 num

动画效果:
在这里插入图片描述

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="-1"
    android:repeatMode="reverse"
    android:toDegrees="1800" />
Animation rotateAnim = AnimationUtils.loadAnimation(this, R.anim.view_rotate);
view.startAnimation(rotateAnim);

2.2.4 透明度(AlphaAnimation)

属性:
fromAlpha表示动画开始时的透明度
toAlpha 表示动画结束时候的透明度
取值为[0.0,1.0],0代表完全透明,1代表不透明。

动画效果:
在这里插入图片描述

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromAlpha="0"
    android:repeatCount="-1"
    android:repeatMode="reverse"
    android:toAlpha="1" />
Animation scaleAnim = AnimationUtils.loadAnimation(this, R.anim.view_scale);
view.startAnimation(scaleAnim);

2.2.5 组合(AnimationSet)

动画效果:
在这里插入图片描述

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true"
    android:fillAfter="true"
    android:duration="15000">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1" />
    <rotate
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720" />
    <scale
        android:fromXScale="0.1"
        android:fromYScale="0.1"
        android:pivotX="0%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0" />

    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="300"
        android:toYDelta="300" />
</set>

java代码:

AnimationSet set = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.view_animation_set);
view.startAnimation(set);
//动画监听
set.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});
//结束动画
//view.clearAnimation();

2.2.6 TimeInterpolator 时间插值器

TimeInterpolator的作用是根据时间流逝的百分比来计算出当前属性值改变的百分比。负责控制动画变化的速率,使得基本的动画效果能够以匀速、加速、减速、抛物线速率等各种速率变化。其实质是一个数学函数 y = f(x),定义域 x 属于 (0.0,1.0) 的 float 值,值域 y 也是 (0.0,1.0) 的 float 值

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值