這不是一篇介紹屬性動畫使用的文章,如何使用網上一大把,不愿意做別人做過的事情。
我在看了N多介紹屬性動畫的文章后,發現所有千篇一律,諸如以下代碼(請關注第二個參數屬性名)
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f); animator.setDuration(5000); animator.start(); ...ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f); animator.setDuration(5000); animator.start(); ...ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", 0f, 360f); animator.setDuration(5000); animator.start(); ...ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationY", 0f, 360f); animator.setDuration(5000); 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, 0f, 45f);//ObjectAnimator animation = ObjectAnimator.ofFloat(fabIconStar,View.ROTATION, 0f, 45f,0f);//轉回去ObjectAnimator animation = ObjectAnimator.ofFloat(tager, View.ROTATION, 45f, 0);//移動xyPath path = new Path(); path.lineTo(10, 10); path.lineTo(50, 20); //path.lineTo(10,10); //path.lineTo(0,0); 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(50, 20); path.lineTo(10, 10); path.lineTo(0, 0); 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!歡淫吐槽,指正,評論!
新聞熱點
疑難解答