[关闭]
@linux1s1s 2017-01-22T16:33:17.000000Z 字数 3334 阅读 2986

ObjectAnimator Demo

AndroidAnimation 2015-06


这里提供一个 ObjectAnimator 以供参考:这个Demo基本实现了从左向右(从左边出来,然后停留在中间放大,然后从右边缩小消失的效果)

代码比较简单,就一个类FiveActivity.java即可

  1. package studio.neulion.com.tasktest;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.AnimatorSet;
  5. import android.animation.ObjectAnimator;
  6. import android.animation.PropertyValuesHolder;
  7. import android.annotation.TargetApi;
  8. import android.app.Activity;
  9. import android.os.Build.VERSION_CODES;
  10. import android.os.Bundle;
  11. import android.util.DisplayMetrics;
  12. import android.view.View;
  13. import android.view.animation.BounceInterpolator;
  14. public class FiveActivity extends Activity
  15. {
  16. private View mTestView;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. initView();
  23. translateX(); //仅仅一个动画
  24. translateXScaleBoth(); //实现叠加动画
  25. translateXScaleOrder(); //实现顺序动画
  26. }
  27. private void initView()
  28. {
  29. mTestView = findViewById(R.id.test_view);
  30. }
  31. @TargetApi(VERSION_CODES.HONEYCOMB)
  32. private void translateX()
  33. {
  34. DisplayMetrics dm = new DisplayMetrics();
  35. getWindowManager().getDefaultDisplay().getMetrics(dm);
  36. ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTestView, "translationX", -(dm.widthPixels / 2 + mTestView.getWidth() / 2), 0F);
  37. objectAnimator.setDuration(1000);
  38. objectAnimator.setInterpolator(new BounceInterpolator());
  39. objectAnimator.addListener(new AnimatorListenerAdapter()
  40. {
  41. @Override
  42. public void onAnimationStart(Animator animation)
  43. {
  44. super.onAnimationStart(animation);
  45. mTestView.setVisibility(View.VISIBLE);
  46. }
  47. @Override
  48. public void onAnimationEnd(Animator animation)
  49. {
  50. super.onAnimationEnd(animation);
  51. }
  52. });
  53. objectAnimator.start();
  54. }
  55. @TargetApi(VERSION_CODES.HONEYCOMB)
  56. private void translateXScaleBoth()
  57. {
  58. PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
  59. PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
  60. PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
  61. ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mTestView, pvhX, pvhY, pvhZ);
  62. objectAnimator.setDuration(1000);
  63. objectAnimator.addListener(new AnimatorListenerAdapter()
  64. {
  65. @Override
  66. public void onAnimationStart(Animator animation)
  67. {
  68. super.onAnimationStart(animation);
  69. mTestView.setVisibility(View.VISIBLE);
  70. }
  71. });
  72. objectAnimator.start();
  73. }
  74. @TargetApi(VERSION_CODES.HONEYCOMB)
  75. private void translateXScaleOrder()
  76. {
  77. DisplayMetrics dm = new DisplayMetrics();
  78. getWindowManager().getDefaultDisplay().getMetrics(dm);
  79. ObjectAnimator translationXin = ObjectAnimator.ofFloat(mTestView, "translationX", -(dm.widthPixels / 2 + mTestView.getWidth() / 2), 0F);
  80. ObjectAnimator scaleXin = ObjectAnimator.ofFloat(mTestView, "scaleX", 1f, 0f, 6f);
  81. ObjectAnimator scaleYin = ObjectAnimator.ofFloat(mTestView, "scaleY", 1f, 0f, 6f);
  82. ObjectAnimator translationXout = ObjectAnimator.ofFloat(mTestView, "translationX", 0F, (dm.widthPixels / 2 + mTestView.getWidth() / 2));
  83. ObjectAnimator scaleXout = ObjectAnimator.ofFloat(mTestView, "scaleX", 6f, 0f);
  84. ObjectAnimator scaleYout = ObjectAnimator.ofFloat(mTestView, "scaleY", 6f, 0f);
  85. AnimatorSet set = new AnimatorSet();
  86. set.play(translationXin).before(scaleXin);
  87. set.play(scaleXin).with(scaleYin);
  88. set.play(translationXout).with(scaleXout).with(scaleYout).after(scaleXin);
  89. set.setDuration(1000);
  90. set.addListener(new AnimatorListenerAdapter()
  91. {
  92. @Override
  93. public void onAnimationStart(Animator animation)
  94. {
  95. super.onAnimationStart(animation);
  96. mTestView.setVisibility(View.VISIBLE);
  97. }
  98. });
  99. set.start();
  100. }
  101. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注