[关闭]
@coder-pig 2015-11-17T12:17:19.000000Z 字数 11206 阅读 1519

Android基础入门教程——8.4.3 Android动画合集之属性动画-初见

Android基础入门教程


本节引言:

本节给带来的是Android动画中的第三种动画——属性动画(Property Animation),
记得在上一节Android基础入门教程——8.4.2 Android动画合集之补间动画为Fragment
设置过渡动画的时候,说过,App包和V4包下的Fragment调用setCustomAnimations()对应的
动画类型是不一样的,v4包下的是Animation,而app包下的是Animator
Animation一般动画就是我们前面学的帧动画和补间动画Animator则是本节要讲的属性动画
关于属性动画,大牛郭大叔已经写了三篇非常好的总结文,写得非常赞,就没必要重复造轮子了,
不过这里还是过一遍,大部分内容参考的下面三篇文章:
Android属性动画完全解析(上),初识属性动画的基本用法
Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
写的非常好,或者说你可以直接跳过本文去看上面的三篇文章~
当然,你愿意看我叨叨逼的话,也很欢迎,好了,开始本节内容吧~


1.属性动画概念叨叨逼


不BB,直接上图,就是这么暴力~


2.ValueAnimator简单使用

使用流程

  • 1.调用ValueAnimator的ofInt(),ofFloat()或ofObject()静态方法创建ValueAnimator实例
  • 2.调用实例的setXxx方法设置动画持续时间,插值方式,重复次数等
  • 3.调用实例的addUpdateListener添加AnimatorUpdateListener监听器,在该监听器中
    可以获得ValueAnimator计算出来的值,你可以值应用到指定对象上~
  • 4.调用实例的start()方法开启动画!
    另外我们可以看到ofInt和ofFloat都有个这样的参数:float/int... values代表可以多个值!

使用示例

代码实现

布局文件:activity_main.xml,非常简单,四个按钮,一个ImageView

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/ly_root"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/btn_one"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="动画1" />
  11. <Button
  12. android:id="@+id/btn_two"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="动画2" />
  16. <Button
  17. android:id="@+id/btn_three"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="动画3" />
  21. <Button
  22. android:id="@+id/btn_four"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="动画4" />
  26. <ImageView
  27. android:id="@+id/img_babi"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center"
  31. android:background="@mipmap/img_babi" />
  32. </LinearLayout>

