Android属性动画-Property Animation(四) 组合动画

(一)、使用AnimatorSet实现动画集

之前我们研究的都是一个动画一个动画的单独播放,而在很多时候,我么需要将几个动画,以某种逻辑顺序来执行。我们可以用AnimatorSet来组合各种各样的动画。

首先我们先来看这样一个动画:

这个动画集由{下落动画,改变背景颜色动画,改变字体颜色动画}组成的。他们是同时执行的


布局文件如下:

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
           
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<Button
android:id= "@+id/my_brick"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "砖头"
android:textColor= "#ff000000"
android:background= "#ccee4400"
android:layout_centerHorizontal= "true"
/>
</RelativeLayout>
 来自CODE的代码片
main_activity.xml
MainActiity如下:

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
           
public class MainActivity extends Activity {
private Button mBrick ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
getWindow (). requestFeature ( Window . FEATURE_NO_TITLE );
setContentView ( R . layout . activity_main );
mBrick =( Button ) findViewById ( R . id . my_brick );
mBrick . setOnClickListener ( new OnClickListener () {
@Override
public void onClick ( View v ) {
//砖块下落的动画
ObjectAnimator drop = ObjectAnimator . ofFloat ( mBrick , "Y" , 0.0f , 500.0f );
drop . setDuration ( 3000 );
drop . setInterpolator ( new BounceInterpolator ());
//改变砖块颜色的动画,"backgroundColor"这个属性是在View中的
//Button继承自TextView继承自View,View中有对这个属性的setter/getter方法
ObjectAnimator backGroundColor = ObjectAnimator . ofObject ( mBrick , "backgroundColor" , new ArgbEvaluator (), 0xccee4400 , 0xcc000000 );
backGroundColor . setDuration ( 3000 );
//改变字体颜色的动画,"textColor"这个属性是TextView中的
//Button继承自TextView,TextView中有对这个属性的setter/getter方法
ObjectAnimator textColor = ObjectAnimator . ofObject ( mBrick , "textColor" , new ArgbEvaluator (), 0xff000000 , 0xffffffff );
textColor . setDuration ( 3000 );
//新建一个AnimatorSet实例
AnimatorSet set = new AnimatorSet ();
//将这3个动画一起执行
set . playTogether ( drop , backGroundColor , textColor );
//启动这个动画集
set . start ();
}
});
}
}
 来自CODE的代码片
MainActivity.java
我们还可以这样写:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
           
//将这3个动画一起执行
set . play ( drop ). with ( backGroundColor ). with ( textColor );
 来自CODE的代码片
MainActivity.java
如果是同时执行的动画,我们这样写在一行是没有问题的,但是谷歌并不推荐我们这样写,因为一个动画集中可能还会牵扯到谁先执行,谁后执行,before,after什么的。如果都写在一行,首先系统是分辨不出到底谁先谁后的,其次我们自己也会乱的。所以我们应该分行写

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
            
//将这3个动画一起执行
set . play ( drop ). with ( backGroundColor );
set . play ( drop ). with ( textColor );
 来自CODE的代码片
MainActivity.java
下面看一个先后执行的动画:

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
             
public class MainActivity extends Activity {
private Button mBrick ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
getWindow (). requestFeature ( Window . FEATURE_NO_TITLE );
setContentView ( R . layout . activity_main );
mBrick =( Button ) findViewById ( R . id . my_brick );
mBrick . setOnClickListener ( new OnClickListener () {
@Override
public void onClick ( View v ) {
//砖块下落的动画
ObjectAnimator drop = ObjectAnimator . ofFloat ( mBrick , "Y" , 0.0f , 500.0f );
drop . setDuration ( 3000 );
drop . setInterpolator ( new BounceInterpolator ());
//改变砖块颜色的动画
ObjectAnimator backGroundColor = ObjectAnimator . ofObject ( mBrick , "backgroundColor" , new ArgbEvaluator (), 0xccee4400 , 0xcc000000 );
backGroundColor . setDuration ( 3000 );
//改变字体颜色的动画
ObjectAnimator textColor = ObjectAnimator . ofObject ( mBrick , "textColor" , new ArgbEvaluator (), 0xff000000 , 0xffffffff );
textColor . setDuration ( 3000 );
//新建一个AnimatorSet实例
AnimatorSet set = new AnimatorSet ();
//将这3个动画指定一个先后顺序,1.砖头先掉下来。2.改变背景颜色。3.改变字体颜色
set . playSequentially ( drop , backGroundColor , textColor );
//启动这个动画集
set . start ();
}
});
}
}
 来自CODE的代码片
