含义:通过设置控件属性,达到动画效果。
iv为ImageView
代码实现
//平移 public void translate(View v) { //-400到0,再从0到400,再400到0 ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", -400, 0, 0, 400, 400, 0); oa.setDuration(2000); oa.start(); } //缩放 public void scale(View v) { //1到0.1,再从0.1到1 ObjectAnimator sc = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 0.1f, 0.1f, 1f); sc.setDuration(2000); sc.setRepeatCount(3); sc.start(); } //透明 public void alpha(View v) { //1到0,再从0到1 ObjectAnimator al = ObjectAnimator.ofFloat(iv, "alpha", 1, 0, 0, 1); al.setDuration(5000); al.setRepeatCount(3); al.start(); } //旋转 public void rotate(View v) { //0°到360°,再从360°到0 // ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotation", 0, 360, 360, 0); ObjectAnimator oa = ObjectAnimator.ofFloat(iv,"rotationY",0,360,360,0); oa.setDuration(2000); oa.setRepeatCount(3); oa.start(); } //动画集 public void fly(View v){ AnimatorSet as = new AnimatorSet(); ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", -400, 0, 0, 400, 400, 0); ObjectAnimator sc = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 0.1f, 0.1f, 1f); ObjectAnimator al = ObjectAnimator.ofFloat(iv, "alpha", 1, 0, 0, 1); ObjectAnimator oe = ObjectAnimator.ofFloat(iv,"rotationY",0,360,360,0); as.setDuration(2000); as.setTarget(iv);//设置执行者,imageView as.playSequentially(oa,sc,al,oe);//按顺序播放 //as.playTogether(oa,sc,al,oe);//一起播放 as.start();//启动 }
xml实现
//xml文件设置动画属性 public void playxml(View v){ ObjectAnimator oa= (ObjectAnimator) AnimatorInflater.loadAnimator(this,R.animator.oanimator);//加载xml定义属性 oa.setTarget(iv);//设置目标 oa.start();//启动 }
res/animator目录下
<?xml version="1.0" encoding="utf-8"?> <animator xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:propertyName="translationX" android:duration="2000" android:valueFrom="10" android:valueTo="100" ></objectAnimator> </animator>