接着到MainActivity.java
首先需要一个修改View位置的方法,这里调用moveView()设置左边和上边的起始坐标以及宽高!
接着定义了四个动画,分别是:直线移动,缩放,旋转加透明,以及圆形旋转!
然后通过按钮触发对应的动画~

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private Button btn_one;
  3. private Button btn_two;
  4. private Button btn_three;
  5. private Button btn_four;
  6. private LinearLayout ly_root;
  7. private ImageView img_babi;
  8. private int width;
  9. private int height;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. bindViews();
  15. }
  16. private void bindViews() {
  17. ly_root = (LinearLayout) findViewById(R.id.ly_root);
  18. btn_one = (Button) findViewById(R.id.btn_one);
  19. btn_two = (Button) findViewById(R.id.btn_two);
  20. btn_three = (Button) findViewById(R.id.btn_three);
  21. btn_four = (Button) findViewById(R.id.btn_four);
  22. img_babi = (ImageView) findViewById(R.id.img_babi);
  23. btn_one.setOnClickListener(this);
  24. btn_two.setOnClickListener(this);
  25. btn_three.setOnClickListener(this);
  26. btn_four.setOnClickListener(this);
  27. img_babi.setOnClickListener(this);
  28. }
  29. @Override
  30. public void onClick(View v) {
  31. switch (v.getId()) {
  32. case R.id.btn_one:
  33. lineAnimator();
  34. break;
  35. case R.id.btn_two:
  36. scaleAnimator();
  37. break;
  38. case R.id.btn_three:
  39. raAnimator();
  40. break;
  41. case R.id.btn_four:
  42. circleAnimator();
  43. break;
  44. case R.id.img_babi:
  45. Toast.makeText(MainActivity.this, "不愧是coder-pig~", Toast.LENGTH_SHORT).show();
  46. break;
  47. }
  48. }
  49. //定义一个修改ImageView位置的方法
  50. private void moveView(View view, int rawX, int rawY) {
  51. int left = rawX - img_babi.getWidth() / 2;
  52. int top = rawY - img_babi.getHeight();
  53. int width = left + view.getWidth();
  54. int height = top + view.getHeight();
  55. view.layout(left, top, width, height);
  56. }
  57. //定义属性动画的方法:
  58. //按轨迹方程来运动
  59. private void lineAnimator() {
  60. width = ly_root.getWidth();
  61. height = ly_root.getHeight();
  62. ValueAnimator xValue = ValueAnimator.ofInt(height,0,height / 4,height / 2,height / 4 * 3 ,height);
  63. xValue.setDuration(3000L);
  64. xValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  65. @Override
  66. public void onAnimationUpdate(ValueAnimator animation) {
  67. // 轨迹方程 x = width / 2
  68. int y = (Integer) animation.getAnimatedValue();
  69. int x = width / 2;
  70. moveView(img_babi, x, y);
  71. }
  72. });
  73. xValue.setInterpolator(new LinearInterpolator());
  74. xValue.start();
  75. }
  76. //缩放效果
  77. private void scaleAnimator(){
  78. //这里故意用两个是想让大家体会下组合动画怎么用而已~
  79. final float scale = 0.5f;
  80. AnimatorSet scaleSet = new AnimatorSet();
  81. ValueAnimator valueAnimatorSmall = ValueAnimator.ofFloat(1.0f, scale);
  82. valueAnimatorSmall.setDuration(500);
  83. ValueAnimator valueAnimatorLarge = ValueAnimator.ofFloat(scale, 1.0f);
  84. valueAnimatorLarge.setDuration(500);
  85. valueAnimatorSmall.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  86. @Override
  87. public void onAnimationUpdate(ValueAnimator animation) {
  88. float scale = (Float) animation.getAnimatedValue();
  89. img_babi.setScaleX(scale);
  90. img_babi.setScaleY(scale);
  91. }
  92. });
  93. valueAnimatorLarge.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  94. @Override
  95. public void onAnimationUpdate(ValueAnimator animation) {
  96. float scale = (Float) animation.getAnimatedValue();
  97. img_babi.setScaleX(scale);
  98. img_babi.setScaleY(scale);
  99. }
  100. });
  101. scaleSet.play(valueAnimatorLarge).after(valueAnimatorSmall);
  102. scaleSet.start();
  103. //其实可以一个就搞定的
  104. // ValueAnimator vValue = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);
  105. // vValue.setDuration(1000L);
  106. // vValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  107. // @Override
  108. // public void onAnimationUpdate(ValueAnimator animation) {
  109. // float scale = (Float) animation.getAnimatedValue();
  110. // img_babi.setScaleX(scale);
  111. // img_babi.setScaleY(scale);
  112. // }
  113. // });
  114. // vValue.setInterpolator(new LinearInterpolator());
  115. // vValue.start();
  116. }
  117. //旋转的同时透明度变化
  118. private void raAnimator(){
  119. ValueAnimator rValue = ValueAnimator.ofInt(0, 360);
  120. rValue.setDuration(1000L);
  121. rValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  122. @Override
  123. public void onAnimationUpdate(ValueAnimator animation) {
  124. int rotateValue = (Integer) animation.getAnimatedValue();
  125. img_babi.setRotation(rotateValue);
  126. float fractionValue = animation.getAnimatedFraction();
  127. img_babi.setAlpha(fractionValue);
  128. }
  129. });
  130. rValue.setInterpolator(new DecelerateInterpolator());
  131. rValue.start();
  132. }
  133. //圆形旋转
  134. protected void circleAnimator() {
  135. width = ly_root.getWidth();
  136. height = ly_root.getHeight();
  137. final int R = width / 4;
  138. ValueAnimator tValue = ValueAnimator.ofFloat(0,
  139. (float) (2.0f * Math.PI));
  140. tValue.setDuration(1000);
  141. tValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  142. @Override
  143. public void onAnimationUpdate(ValueAnimator animation) {
  144. // 圆的参数方程 x = R * sin(t) y = R * cos(t)
  145. float t = (Float) animation.getAnimatedValue();
  146. int x = (int) (R * Math.sin(t) + width / 2);
  147. int y = (int) (R * Math.cos(t) + height / 2);
  148. moveView(img_babi, x, y);
  149. }
  150. });
  151. tValue.setInterpolator(new DecelerateInterpolator());
  152. tValue.start();
  153. }
  154. }