MainActivity.java
我们还可以这样写:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
             
//将这3个动画指定一个先后顺序,1.砖头先掉下来。2.改变背景颜色。3.改变字体颜色
set . play ( drop ). before ( backGroundColor );
set . play ( textColor ). after ( backGroundColor );
 来自CODE的代码片
MainActivity.java
我们还可以有多个AnimaorSet嵌套使用

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
<a target=_blank id="L39" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L39" rel="#L39" style="color: rgb(102, 102, 102); text-decoration: none;"> 39</a>
<a target=_blank id="L40" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L40" rel="#L40" style="color: rgb(102, 102, 102); text-decoration: none;"> 40</a>
<a target=_blank id="L41" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L41" rel="#L41" style="color: rgb(102, 102, 102); text-decoration: none;"> 41</a>
<a target=_blank id="L42" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L42" rel="#L42" style="color: rgb(102, 102, 102); text-decoration: none;"> 42</a>
<a target=_blank id="L43" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L43" rel="#L43" style="color: rgb(102, 102, 102); text-decoration: none;"> 43</a>
<a target=_blank id="L44" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L44" rel="#L44" style="color: rgb(102, 102, 102); text-decoration: none;"> 44</a>
<a target=_blank id="L45" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L45" rel="#L45" style="color: rgb(102, 102, 102); text-decoration: none;"> 45</a>
<a target=_blank id="L46" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L46" rel="#L46" style="color: rgb(102, 102, 102); text-decoration: none;"> 46</a>
<a target=_blank id="L47" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L47" rel="#L47" style="color: rgb(102, 102, 102); text-decoration: none;"> 47</a>
<a target=_blank id="L48" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L48" rel="#L48" style="color: rgb(102, 102, 102); text-decoration: none;"> 48</a>
<a target=_blank id="L49" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L49" rel="#L49" style="color: rgb(102, 102, 102); text-decoration: none;"> 49</a>
<a target=_blank id="L50" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L50" rel="#L50" style="color: rgb(102, 102, 102); text-decoration: none;"> 50</a>
<a target=_blank id="L51" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L51" rel="#L51" style="color: rgb(102, 102, 102); text-decoration: none;"> 51</a>
<a target=_blank id="L52" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L52" rel="#L52" style="color: rgb(102, 102, 102); text-decoration: none;"> 52</a>
<a target=_blank id="L53" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L53" rel="#L53" style="color: rgb(102, 102, 102); text-decoration: none;"> 53</a>
<a target=_blank id="L54" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L54" rel="#L54" style="color: rgb(102, 102, 102); text-decoration: none;"> 54</a>
<a target=_blank id="L55" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L55" rel="#L55" style="color: rgb(102, 102, 102); text-decoration: none;"> 55</a>
<a target=_blank id="L56" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L56" rel="#L56" style="color: rgb(102, 102, 102); text-decoration: none;"> 56</a>
             
