Android开发之安卓属性动画大总结

本文详细介绍安卓属性动画的基础用法及高级技巧,包括平移、旋转等基本动画效果,以及如何通过监听器、`ValueAnimator`、`PropertyValuesHolder`和`AnimatorSet`实现复杂的动画组合。同时提供抛物线动画案例,并介绍不同类型的动画插值器。

前言:之前就一直想做一个安卓动画的知识点的归纳,趁这个机会,就把安卓的属性动画归纳起来,包括里面的基本用法,使用的时候注意事项等,属性动画安卓3.0之后提出的新的动画属性,和安卓2.3之前的补间动画有着本质的区别,正如其名字那样,属性动画,改变的是其真正的属性,如果你想兼容安卓3.0以下的,可以使用第三方的jar包“nineOldAndroid”,好了,下面开始今天的属性动画!

--------------------------分割线----------------------------------

一:普通属性动画

1.平移效果x方向

ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationX", 0f, 300f);
oa.setDuration(500);
oa.start();
2.平移效果y方向

ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationY", 0f, 300f);
oa.setDuration(500);
oa.start();
3.方向x旋转:

ObjectAnimator oa = ObjectAnimator.ofFloat(view, "rotationX", 0f, 360f);
oa.setDuration(4000);
oa.start();
4.......
--------------------------分割线----------------------------------

二:多个动画同时执行

1:设置动画监听,开始第一个动画同时开启其他动画
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "hello", 0f, 100f);//没有这个属性的时候就是valueanimator
        animator.setDuration(500);
        //设置动画监听
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //动画在执行过程中不断调用此方法
                animation.getAnimatedFraction();//百分比
                //得到duration时间内 values当中的某一个中间值。0f~100f
                float value = (float) animation.getAnimatedValue();
                System.out.println("---->value:" + value);
                imageView.setScaleX(0.5f + value / 200);//0.5~1
                imageView.setScaleY(0.5f + value / 200);//0.5~1
            }
        });
        animator.start();
        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) {

            }
        });

2.用ValueAnimator
ValueAnimator animator = ValueAnimator.ofFloat(0f, 100f);
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //动画在执行过程中不断调用此方法
                animation.getAnimatedFraction();//百分比
                //得到duration时间内 values当中的某一个中间值。0f~100f
                float value = (float) animation.getAnimatedValue();
                System.out.println("---->value:" + value);
                imageView.setScaleX(0.5f + value / 200);//0.5~1
                imageView.setScaleY(0.5f + value / 200);//0.5~1
            }
        });
        animator.start();
3.使用PropertyValuesHolder。
        //float...values:代表关键帧的值
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.7f, 1f);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.7f, 1f);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.7f, 1f);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(imageView, holder1, holder2, holder3);
        animator.setDuration(1000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatedValue = (float) animation.getAnimatedValue();
                float animatedFraction = animation.getAnimatedFraction();
                long playTime = animation.getCurrentPlayTime();
                System.out.println("animatedValue:" + animatedValue + ",  playTime:" + playTime);
            }
        });
        animator.start();

4.使用AnimatorSet
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.7f, 1f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.7f, 1f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.7f, 1f);

        //AnimationSet animationSet = new AnimationSet();
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(500);
        //animatorSet.play(animator1);//执行单个动画
        //animatorSet.playTogether(animator1, animator2, animator3);//同时执行
        animatorSet.playSequentially(animator1, animator2, animator3);//依次执行动画
        animatorSet.start();

--------------------------分割线----------------------------------
案列:实现抛物线效果。
注意:x是匀速,y是加速y = 1/2*g*t*t(初中物理公式)。
ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setDuration(4000);
        //valueAnimator.setFloatValues(values);
        valueAnimator.setObjectValues(new PointF(0, 0));
        //估值器---定义计算规则
        valueAnimator.setEvaluator(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                //拿到时间点的每一个坐标
                //x = v*t
                PointF pointF = new PointF();
                pointF.x = 100f * (fraction * 4);
                //pointF.y = 0.5f * 9.8f * (fraction * 4) * (fraction * 4);
                pointF.y = 0.5f * 98f * (fraction * 4) * (fraction * 4);
                return pointF;
            }
        });

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //得到此时时间点的坐标
                PointF pointF = (PointF) animation.getAnimatedValue();

                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
            }
        });
        valueAnimator.start();

--------------------------分割线----------------------------------
这几个是google给我们封装好的实现各种动画的轨迹。
        ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 1100f);
        oa.setDuration(2000);
        //设置加速器---
		oa.setInterpolator(new AccelerateInterpolator(5));
//		oa.setInterpolator(new AccelerateDecelerateInterpolator());
//		oa.setInterpolator(new AnticipateInterpolator(8));
//		oa.setInterpolator(new OvershootInterpolator());
//		oa.setInterpolator(new CycleInterpolator(4));
//      oa.setInterpolator(new BounceInterpolator());

        oa.start();

图就没有了,大家可以自己去实现一下。
--------------------------完!----------------------------------





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等待着冬天的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值