好的,使用的流程非常简单,先创建ValueAnimator对象,调用ValueAnimator.ofInt/ofFloat
获得,然后设置动画持续时间,addUpdateListener添加AnimatorUpdateListener事件监听,
然后使用参数animationgetAnimatedValue()获得当前的值,然后我们可以拿着这个值
来修改View的一些属性,从而形成所谓的动画效果,接着设置setInterpolator动画渲染模式,
最后调用start()开始动画的播放~
卧槽,直线方程,圆的参数方程,我都开始方了,这不是高数的东西么,
挂科学渣连三角函数都忘了...
例子参考自github:MoveViewValueAnimator


3.ObjectAnimator简单使用

比起ValueAnimator,ObjectAnimator显得更为易用,通过该类我们可以直接
对任意对象的任意属性进行动画操作!没错,是任意对象,而不单单只是View对象,
不断地对对象中的某个属性值进行赋值,然后根据对象属性值的改变再来决定如何展现
出来!比如为TextView设置如下动画:
ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f);
这里就是不断改变alpha的值,从1f - 0f,然后对象根据属性值的变化来刷新界面显示,从而
展现出淡入淡出的效果,而在TextView类中并没有alpha这个属性,ObjectAnimator内部机制是:
寻找传输的属性名对应的get和set方法~,而非找这个属性值!
不信的话你可以到TextView的源码里找找是否有alpha这个属性!
好的,下面我们利用ObjectAnimator来实现四种补间动画的效果吧~

运行效果图

代码实现

布局直接用的上面那个布局,加了个按钮,把ImageView换成了TextView,这里就不贴代码了,
直接上MainActivity.java部分的代码,其实都是大同小异的~

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private Button btn_one;
  3. private Button btn_two;
  4. private Button btn_three;
  5. private Button btn_four;
  6. private Button btn_five;
  7. private LinearLayout ly_root;
  8. private TextView tv_pig;
  9. private int height;
  10. private ObjectAnimator animator1;
  11. private ObjectAnimator animator2;
  12. private ObjectAnimator animator3;
  13. private ObjectAnimator animator4;
  14. private AnimatorSet animSet;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. bindViews();
  20. initAnimator();
  21. }
  22. private void bindViews() {
  23. ly_root = (LinearLayout) findViewById(R.id.ly_root);
  24. btn_one = (Button) findViewById(R.id.btn_one);
  25. btn_two = (Button) findViewById(R.id.btn_two);
  26. btn_three = (Button) findViewById(R.id.btn_three);
  27. btn_four = (Button) findViewById(R.id.btn_four);
  28. btn_five = (Button) findViewById(R.id.btn_five);
  29. tv_pig = (TextView) findViewById(R.id.tv_pig);
  30. height = ly_root.getHeight();
  31. btn_one.setOnClickListener(this);
  32. btn_two.setOnClickListener(this);
  33. btn_three.setOnClickListener(this);
  34. btn_four.setOnClickListener(this);
  35. btn_five.setOnClickListener(this);
  36. tv_pig.setOnClickListener(this);
  37. }
  38. //初始化动画
  39. private void initAnimator() {
  40. animator1 = ObjectAnimator.ofFloat(tv_pig, "alpha", 1f, 0f, 1f, 0f, 1f);
  41. animator2 = ObjectAnimator.ofFloat(tv_pig, "rotation", 0f, 360f, 0f);
  42. animator3 = ObjectAnimator.ofFloat(tv_pig, "scaleX", 2f, 4f, 1f, 0.5f, 1f);
  43. animator4 = ObjectAnimator.ofFloat(tv_pig, "translationY", height / 8, -100, height / 2);
  44. }
  45. @Override
  46. public void onClick(View v) {
  47. switch (v.getId()) {
  48. case R.id.btn_one:
  49. animator1.setDuration(3000l);
  50. animator1.start();
  51. break;
  52. case R.id.btn_two:
  53. animator2.setDuration(3000l);
  54. animator2.start();
  55. break;
  56. case R.id.btn_three:
  57. animator3.setDuration(3000l);
  58. animator3.start();
  59. break;
  60. case R.id.btn_four:
  61. animator4.setDuration(3000l);
  62. animator4.start();
  63. break;
  64. case R.id.btn_five:
  65. //将前面的动画集合到一起~
  66. animSet = new AnimatorSet();
  67. animSet.play(animator4).with(animator3).with(animator2).after(animator1);
  68. animSet.setDuration(5000l);
  69. animSet.start();
  70. break;
  71. case R.id.tv_pig:
  72. Toast.makeText(MainActivity.this, "不愧是coder-pig~", Toast.LENGTH_SHORT).show();
  73. break;
  74. }
  75. }
  76. }

