当前位置:   article > 正文

安卓的属性动画基本用法(一)_安卓清除属性动画

安卓清除属性动画

    休假归来,十几天的婚假过得真快,时间不管对谁都是不够用的,我们能做的只能是珍惜时间。

    上篇文章讲解了补间动画,这次我们来看看属性动画,先给大家看看两张效果图,大家看看其中的不同:

    第一张(补间动画):

   

   第二张(属性动画):

  

  模拟器录制的第二张比较卡,但是大致的效果是能看出来的,大家也能一目了然的看出两张图的区别吧,那就是点击“飘逸的我”按钮的点击事件的不同,其实这就是补间动画和属性动画的本质的区别。补间动画虽能对控件做动画,但并没有改变控件内部的属性值。而Property Animator则是恰恰相反,Property Animator是通过改变控件内部的属性值来达到动画效果的。

 下面看下代码:

 

  1. public class MainActivity extends Activity implements OnClickListener {
  2. private Button btn_move;
  3. private Button btn_start;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. initView();
  9. setListener();
  10. }
  11. private void initView() {
  12. btn_start = (Button) findViewById(R.id.btn_start);
  13. btn_move = (Button) findViewById(R.id.btn_move);
  14. }
  15. private void setListener() {
  16. btn_start.setOnClickListener(this);
  17. btn_move.setOnClickListener(this);
  18. }
  19. @Override
  20. public void onClick(View v) {
  21. switch (v.getId()) {
  22. case R.id.btn_start:
  23. // viewAnimation(btn_move); //补间动画
  24. propertyAnimation(btn_move); //属性动画
  25. break;
  26. case R.id.btn_move:
  27. Toast.makeText(this, "点我干嘛", 0).show();
  28. break;
  29. default:
  30. break;
  31. }
  32. }
  33. private void propertyAnimation(final View view) {
  34. ValueAnimator animator = ValueAnimator.ofInt(0,200);
  35. animator.setDuration(1500);
  36. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  37. @Override
  38. public void onAnimationUpdate(ValueAnimator animation) {
  39. int curValue = (int)animation.getAnimatedValue();
  40. view.layout(dp2px(MainActivity.this, 100), dp2px(MainActivity.this, 100)+curValue, dp2px(MainActivity.this, 100)+view.getWidth(), curValue+view.getHeight()+dp2px(MainActivity.this, 100));
  41. }
  42. });
  43. //属性动画的相关监听
  44. animator.addListener(new AnimatorListener() {
  45. @Override
  46. public void onAnimationStart(Animator animation) {
  47. // TODO Auto-generated method stub
  48. }
  49. @Override
  50. public void onAnimationRepeat(Animator animation) {
  51. // TODO Auto-generated method stub
  52. }
  53. @Override
  54. public void onAnimationEnd(Animator animation) {
  55. // TODO Auto-generated method stub
  56. }
  57. @Override
  58. public void onAnimationCancel(Animator animation) {
  59. // TODO Auto-generated method stub
  60. }
  61. });
  62. animator.start();
  63. }
  64. private void viewAnimation(View view) {
  65. TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 200);
  66. translateAnimation.setFillAfter(true);
  67. translateAnimation.setDuration(1500);
  68. view.startAnimation(translateAnimation);
  69. }
  70. /**
  71. * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
  72. */
  73. public int dp2px(Context context, int dpValue) {
  74. final float scale = context.getResources().getDisplayMetrics().density;
  75. return (int) (dpValue * scale + 0.5f);
  76. }
  77. }
   以上是MainActivity的所有代码,这里要提的是上篇介绍的补间动画都是用xml的形式来实现的,而这边直接代码实现,所以像补间动画的实现大家自己看着用,习惯用代码实现的就用代码实现,习惯用xml实现的就用xml实现。其实代码实现也简单:

  1. private void viewAnimation(View view) {
  2. TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 200);
  3. translateAnimation.setFillAfter(true);
  4. translateAnimation.setDuration(1500);
  5. view.startAnimation(translateAnimation);
  6. }
   好,下面就来分析属性动画怎么用的吧!

  属性动画Property Animator包括ValueAnimator和ObjectAnimator;而我们这篇用到的是ValueAnimator。

  先看ValueAnimator的相关代码:

 

  1. ValueAnimator animator = ValueAnimator.ofInt(0,200);
  2. animator.setDuration(1500);
  3. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  4. @Override
  5. public void onAnimationUpdate(ValueAnimator animation) {
  6. int curValue = (int)animation.getAnimatedValue();
  7. view.layout(dp2px(MainActivity.this, 100), dp2px(MainActivity.this, 100)+curValue, dp2px(MainActivity.this, 100)+view.getWidth(), curValue+view.getHeight()+dp2px(MainActivity.this, 100));
  8. }
  9. });
  10. animator.start();
java的核心思想面向对象,我们要用属性动画肯定要拿到属性动画的相关实例,那么第一步就是创建属性动画的相关实例:

