【轉】android 屬性動畫之 ObjectAnimator


原文網址:http://blog.csdn.net/feiduclear_up/article/details/39255083

前面一篇博客講解了 android 簡單動畫之 animtion,這里來講解一下android 3.0之后添加的一些動畫   animator 中的 ObjectAnimator 。

屬性動畫概念:

所謂屬性動畫:改變一切能改變的對象的屬性值,不同於補間動畫:只能改變 alpha,scale,rotate,translate。聽着有點抽象,舉例子說明
 

補間動畫能實現的:

1.alpha 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. //第一個參數為 view對象,第二個參數為 動畫改變的類型,第三,第四個參數依次是開始透明度和結束透明度。  
  2.         ObjectAnimator alpha = ObjectAnimator.ofFloat(text, "alpha", 0f, 1f);  
  3.         alpha.setDuration(2000);//設置動畫時間  
  4.         alpha.setInterpolator(new DecelerateInterpolator());//設置動畫插入器,減速  
  5.         alpha.setRepeatCount(-1);//設置動畫重復次數,這里-1代表無限  
  6.         alpha.setRepeatMode(Animation.REVERSE);//設置動畫循環模式。  
  7.         alpha.start();//啟動動畫。  


2.scale

 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. AnimatorSet animatorSet = new AnimatorSet();//組合動畫  
  2.         ObjectAnimator scaleX = ObjectAnimator.ofFloat(text, "scaleX", 1f, 0f);  
  3.         ObjectAnimator scaleY = ObjectAnimator.ofFloat(text, "scaleY", 1f, 0f);  
  4.   
  5.         animatorSet.setDuration(2000);  
  6.         animatorSet.setInterpolator(new DecelerateInterpolator());  
  7.         animatorSet.play(scaleX).with(scaleY);//兩個動畫同時開始  
  8.         animatorSet.start();  


3.translate

 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. ObjectAnimator translationUp = ObjectAnimator.ofFloat(button, "Y",  
  2.                 button.getY(), 0);  
  3.         translationUp.setInterpolator(new DecelerateInterpolator());  
  4.         translationUp.setDuration(1500);  
  5.         translationUp.start();  


4. rotate

 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. AnimatorSet set = new AnimatorSet() ;               
  2.      ObjectAnimator anim = ObjectAnimator .ofFloat(phone, "rotationX", 0f, 180f);   
  3.      anim.setDuration(2000);   
  4.      ObjectAnimator anim2 = ObjectAnimator .ofFloat(phone, "rotationX", 180f, 0f);   
  5.      anim2.setDuration(2000);   
  6.      ObjectAnimator anim3 = ObjectAnimator .ofFloat(phone, "rotationY", 0f, 180f);   
  7.      anim3.setDuration(2000);   
  8.      ObjectAnimator anim4 = ObjectAnimator .ofFloat(phone, "rotationY", 180f, 0f);   
  9.      anim4.setDuration(2000);   
  10.         
  11.      set.play(anim).before(anim2); //先執行anim動畫之后在執行anim2  
  12.      set.play(anim3).before(anim4) ;   
  13.      set.start();   

 

補間動畫不能實現的:

5.android 改變背景顏色的動畫實現如下

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. ObjectAnimator translationUp = ObjectAnimator.ofInt(button,  
  2.                 "backgroundColor", Color.RED, Color.BLUE, Color.GRAY,  
  3.                 Color.GREEN);  
  4.         translationUp.setInterpolator(new DecelerateInterpolator());  
  5.         translationUp.setDuration(1500);  
  6.         translationUp.setRepeatCount(-1);  
  7.         translationUp.setRepeatMode(Animation.REVERSE);  
  8.         /* 
  9.          * ArgbEvaluator:這種評估者可以用來執行類型之間的插值整數值代表ARGB顏色。 
  10.          * FloatEvaluator:這種評估者可以用來執行浮點值之間的插值。 
  11.          * IntEvaluator:這種評估者可以用來執行類型int值之間的插值。 
  12.          * RectEvaluator:這種評估者可以用來執行類型之間的插值矩形值。 
  13.          *  
  14.          * 由於本例是改變View的backgroundColor屬性的背景顏色所以此處使用ArgbEvaluator 
  15.          */  
  16.   
  17.         translationUp.setEvaluator(new ArgbEvaluator());  
  18.         translationUp.start();  



更多關於android 屬性動畫的相關知識請參考詳細博客 Android屬性動畫Property Animation系列一之ValueAnimator


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM