Android 动画(属性动画基本使用)

本文详细介绍了Android中的帧动画、View动画及属性动画,并深入探讨了ValueAnimator、ObjectAnimator的应用及组合动画的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、帧动画

  • 将一张张单独的图片连贯的进行播放

二、View 动画(补间动画)

  • 动画效果
    alpha(淡入淡出)
    translate(位移)
    scale(缩放大小)
    rotate(旋转)

  • 只能够作用在View上,对于非 View 对象不能使用,例如自定义 View 中的 Point 。

  • 只能实现以上几种动画,没有扩展性。

  • 只能改变 View 的效果,不能改变 View 的属性。

三、属性动画(Android 3.0)

参考:Android属性动画完全解析

在 Animator 框架中使用最多的就是 AnimatorSet 和 ObjectAnimator 配合:使用 ObjectAnimator 进行更精细化的控制,控制一个对象和一个属性值,而使用多个 ObjectAnimator 组合到 AnimatorSet 形成一个动画。

1. valueAnimator

  • 本身不提供任何动画效果,它更像一个数值发生器,用来产生有一定规律的数字。
  • 负责计算初始值和结束值之间的动画过渡,管理动画的播放次数、播放模式、对动画设置监听等。
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f);
        animator.setDuration(300);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentValue = (float) animation.getAnimatedValue();
                Log.d("TAG", "currentValue:" + currentValue);
            }
        });
        animator.start();
        ValueAnimator animator = ValueAnimator.ofFloat(0f, 5f, 3f, 10f);
        ValueAnimator animator = ValueAnimator.ofInt(0, 100);

设置延迟

        animator.setStartDelay(2000);

循环播放次数

        animator.setRepeatCount(3);

循环播放模式

        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatMode(ValueAnimator.REVERSE);

2. ObjectAnimator

  • 直接对任意对象的任意属性进行动画操作。
  • 内部通过反射机制调用 set 方法来修改对象的属性值。
  • 继承自ValueAnimator的,底层的动画实现机制也是基于ValueAnimator来完成的。
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);
        objectAnimator.setDuration(3000);
        objectAnimator.start();
  • 变化属性
ofFloat(textview, "alpha", 1f, 0f, 1f)//透明度
ofFloat(textview, "rotation", 0f, 360f)//旋转
ofFloat(textview, "rotationX", 0f, 360f)//围绕x轴翻转
ofFloat(textview, "rotationY", 0f, 360f)//围绕y轴翻转
ofFloat(imageView, "translationX", 100f, -500f, 300f)//在x轴平移
ofFloat(imageView, "translationY", 100f, -500f, 300f)//在y轴平移
ofFloat(imageView, "scaleX", 1f, 0.5f, 3f)//横向缩放
ofFloat(imageView, "scaleY", 1f, 0.5f, 3f)//纵向缩放

3. 动画的监听

  • Animator类当中提供了一个 addListener 方法,ObjectAnimator 继承自 ValueAnimator 的,ValueAnimator 继承自Animator,因此都可以监听。
  • 完整的动画具有 start、end、cancel、repeat 四个过程,实现 AnimatorListener。
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
  • 只需要实现部分方法 AnimatorListenerAdapter
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
            }
        });

4. 组合动画——AnimatorSet

  • AnimatorSet 类提供了一个 play() 方法,向这个方法中传入一个 Animator 对象(ValueAnimator 或 ObjectAnimator),将会返回一个 AnimatorSet.Builder 的实例。
  • AnimatorSet.Builder 中包括了 4 个方法
    after(Animator anim):将现有动画插入到传入的动画之后执行。
    after(long delay):将现有动画延迟指定毫秒后执行。
    before(Animator anim):将现有动画插入到传入的动画之前执行。
    with(Animator anim):将现有动画和传入的动画同时执行。
        ObjectAnimator translationX = ObjectAnimator.ofFloat(imageView, "translationX", -300f, 0f);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(translationX).with(rotation).after(alpha);
        animSet.setDuration(3000);
        animSet.start();

5. 组合动画——PropertyValuesHolder

  • 相比于 AnimatorSet,PropertyValuesHolder 只能是多个动画同时执行
        PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat("translationX", -300f, 0f);
        PropertyValuesHolder rotation = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(imageView, translationX, rotation, alpha);
        animator.setDuration(3000);
        animator.start();

6. 在 XML 中使用属性动画

  • 属性动画的 xml 文件都应该存放在 animator 文件夹中。
  • XML文件中我们一共可以使用如下三种标签。
    <animator> 对应代码中的 ValueAnimator
    <objectAnimator> 对应代码中的 ObjectAnimator
    <set> 对应代码中的 AnimatorSet
<animator xmlns:android="http://schemas.android.com/apk/res/android"
    android:valueFrom="0"
    android:valueTo="100"
    android:valueType="intType"/>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:valueFrom="1"
    android:valueTo="0"
    android:valueType="floatType"
    android:propertyName="alpha"/>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially" >
 
    <objectAnimator
        android:duration="2000"
        android:propertyName="translationX"
        android:valueFrom="-500"
        android:valueTo="0"
        android:valueType="floatType" >
    </objectAnimator>
 
    <set android:ordering="together" >
        <objectAnimator
            android:duration="3000"
            android:propertyName="rotation"
            android:valueFrom="0"
            android:valueTo="360"
            android:valueType="floatType" >
        </objectAnimator>
 
        <set android:ordering="sequentially" >
            <objectAnimator
                android:duration="1500"
                android:propertyName="alpha"
                android:valueFrom="1"
                android:valueTo="0"
                android:valueType="floatType" >
            </objectAnimator>
            <objectAnimator
                android:duration="1500"
                android:propertyName="alpha"
                android:valueFrom="0"
                android:valueTo="1"
                android:valueType="floatType" >
            </objectAnimator>
        </set>
    </set>
 
</set>
  • 加载 xml 文件并启动动画
    loadAnimator 加载 xml 文件
    setTarget 设置到某一个对象上
    start 启动动画
Animator animator = AnimatorInflater.loadAnimator(context, R.animator.anim_file);
animator.setTarget(view);
animator.start();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值