欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android动画详解-补间动画-AnimationSet

程序员文章站 2024-03-24 08:28:40
...

在之前呢,我们使用的都是单个补间动画的效果,当然这几种效果可以进行叠加使用,无论是相同种类还是不同种类之间都可以。

那么我们准备实现怎样的动画呢?
我实事先准备一个圆形图片(随便P的)放在屏幕的左上角,让它模拟平抛运动到达屏幕的右下角,同时小球绕中心旋转。

通过Java代码实现动画效果

AnimationSet animationSet = new AnimationSet(false);
            animationSet.setFillAfter(true);
            TranslateAnimation animationX = new TranslateAnimation(0, width - image_x, 0, 0);
            animationX.setDuration(4000);
            TranslateAnimation animationY = new TranslateAnimation(0, 0, 0, height - image_y);
            animationY.setInterpolator(new AccelerateInterpolator());
            animationY.setDuration(4000); 
            RotateAnimation rotateAnimation=new RotateAnimation(0, 1080, image_x/2, image_x/2);
            rotateAnimation.setDuration(4000);
            rotateAnimation.setInterpolator(new AccelerateInterpolator());
            animationSet.addAnimation(rotateAnimation);
            animationSet.addAnimation(animationX);
            animationSet.addAnimation(animationY);
            imageView1.startAnimation(animationSet);
            animationSet.start();

注:animationY.setInterpolator(new AccelerateInterpolator());
设置运动的速度方式,该方式是内置的匀加速运动,默认是匀速运动。

附相关速度参数:

/**
     * android:interpolator="@android:anim/accelerate_interpolator"
     * 设置动画为加速动画(动画播放中越来越快)
     * 
     * android:interpolator="@android:anim/decelerate_interpolator"
     * 设置动画为减速动画(动画播放中越来越慢)
     * 
     * android:interpolator="@android:anim/accelerate_decelerate_interpolator"
     * 设置动画为先加速在减速(开始速度最快 逐渐减慢)
     * 
     * android:interpolator="@android:anim/anticipate_interpolator"
     * 先反向执行一段,然后再加速反向回来(相当于我们弹簧,先反向压缩一小段,然后在加速弹出)
     * 
     * android:interpolator="@android:anim/anticipate_overshoot_interpolator"
     * 同上先反向一段,然后加速反向回来,执行完毕自带回弹效果(更形象的弹簧效果)
     * 
     * android:interpolator="@android:anim/bounce_interpolator"
     * 执行完毕之后会回弹跳跃几段(相当于我们高空掉下一颗皮球,到地面是会跳动几下)
     * 
     * android:interpolator="@android:anim/cycle_interpolator"
     * 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)
     * 
     * android:interpolator="@android:anim/linear_interpolator" 线性均匀改变
     * 
     * android:interpolator="@android:anim/overshoot_interpolator" 加速执行,结束之后回弹
     */

AnimationSet当然也可以通过xml定义实现,但是本例中不能使用,主要是需要在代码中动态的获取屏幕的高度和宽度,而在xml都是通过相对控件的相对比例进行变换的。