Android ObjectAnimator ValueAnimation学习

AnimatorSet set = new AnimatorSet() ;             
        ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "translationX", -500f, 0f); 
        anim.setDuration(2000); 
        ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "translationX", 0f, -500f); 
        anim3.setDuration(2000); 
        ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "translationY", 0f, -500f); 
        anim2.setDuration(2000); 
        ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "translationY", -500f, 0f); 
        anim4.setDuration(2000); 
         
         
        AnimatorSet set3 = new AnimatorSet(); 
        set3.play(anim4).before(anim3) ; 
        AnimatorSet set2 = new AnimatorSet(); 
        set2.play(anim2).before(set3) ; 
        set.play(anim).before(set2); 
        set.start(); 

AnimatorSet set = new AnimatorSet() ;           
  ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "translationX", -500f, 0f);
  anim.setDuration(2000);
  ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "translationX", 0f, -500f);
  anim3.setDuration(2000);
  ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "translationY", 0f, -500f);
  anim2.setDuration(2000);
  ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "translationY", -500f, 0f);
  anim4.setDuration(2000);
  
  
  AnimatorSet set3 = new AnimatorSet();
  set3.play(anim4).before(anim3) ;
  AnimatorSet set2 = new AnimatorSet();
  set2.play(anim2).before(set3) ;
  set.play(anim).before(set2);
  set.start();

讲解:anim 是从-500f 位置移动到当前位置(X轴);
    anim3是从当前位置移动到-500f的位置(X轴);
    anim2是从当前位置移动-500f的位置(Y轴);
    anim 4是从-500f 位置移动到当前位置(Y轴);

   [java]
AnimatorSet set3 = new AnimatorSet(); 
        set3.play(anim4).before(anim3) ; 
        AnimatorSet set2 = new AnimatorSet(); 
        set2.play(anim2).before(set3) ; 
        set.play(anim).before(set2); 
        set.start(); 

AnimatorSet set3 = new AnimatorSet();
  set3.play(anim4).before(anim3) ;
  AnimatorSet set2 = new AnimatorSet();
  set2.play(anim2).before(set3) ;
  set.play(anim).before(set2);
  set.start();         这样做的目的是为了,让目标view移动一个来回,从哪里出发, 最后回到出发的位置。 


2. 透明度(alpha)

主要代码
[java]
AnimatorSet set = new AnimatorSet() ; 
        ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "alpha", 1f, 0f); 
        anim.setDuration(2000); 
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "alpha", 0f, 1f); 
        anim2.setDuration(2000); 
        set.play(anim).before(anim2) ; 
        set.start(); 

AnimatorSet set = new AnimatorSet() ;
  ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "alpha", 1f, 0f);
  anim.setDuration(2000);
  ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "alpha", 0f, 1f);
  anim2.setDuration(2000);
  set.play(anim).before(anim2) ;
  set.start();
讲解: anim 从可见到不可见;

     anim 从不可见到可见;
 3. 旋转(rotate)
主要代码

[java]
nimatorSet set = new AnimatorSet() ;             
        ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "rotationX", 0f, 180f); 
        anim.setDuration(2000); 
        ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "rotationX", 180f, 0f); 
        anim2.setDuration(2000); 
        ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "rotationY", 0f, 180f); 
        anim3.setDuration(2000); 
        ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "rotationY", 180f, 0f); 
        anim4.setDuration(2000); 
         
        set.play(anim).before(anim2); 
        set.play(anim3).before(anim4) ; 
        set.start(); 

AnimatorSet set = new AnimatorSet() ;           
  ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "rotationX", 0f, 180f);
  anim.setDuration(2000);
  ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "rotationX", 180f, 0f);
  anim2.setDuration(2000);
  ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "rotationY", 0f, 180f);
  anim3.setDuration(2000);
  ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "rotationY", 180f, 0f);
  anim4.setDuration(2000);
  
  set.play(anim).before(anim2);
  set.play(anim3).before(anim4) ;
  set.start();

讲解:anim 从0度转180度(X轴)

            anim2从180度转0度(X轴)
            anim3从0度转180度(Y轴)
            anim4从180度转0度(Y轴)


[java]
set.play(anim).before(anim2); 
        set.play(anim3).before(anim4) ; 

set.play(anim).before(anim2);
  set.play(anim3).before(anim4) ;这样写X和Y会同时旋转

 

 


 4. 缩放 (scale)

主要代码