ValueAnimator animator = ValueAnimator.ofInt(0,200); 
大家可能比较迷惑ofInt(0,200)是啥意思?不用怕,不懂我们就查查源码,看看源码怎么说的:

  1. /**
  2. * Constructs and returns a ValueAnimator that animates between int values. A single
  3. * value implies that that value is the one being animated to. However, this is not typically
  4. * useful in a ValueAnimator object because there is no way for the object to determine the
  5. * starting value for the animation (unlike ObjectAnimator, which can derive that value
  6. * from the target object and property being animated). Therefore, there should typically
  7. * be two or more values.
  8. *
  9. * @param values A set of values that the animation will animate between over time.
  10. * @return A ValueAnimator object that is set up to animate between the given values.
  11. */
  12. public static ValueAnimator ofInt(int... values) {
  13. ValueAnimator anim = new ValueAnimator();
  14. anim.setIntValues(values);
  15. return anim;
  16. }
  17. /**
  18. * Constructs and returns a ValueAnimator that animates between color values. A single
  19. * value implies that that value is the one being animated to. However, this is not typically
  20. * useful in a ValueAnimator object because there is no way for the object to determine the
  21. * starting value for the animation (unlike ObjectAnimator, which can derive that value
  22. * from the target object and property being animated). Therefore, there should typically
  23. * be two or more values.
  24. *
  25. * @param values A set of values that the animation will animate between over time.
  26. * @return A ValueAnimator object that is set up to animate between the given values.
  27. */
  28. public static ValueAnimator ofArgb(int... values) {
  29. ValueAnimator anim = new ValueAnimator();
  30. anim.setIntValues(values);
  31. anim.setEvaluator(ArgbEvaluator.getInstance());
  32. return anim;
  33. }
  34. /**
  35. * Constructs and returns a ValueAnimator that animates between float values. A single
  36. * value implies that that value is the one being animated to. However, this is not typically
  37. * useful in a ValueAnimator object because there is no way for the object to determine the
  38. * starting value for the animation (unlike ObjectAnimator, which can derive that value
  39. * from the target object and property being animated). Therefore, there should typically
  40. * be two or more values.
  41. *
  42. * @param values A set of values that the animation will animate between over time.
  43. * @return A ValueAnimator object that is set up to animate between the given values.
  44. */
  45. public static ValueAnimator ofFloat(float... values) {
  46. ValueAnimator anim = new ValueAnimator();
  47. anim.setFloatValues(values);
  48. return anim;
  49. }
    上面的注释的英文比较的简单,大致的意思就是参数里面值至少要有两个,一个开始值一个结束值,动画会在这点规定的点之间进行变化。当然你也可以设置一系列的值,那么我也给大家展示一下设置一系列值的效果:

  这里我把代码改为: ValueAnimator animator = ValueAnimator.ofInt(0,200,-60,300); 
 

其实就是动画线性点的变化;大家有没有注意到我在给大家展示源码的时候还展示了两个方法ofArgb(int... values)和ofFloat(float... values),这两个方法是什么意思呢?先不要急,一步步往下看,我们第一步创建了ValueAnimator的实例。

第二部: animator.setDuration(1500);  这个就不解释了,你懂得!

第三部:   animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
            @Override  
            public void onAnimationUpdate(ValueAnimator animation) {  
                int curValue = (int)animation.getAnimatedValue();  
                view.layout(dp2px(MainActivity.this, 100), dp2px(MainActivity.this, 100)+curValue, dp2px(MainActivity.this, 100)+view.getWidth(), curValue+view.getHeight()+dp2px(MainActivity.this, 100));
            }  
            
        });

这是最关键的一步,动画时刻更新的监听器,其中在onAnimationUpdate的重写方法中有一个   int curValue = (int)animation.getAnimatedValue();可能大家不知道这个得到的int值是什么,无妨看下日志:

   我只是截取了一部分,其实就是我们设置的ofInt(0,200)值的变化,  int curValue = (int)animation.getAnimatedValue();  其实这边有一个强制的转换直接强转为int了,为什么?因为我们在创建实例的时候用的是ofInt(),如果你用的是ofFloat()那这边只能强制转换为float。那可能大家有想问什么时候用ofInt什么时候用ofFloat呢?其实很简单具体的就要看你的属性动画是用来干什么的,我这边是位置的改变用到的是:

view.layout(dp2px(MainActivity.this, 100), dp2px(MainActivity.this, 100)+curValue, dp2px(MainActivity.this, 100)+view.getWidth(), curValue+view.getHeight()+dp2px(MainActivity.this, 100));
view.layout(int l, int t, int r, int b),这个方法里面用到的是int值那么我就用ofInt,如果你的属性动画想来改变透明度那就用ofFloat,如果你想改变色值就要用ofArgb了,所以一切就看你想要什么。当然属性动画也有着自己基本的方法,比如设置插值器,设置重复模式,设置重复次数,这些基本的东西不管什么动画都是有的,我也不具体的说明了。

  1. animator.setInterpolator(new BounceInterpolator());
  2. animator.setRepeatCount(1);
  3. animator.setRepeatMode(ValueAnimator.RESTART);
   好了,属性动画的ValueAnimator的简单用法就介绍到这了,关于更深层次的东西大家可以自己慢慢去研究。

  最后送给大家一句话,穷则变,变则通,穷则变通,海阔天空!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/程序质量控制师/article/detail/60760
推荐阅读
相关标签
  

闽ICP备14008679号