public class MainActivity extends Activity {
private Button mBrick ;
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
getWindow (). requestFeature ( Window . FEATURE_NO_TITLE );
setContentView ( R . layout . activity_main );
mBrick =( Button ) findViewById ( R . id . my_brick );
mBrick . setOnClickListener ( new OnClickListener () {
@Override
public void onClick ( View v ) {
//砖块下落的动画
ObjectAnimator drop = ObjectAnimator . ofFloat ( mBrick , "Y" , 0.0f , 500.0f );
drop . setDuration ( 3000 );
drop . setInterpolator ( new BounceInterpolator ());
//改变砖块颜色的动画
ObjectAnimator backGroundColor = ObjectAnimator . ofObject ( mBrick , "backgroundColor" , new ArgbEvaluator (), 0xccee4400 , 0xcc000000 );
backGroundColor . setDuration ( 3000 );
//改变字体颜色的动画
ObjectAnimator textColor = ObjectAnimator . ofObject ( mBrick , "textColor" , new ArgbEvaluator (), 0xff000000 , 0xffffffff );
textColor . setDuration ( 3000 );
//新建一个AnimatorSet实例
AnimatorSet set1 = new AnimatorSet ();
//将这3个动画指定一个先后顺序,1.砖头先掉下来。2.改变背景颜色。3.改变字体颜色
set1 . play ( drop ). before ( backGroundColor );
set1 . play ( textColor ). after ( backGroundColor );
//将砖块的宽度扩大一倍
ObjectAnimator biggerWidth = ObjectAnimator . ofFloat ( mBrick , "scaleX" , 1.0f , 2.0f );
biggerWidth . setDuration ( 1000 );
biggerWidth . setInterpolator ( new BounceInterpolator ());
//将砖块的高度扩大一倍
ObjectAnimator biggerHeight = ObjectAnimator . ofFloat ( mBrick , "scaleY" , 1.0f , 2.0f );
biggerWidth . setDuration ( 1000 );
biggerWidth . setInterpolator ( new BounceInterpolator ());
//将砖块的颜色从黑色变回橙色,没有设置时间,那么系统默认为300ms
ObjectAnimator backGroundColorReverse = ObjectAnimator . ofObject ( mBrick , "backgroundColor" , new ArgbEvaluator (), 0xcc000000 , 0xccee4400 );
//将字体颜色从白色变回黑色,没有设置时间,那么系统默认为300ms
ObjectAnimator textColorReverse = ObjectAnimator . ofObject ( mBrick , "textColor" , new ArgbEvaluator (), 0xffffffff , 0xff000000 );
//又新建一个AnimatorSet
AnimatorSet set2 = new AnimatorSet ();
//让砖块的宽度和高度同时扩大一倍
set2 . play ( biggerHeight ). with ( biggerWidth );
//在砖块宽度和高度扩大完之后,改变砖块背景颜色
set2 . play ( backGroundColorReverse ). after ( biggerHeight );
//在砖块改变完背景颜色后,改变字体颜色
set2 . play ( textColorReverse ). after ( backGroundColorReverse );
//让set2在set1之后执行
set2 . play ( set1 ). before ( biggerHeight );
//启动动画
set2 . start ();
}
});
}
}
 来自CODE的代码片
MainActivity.java
但是这样会有一个问题,当我们再次点击砖块的时候,直接是一个原来两倍大的砖块往下掉!相信大家应该知道为什么了,就是砖块的属性已经被我们改变了,第一次动画执行完毕后,砖块的大小已经被改变了,而我们没有把他的大小再还原回去。为了解决这个问题,我们可以添加一个动画监听器


(二)、使用Animation Listeners动画监听器

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
             
set2 . addListener ( new AnimatorListener () {
@Override
public void onAnimationStart ( Animator animation ) {
//我们在每次动画开始的时候,将砖块的大小还原为正常大小
mBrick . setScaleX ( 1.0f );
mBrick . setScaleY ( 1.0f );
}
@Override
public void onAnimationRepeat ( Animator animation ) {
// 当动画重复执行的时候执行
}
@Override
public void onAnimationEnd ( Animator animation ) {
// 当动画结束的时候执行,不管动画以什么方式结束,都会调用
}
@Override
public void onAnimationCancel ( Animator animation ) {
// 当动画取消的时候执行,也会调用onAnimationEnd()
}
});
 来自CODE的代码片
MainActivity.java
这样就没有问题了!

我们发现,上面的监听器中我们只用到了onAnimationStart方法,其余的方法全部都重写出来了,然而我们却不需要使用他们,谷歌充分考虑了我们的需要,提供了AnimatorListenerAdapter,我们只需要重写出我们想要的方法就可以了。代码如下:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;"> 7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;"> 8</a>
             