用法也非常简单,上面涉及到的组合动画我们下面讲~


4.组合动画与AnimatorListener

从上面两个例子中我们都体验了一把组合动画,用到了AnimatorSet这个类!
我们调用的play()方法,然后传入第一个开始执行的动画,此时他会返回一个Builder类给我们:

接下来我们可以调用Builder给我们提供的四个方法,来组合其他的动画:

  • after(Animator anim) 将现有动画插入到传入的动画之后执行
  • after(long delay) 将现有动画延迟指定毫秒后执行
  • before(Animator anim) 将现有动画插入到传入的动画之前执行
  • with(Animator anim) 将现有动画和传入的动画同时执行

嗯,很简单,接下来要说下动画事件的监听,上面我们ValueAnimator的监听器是
AnimatorUpdateListener,当值状态发生改变时候会回调onAnimationUpdate方法!
除了这种事件外还有:动画进行状态的监听~ AnimatorListener,我们可以调用addListener方法
添加监听器,然后重写下面四个回调方法:

  • onAnimationStart():动画开始
  • onAnimationRepeat():动画重复执行
  • onAnimationEnd():动画结束
  • onAnimationCancel():动画取消

没错,加入你真的用AnimatorListener的话,四个方法你都要重写,当然和前面的手势那一节一样,
Android已经给我们提供好一个适配器类:AnimatorListenerAdapter,该类中已经把每个接口
方法都实现好了,所以我们这里只写一个回调方法也可以额!


5.使用XML来编写动画

使用XML来编写动画,画的时间可能比Java代码长一点,但是重用起来就轻松很多!
对应的XML标签分别为:<animator><objectAnimator><set>
相关的属性解释如下:

  • android:ordering:指定动画的播放顺序:sequentially(顺序执行),together(同时执行)
  • android:duration:动画的持续时间
  • android:propertyName="x":这里的x,还记得上面的"alpha"吗?加载动画的那个对象里需要
    定义getx和setx的方法,objectAnimator就是通过这里来修改对象里的值的!
  • android:valueFrom="1" :动画起始的初始值
  • android:valueTo="0" :动画结束的最终值
  • android:valueType="floatType":变化值的数据类型

使用例子如下

从0到100平滑过渡的动画

  1. <animator xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:valueFrom="0"
  3. android:valueTo="100"
  4. android:valueType="intType"/>

将一个视图的alpha属性从1变成0

  1. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:valueFrom="1"
  3. android:valueTo="0"
  4. android:valueType="floatType"
  5. android:propertyName="alpha"/>

set动画使用演示

  1. <set android:ordering="sequentially" >
  2. <set>
  3. <objectAnimator
  4. android:duration="500"
  5. android:propertyName="x"
  6. android:valueTo="400"
  7. android:valueType="intType" />
  8. <objectAnimator
  9. android:duration="500"
  10. android:propertyName="y"
  11. android:valueTo="300"
  12. android:valueType="intType" />
  13. </set>
  14. <objectAnimator
  15. android:duration="500"
  16. android:propertyName="alpha"
  17. android:valueTo="1f" />
  18. </set>

加载我们的动画文件

  1. AnimatorSet set = (AnimatorSet)AnimatorInflater.loadAnimator(mContext,
  2. R.animator.property_animator);
  3. animator.setTarget(view);
  4. animator.start();

6.本节示例代码下载:

AnimatorDemo1.zip
AnimatorDemo2.zip


本节小结:

好的,本节给大家捋了一捋安卓中属性动画的基本用法,不知道你get了没,内容还是比较简单
的,而且例子比较有趣,相信大家会喜欢,嗯,就说这么多,谢谢~
感谢郭神的文章~

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注