[java]
AnimatorSet set = new AnimatorSet() ;             
        ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "scaleX", 1f); 
        anim.setDuration(1000); 
        ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "scaleX", 0.5f); 
        anim2.setDuration(1000); 
        ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "scaleY", 1f); 
        anim3.setDuration(1000); 
        ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "scaleY", 0.5f); 
        anim4.setDuration(1000); 
         
        ObjectAnimator anim5 = ObjectAnimator .ofFloat(phone, "scaleX", 0.5f); 
        anim5.setDuration(1000); 
        ObjectAnimator anim6 = ObjectAnimator .ofFloat(phone, "scaleX",  1f); 
        anim6.setDuration(1000); 
        ObjectAnimator anim7 = ObjectAnimator .ofFloat(phone, "scaleY",0.5f); 
        anim7.setDuration(1000); 
        ObjectAnimator anim8 = ObjectAnimator .ofFloat(phone, "scaleY",  1f); 
        anim8.setDuration(1000); 
         
        AnimatorSet set3 = new AnimatorSet() ;  
        set3.play(anim5).before(anim6); 
        AnimatorSet set2 = new AnimatorSet() ;   
        set2.play(anim2).before(set3) ; 
          
         
        AnimatorSet set4 = new AnimatorSet() ;   
        set4.play(anim7).before(anim8) ; 
        AnimatorSet set5 = new AnimatorSet() ;   
        set5.play(anim4).before(set4); 
         
        set.play(anim).before(set2); 
        set.play(anim3).before(set5) ; 
        set.start(); 

AnimatorSet set = new AnimatorSet() ;           
  ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "scaleX", 1f);
  anim.setDuration(1000);
  ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "scaleX", 0.5f);
  anim2.setDuration(1000);
  ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "scaleY", 1f);
  anim3.setDuration(1000);
  ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "scaleY", 0.5f);
  anim4.setDuration(1000);
  
  ObjectAnimator anim5 = ObjectAnimator .ofFloat(phone, "scaleX", 0.5f);
  anim5.setDuration(1000);
  ObjectAnimator anim6 = ObjectAnimator .ofFloat(phone, "scaleX",  1f);
  anim6.setDuration(1000);
  ObjectAnimator anim7 = ObjectAnimator .ofFloat(phone, "scaleY",0.5f);
  anim7.setDuration(1000);
  ObjectAnimator anim8 = ObjectAnimator .ofFloat(phone, "scaleY",  1f);
  anim8.setDuration(1000);
  
  AnimatorSet set3 = new AnimatorSet() ;
  set3.play(anim5).before(anim6);
  AnimatorSet set2 = new AnimatorSet() ; 
  set2.play(anim2).before(set3) ;
  
  
  AnimatorSet set4 = new AnimatorSet() ; 
  set4.play(anim7).before(anim8) ;
  AnimatorSet set5 = new AnimatorSet() ; 
  set5.play(anim4).before(set4);
  
  set.play(anim).before(set2);
  set.play(anim3).before(set5) ;
  set.start();

 

 


讲解:anim 从原来大小开始(X轴)

            anim2 缩放到原来的1/2(X轴)
            anim3从原来大小开始(Y轴)

            anim4 缩放到原来的1/2(Y轴)

            anim5从原来的1/2开始放大(X轴)

            anim6放大到原来的大小(X轴)

            anim7从原来的1/2开始放大(Y轴)

            anim8放大到原来的大小(Y轴)

 

1、初步使用ValueAnimator

要使用ValueAnimaiton,总共有两步: 

第一步:创建ValueAnimator实例

[java]  view plain copy
  1. ValueAnimator animator = ValueAnimator.ofInt(0,400);  
  2. animator.setDuration(1000);  
  3. animator.start();  
在这里我们利用ValueAnimator.ofInt创建了一个值从0到400的动画,动画时长是1s,然后让动画开始。从这段代码中可以看出,ValueAnimator没有跟任何的控件相关联,那也正好说明ValueAnimator只是对值做动画运算,而不是针对控件的,我们需要监听ValueAnimator的动画过程来自己对控件做操作。 

第二步:添加监听

上面的三行代码,我们已经实现了动画,下面我们就添加监听:
[java]  view plain copy
  1. ValueAnimator animator = ValueAnimator.ofInt(0,400);  
  2. animator.setDuration(1000);  
  3.   
  4. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
  5.     @Override  
  6.     public void onAnimationUpdate(ValueAnimator animation) {  
  7.         int curValue = (int)animation.getAnimatedValue();  
  8.         Log.d("qijian","curValue:"+curValue);  
  9.     }  
  10. });  
  11. animator.start();  
在上面的代码中,我们通过addUpdateListener添加了一个监听,在监听传回的结果中,是表示当前状态的ValueAnimator实例,我们通过animation.getAnimatedValue()得到当前值。然后通过Log打印出来,结果如下: 
Android ObjectAnimator ValueAnimation学习_第1张图片

这就是ValueAnimator的功能:ValueAnimator对指定值区间做动画运算,我们通过对运算过程做监听来自己操作控件。 
总而言之就是两点:

  • ValueAnimator只负责对指定的数字区间进行动画运算
  • 我们需要对运算过程进行监听,然后自己对控件做动画操作






你可能感兴趣的:(Android ObjectAnimator ValueAnimation学习)