set2 . addListener ( new AnimatorListenerAdapter () {
@Override
public void onAnimationStart ( Animator animation ) {
super . onAnimationStart ( animation );
mBrick . setScaleX ( 1.0f );
mBrick . setScaleY ( 1.0f );
}
});
 来自CODE的代码片
MainActivity.java

(三)、使用XML实现属性动画

属性动画也可以使用xml来实现,使用xml实现的好处是我们可以在任意一个activity使用我们的属性动画,而使用java代码方式只能让我们在当前activity上面使用。

以前我们使用View Animation的时候,我们要在我们工程目录下的res目录下新建一个anim文件夹,里面写我们的动画xml文件。在Android 3.1以后,为了和View Animation区分开,我们要在res目录下新建一个animator的文件夹,里面写我们的属性动画的xml文件。

属性动画的java实现类与xml标签的对应如下:

  • ValueAnimator -<animator>
  • ObjectAnimator -<objectAnimator>
  • AnimatorSet-<set>

下面我们看一个例子:

res/animator/property_animator.xml

<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
              
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android= "http://schemas.android.com/apk/res/android"
android:ordering= "sequentially" >
<objectAnimator
android:duration= "3000"
android:interpolator= "@android:anim/bounce_interpolator"
android:propertyName= "Y"
android:valueFrom= "0.0"
android:valueTo= "500.0" >
</objectAnimator>
<set android:ordering= "together" >
<objectAnimator
android:duration= "1000"
android:interpolator= "@android:anim/bounce_interpolator"
android:propertyName= "scaleX"
android:valueFrom= "1.0"
android:valueTo= "2.0" >
</objectAnimator>
<objectAnimator
android:duration= "1000"
android:interpolator= "@android:anim/bounce_interpolator"
android:propertyName= "scaleY"
android:valueFrom= "1.0"
android:valueTo= "2.0" >
</objectAnimator>
</set>
</set>
 来自CODE的代码片
poperty_animator.xml
MainActivity中要用inflater将xml文件加载进来
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
               
AnimatorSet set = ( AnimatorSet ) AnimatorInflater . loadAnimator ( MainActivity . this , R . animator . property_animator );
//这个方法一定要调用,不然谁来执行这个动画?
set . setTarget ( mBrick );
set . start ();
 来自CODE的代码片
MainActivity.java


补充如果要实现一个同时执行的动画集,我们还有简便方法。
按照前面的学习,我们是这样写的:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<a target=_blank id="L5" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a>
                 
ObjectAnimator animX = ObjectAnimator . ofFloat ( mBrick , "x" , 50 f );
ObjectAnimator animY = ObjectAnimator . ofFloat ( mBrick , "y" , 50 f );
AnimatorSet animSetXY = new AnimatorSet ();
animSetXY . playTogether ( animX , animY );
animSetXY . start ();
 来自CODE的代码片
MainActivity.java
然而我们还可以这样写:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
                  
PropertyValuesHolder pvhX = PropertyValuesHolder . ofFloat ( "x" , 50 f );
PropertyValuesHolder pvhY = PropertyValuesHolder . ofFloat ( "y" , 50 f );
ObjectAnimator . ofPropertyValuesHolder ( mBrick , pvhX , pvhY ). start ();
 来自CODE的代码片
MainActivity.java
最简便的方法是这样写:
<a target=_blank id="L1" href="http://blog.youkuaiyun.com/nugongahou110/article/details/46738069#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
                  
mBrick . animate (). x ( 50 f ). y ( 50 f );
 来自CODE的代码片
MainActivity.java
一句话就搞定了,而且ViewPropertyAnimator不仅简单,而且性能也很高,他只会调用一次invalidate()方法,而不是每一个单独的动画都要独立调用一次invalidate()。但是简便肯定就有他的局限性,这样只能实现淡入淡出、平移、旋转等动画效果,如果想要改变背景颜色就不成了。


关于属性动画我们已经全部分析完了。实现属性动画有好多种方法,简单的也有复杂的也有。简单会有局限,复杂会比较灵活,我们应该根据实际的需求,合理选择最佳的实现方式。




版权声明:欢迎转载,转载请注明出处http://blog.youkuaiyun.com/nugongahou110

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值