Android 动画

来源:互联网 发布:剑网三炮姐捏脸数据 编辑:程序博客网 时间:2024/06/10 01:13

一、为什么使用?

属性动画能够改变真实的改变控件的属性,比如我们用补间动画实现了,平移并且保留了位置,但是点击事件啥的,还是在原来的位置。但是属性动画就不是

二、动画分类

      1、补间动画(Tween Animation

            a:支持四种类型平移(Translate)、旋转(Rotate)、缩放(Scale)、透明度(Alpha)

            b:特点,只是显示位置发生了改变,但是View的实际位置并未改变,比如点击事件,虽然控件移动了,但是点击事件仍然在原位置

                  组合使用比较复杂。View Animation也是补间动画

       2、帧动画(Frame Animation

           a. 用于生成连续的Gif效果图。

           b. DrawableAnimation也是指此动画

       3、属性动画(Property Animation

         a. 支持对所有View能更新的属性的动画(需要属性的setXxx()getXxx())。

         b. 更改的是View实际的属性,所以不会影响其在动画执行后所在位置的正常使用。

         c.这是Android3.0(API11以后出现的功能),3.0之前的可以使用开源库nineoldandroids

三、属性动画的使用

实现缩放效果,同一种动画实现的方式也各不相同

//1

   ObjectAnimator animator = ObjectAnimator.ofFloat(view, "haha", 0f, 100f);
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.i("wxf", "" + animation.getAnimatedFraction());//百分比
                Log.i("wxf", "" + animation.getAnimatedValue());//得到duration时间内values当中的某一个中间值

                float value = (float) animation.getAnimatedValue();
                imageView.setScaleX(0.5f + value / 200);
                imageView.setScaleY(0.5f + value / 200);

            }
        });

    propertyName:可以是知道的属性,也可以是自己随意指定的,具体怎么做,看情况

//2

 ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 100f);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                imageView.setScaleX(0.5f + value / 200);
                imageView.setScaleY(0.5f + value / 200);
            }
        });

        valueAnimator.start();

//3

 PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f,0.5f, 1f);
        PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f,1f);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(imageView,  holder2, holder3);
        objectAnimator.setDuration(200);
        objectAnimator.start();

//4

       ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.5f, 1f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 0.5f, 1f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(500);
        animatorSet.playTogether(animator2,animator3);//动画同时执行
//        animatorSet.playSequentially(animator2, animator3);//动画分开执行
        animatorSet.start();

 

    

比如抛物线效果:

   ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setDuration(8000);
//        valueAnimator.setFloatValues();
        valueAnimator.setObjectValues(new PointF(0, 0));
        //估值器
        valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {

            @Override
            public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
                //拿到每一个时间点的坐标
                PointF pointF = new PointF();
                pointF.x = 150f * (fraction * 4);//初始速度*(执行的百分比*4)

                pointF.y = 0.5f * 150f * (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.setInterpolator(new AccelerateInterpolator(5));
        valueAnimator.start();

setInterpolator的种类很多,自己可以去调试

   

有的人会说,属性动画只要实现就可以了,为什么要列举这么多种方式,其实我就是怕别人装13,我不明白而已

 

原创粉丝点击