Android 属性动画整理

传统动画Animation
属性动画Animator

传统动画Animation

系统不断的进行调用ondraw方法来实现重新绘制

属性动画Animator

是通过对属性的get和set方法对属性的真是的值进行改变

传统动画的局限性,只是对动画的位置进行了改变,属于是障眼法,但是根本的最底层的位置还是没有改变.图片如下,当在初始位置点击时候可以弹出Toast,但是在平移了之后,点击是没有反应的,但是点击最初的已经成为空白的位置的时候,诡异的事情发生了.可以弹出Toast.

Android 属性动画整理_第1张图片
s.gif

传统动画代码如下:

        TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0);
        animation.setDuration(1000);
        animation.setFillAfter(true);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        imageView.startAnimation(animation);

现在用属性动画来做,代码如下:

        /**
         * 一参,操作的对象
         * 二参,操作对象的属性
         * 三参,操作对象的属性的值变化范围
         * 最后设置时间,然后start
         */
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f)
                .setDuration(1000)
                .start();

运行结果如下:

Android 属性动画整理_第2张图片
s1.gif

属性动画是真正的改变动画的值

既然能这么用,那就多来几个试试效果:

        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f)
                .setDuration(1000)
                .start();
        ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f)
                .setDuration(1000)
                .start();
        ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f)
                .setDuration(1000)
                .start();
Android 属性动画整理_第3张图片
s2.gif

可以发现,点击之间的三个动画是同步进行的.一起完成的.当然除了用这种方法之外呢,还有一种方法:

        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0f, 200f);
        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY", 0f, 200f);
        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
        ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2, p3).setDuration(1000).start();

通过PropertyValuesHolder 类来实现每一个动画,然后在ObjectAnimator的ofPropertyValuesHolder方法传入要用动画的对象,然后start开始即可..
那么这两种方式有啥区别呢,很明显第一种实现方式代码量少,第二种方式代码量多,本着存在即为合理的想法,肯定是第一种对内存来耗好用比较大,第二种对内存来说耗用比较小了.实际上Google也是这样说的.

当然还有第三种实现方式:

        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator1,animator2,animator3);
        set.setDuration(1000);
        set.start();

就是用set集合去设置每个子元素的添加,然后作用在一起的动画,运行之后的效果和之前的是一模一样的,此处就不贴图了..
当然还有按照顺序来执行,按照顺序来一个个依次执行,只需要将上方代码中的playTogether方法替换为playSequentially即可

Android 属性动画整理_第4张图片
s3.gif

先X平移,在Y平移,最后旋转.
那么还可以怎么玩呢,还可以让X平移和Y平移一起,它俩完了以后再来个旋转

        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
        AnimatorSet set = new AnimatorSet();
//        set.playSequentially(animator1,animator2,animator3);
        set.play(animator1).with(animator2);
        set.play(animator3).after(animator1);
        set.setDuration(1000);
        set.start();
Android 属性动画整理_第5张图片
s4.gif

接下来,搞一下属性动画的监听;

 public void click(View view) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
        animator.setDuration(1000);
        //此种监听是通过adapter来实现动画执行的过程的中的某一个阶段的监听
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                // TODO: 2017/1/20
            }
        });

        //此种监听是监听整个动画的开始与结束以及取消和重复的模式等监听
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                // TODO: 
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                // TODO: 
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                // TODO:
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                // TODO: 
            }
        });

    }

卫星菜单:

package com.ccstest.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import com.ccstest.R;

import java.util.ArrayList;
import java.util.List;

public class AnimActivity extends AppCompatActivity implements View.OnClickListener {

    private int[] res = {R.id.imageView_a, R.id.imageView_b, R.id.imageView_c, R.id.imageView_d,
            R.id.imageView_e, R.id.imageView_f, R.id.imageView_g, R.id.imageView_h};
    private List mImageViews = new ArrayList<>();
    private boolean flag = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim);

        for (int i = 0; i < res.length; i++) {
            ImageView img = (ImageView) findViewById(res[i]);
            img.setOnClickListener(this);
            mImageViews.add(img);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.imageView_a:
                if (flag) {
                    startAnim();
                } else {
                    closeAnim();
                }
                break;
            default:
                Toast.makeText(AnimActivity.this, "click = " + view.getId(), Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void closeAnim() {
        for (int i = 0; i < res.length; i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViews.get(i), "translationY", i * 200, 0);
            animator.setDuration(500);
            animator.setStartDelay(i * 100);
            animator.setInterpolator(new BounceInterpolator());
            animator.start();
            flag = true;
        }

    }

    private void startAnim() {
        for (int i = 0; i < res.length; i++) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(mImageViews.get(i), "translationY", 0f, i * 300);
            animator.setDuration(500);
            animator.setInterpolator(new BounceInterpolator());
            animator.setStartDelay(i * 100);
            animator.start();
            flag = false;
        }


    }

//    public void click(View view) {
//        Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
//    }
//
//    public void move(View view) {
//        TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0);
//        animation.setDuration(1000);
//        animation.setFillAfter(true);
//        ImageView imageView = (ImageView) findViewById(R.id.imageView);
//        imageView.startAnimation(animation);
    /**
     * 一参,操作的对象
     * 二参,操作对象的属性
     * 三参,操作对象的属性的值变化范围
     * 最后设置时间,然后start
     */
//        ImageView imageView = (ImageView) findViewById(R.id.imageView);
//        ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f)
//                .setDuration(1000)
//                .start();
//        ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f)
//                .setDuration(1000)
//                .start();
//        ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f)
//                .setDuration(1000)
//                .start();

//        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0f, 200f);
//        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY", 0f, 200f);
//        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
//        ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2, p3).setDuration(1000).start();

//        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);
//        ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f);
//        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
//        AnimatorSet set = new AnimatorSet();
//        set.playSequentially(animator1,animator2,animator3);
//        set.play(animator1).with(animator2);
//        set.play(animator3).after(animator1);
//        set.setDuration(1000);
//        set.start();
//        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f, 1f);
//        animator.setDuration(1000);
//        animator.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                super.onAnimationEnd(animation);
//                //....do something
//            }
//        });
//        animator.addListener(new Animator.AnimatorListener() {
//            @Override   //动画开始
//            public void onAnimationStart(Animator animator) {
//
//            }
//
//            @Override  //动画结束
//            public void onAnimationEnd(Animator animator) {
//              ....do something
//            }
//
//            @Override   //动画取消
//            public void onAnimationCancel(Animator animator) {
//
//            }
//
//            @Override   //重复模式
//            public void onAnimationRepeat(Animator animator) {
//
//            }
//        });
//        animator.start();
//
//    }


}

package com.ccstest.activity;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.ccstest.R;

public class AnimTimeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim_time);
    }

    public void animTiem(View view){
//        final Button button = (Button) view;
//        ValueAnimator animator = ValueAnimator.ofInt(0, 100);
//        animator.setDuration(5000);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                Integer value = (Integer) valueAnimator.getAnimatedValue();
//                button.setText(value+"");
//            }
//        });
//        animator.start();
        ValueAnimator animator = ValueAnimator.ofObject(new TypeEvaluator() {
            @Override
            public PointF evaluate(float v, PointF pointF, PointF t1) {
                return null;
            }
        });
    }


}

你可能感兴趣的:(Android 属性动画整理)