Animator是sdk 3.0之后加入的全新动画API,相比传统的Animation API ,属性动画有诸多优点。
Animation局限性:
1.传统Animation API 给ImageView添加动画,并在ImageVIew上监听click事件,执行Toast.makText(this,1,"在ImageView上点击!");
传统方式执行完动画后,我们发现,在ImageVIew上点击鼠标并不出现Toast提示,而是在ImageVIew的原来位置点击才显示Toast提示。这个就说面了Animation动画的局限性,Animation动画只适用显示性的动画,而不适合交互性的动画。
2.Animation的底层原理是不断调用重回方法,这种方式会占用大量CPU资源,会导致应用卡顿,而属性动画Animator只是修改元素的属性,因此会有更高的性能。
下面给出Animator的几种使用方式:
ImageView imageView = (ImageView) findViewById(R.id.iv_ic);
/*
* 传统动画方式 TranslateAnimation animation=new
* TranslateAnimation(0,200,0,0); animation.setDuration(1000);
* animation.setFillAfter(true);//设置图片动画结束后不恢复原位
* imageView.startAnimation(animation);
*/
// 第一种方式:
// 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();
// 第二种方式
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.play(animator1).with(animator2);
set.play(animator3).after(animator1);
set.setDuration(1000);
// set.playTogether(animator1,animator2,animator3); 一起执行动画
// set.playSequentially(animator1, animator2, animator3); 按照给定的顺序执行动画
set.start();
// 第三种方式
// 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();