天天看點

屬性動畫 進階(ObjectAnimator)首先正文謝謝

首先

這不是一篇介紹屬性動畫使用的文章,如何使用網上一大把,不願意做别人做過的事情。

正文

我在看了N多介紹屬性動畫的文章後,發現所有千篇一律,諸如以下代碼(請關注第二個參數屬性名)

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
animator.setDuration();  
animator.start();  
...
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);  
animator.setDuration();  
animator.start();  
...
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", 0f, 360f);  
animator.setDuration();  
animator.start();  
...
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationY", 0f, 360f);  
animator.setDuration();  
animator.start();  
           

我很疑惑的是,難道我大google設計API,設計成這樣?需要開發者記住每一個屬性名嗎,需要實作一個動畫的時候,還需要先去想,這個屬性的全拼是怎麼樣的,實在太扯淡。

觀察ObjectAnimator方法之後發現以下方法

ofInt(T target, Property<T, Integer> property, int... values)

ofInt(T target, Property<T, Integer> xProperty,Property<T, Integer> yProperty, Path path)

ofFloat(T target, Property<T, Float> property,
            float... values)

ofFloat(T target, Property<T, Float> xProperty,Property<T, Float> yProperty, Path path)

...
           

其中第二個帶xy屬性的方法,Api21以上才有,使用起來veryEasy

//旋轉
ObjectAnimator animation = ObjectAnimator.ofFloat(fabIconStar, View.ROTATION, f, f);

//ObjectAnimator animation = ObjectAnimator.ofFloat(fabIconStar,View.ROTATION, f, f,f);

//轉回去
ObjectAnimator animation = ObjectAnimator.ofFloat(tager, View.ROTATION, f, );

//移動xy
Path path = new Path();
     path.lineTo(, );
     path.lineTo(, );
     //path.lineTo(,);
     //path.lineTo(,);
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
         ObjectAnimator animation1 = ObjectAnimator.ofFloat(tager, View.TRANSLATION_X,View.TRANSLATION_Y, path);
         animation1.start();
     }

//移動回去
 Path path = new Path();
      path.moveTo(, );
      path.lineTo(, );
      path.lineTo(, );
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
          ObjectAnimator animation1 = ObjectAnimator.ofFloat(tager, View.TRANSLATION_X,View.TRANSLATION_Y, path);
          animation1.start();
       }
           

可使用屬性View内所有 Property 屬性,

Property<View, Float> 
 ...
           

有興趣的可以一個個去嘗試一下,在這裡不作詳述了。

謝謝

是不是soEasy!歡淫吐槽,指正,評論!

繼續閱讀