当前位置:   article > 正文

记录一次自定义动效组件_动效是否能做成组件

动效是否能做成组件

android 酷炫文案随机归位动效实现

 记录一次自定义控件的实现原理

动画效果分解为一个随机位置归位动效和一个渐变的动效

1.根据文案在onSizechange方法里生成一个随机位置的集合和一个最终位置的集合 用于记录每个字符的位置

2.监听valueanimation动态计算随机位置当前应该到达的位置然后重绘界面

3.监听动效结束开始初始化一个渐变的动效 使用LinearGradient和Matrix实现渐变效果 渐变效果实现参考文末链接

 

关键代码如下

  1. @Override
  2. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  3. super.onSizeChanged(w, h, oldw, oldh);
  4. initTranslateAnimation(w, h);
  5. }
  6. private void initTranslateAnimation(int w, int h) {
  7. int startX;
  8. int centerY;
  9. float contenWith = mPaint.measureText(contentStr);
  10. Rect rect = new Rect();
  11. mPaint.getTextBounds(contentStr, 0, contentStr.length(), rect);
  12. startX = (int) ((w - contenWith) / 2);
  13. centerY = (h + rect.height()) / 2;
  14. queitList.clear();
  15. randowList.clear();
  16. for (int i = 0; i < contentStr.length(); i++) {
  17. char c = contentStr.charAt(i);
  18. float singleW = mPaint.measureText(String.valueOf(c));
  19. //记录每个字符的开始坐标 (文字绘制的左下角坐标)
  20. queitList.add(new PointF(startX, centerY));
  21. // 生成随机位置坐标
  22. randowList.add(new PointF((float) Math.random() * (w - singleW) + singleW, (float) Math.random() * (h - rect.height()) + rect.height()));
  23. startX += (singleW + 3);
  24. }
  25. animator = ValueAnimator.ofFloat(0, 1);
  26. animator.setDuration(2000);
  27. TimeInterpolator intercept = new DecelerateInterpolator(0.05f);
  28. animator.setInterpolator(intercept);
  29. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  30. @Override
  31. public void onAnimationUpdate(ValueAnimator animation) {
  32. // 计算当前文字的坐标并重绘
  33. float value = (float) animation.getAnimatedValue();
  34. for (int i = 0; i < randowList.size(); i++) {
  35. PointF pointF = randowList.get(i);
  36. PointF pointF1 = queitList.get(i);
  37. pointF.x = pointF.x + (pointF1.x - pointF.x) * value;
  38. pointF.y = pointF.y + (pointF1.y - pointF.y) * value;
  39. }
  40. postInvalidate();
  41. }
  42. });
  43. // 检听归位动画结束开始渐变动画
  44. animator.addListener(new Animator.AnimatorListener() {
  45. @Override
  46. public void onAnimationStart(Animator animation) {
  47. isTranslateEnd = false;
  48. }
  49. @Override
  50. public void onAnimationEnd(Animator animation) {
  51. isTranslateEnd = true;
  52. initLightAnimation();
  53. }
  54. @Override
  55. public void onAnimationCancel(Animator animation) {
  56. }
  57. @Override
  58. public void onAnimationRepeat(Animator animation) {
  59. }
  60. });
  61. animator.start();
  62. }
  63. private void initLightAnimation() {
  64. float strW = mPaint.measureText(contentStr);
  65. PointF pointF = queitList.get(0);
  66. // 从文案内容前面开始渐变 注意TileMode.CLAMP 时为了文字颜色不会突变 开始和结束的颜色和textColor保持一致
  67. final Shader shader = new LinearGradient(pointF.x-strW, 0, pointF.x, 0, new int[]{Color.BLACK,Color.RED,Color.WHITE,Color.BLACK}, null,Shader.TileMode.CLAMP);
  68. final Matrix matrix = new Matrix();
  69. shader.setLocalMatrix(matrix);
  70. mPaint.setShader(shader);
  71. //
  72. ValueAnimator animator = ValueAnimator.ofFloat(0, getWidth()*2);
  73. animator.setDuration(1000);
  74. animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  75. @Override
  76. public void onAnimationUpdate(ValueAnimator animation) {
  77. //计算Matrix translate距离
  78. float value = (float) animation.getAnimatedValue();
  79. matrix.setTranslate(value, 0);
  80. shader.setLocalMatrix(matrix);
  81. postInvalidate();
  82. }
  83. });
  84. animator.start();
  85. }
  86. @Override
  87. protected void onDraw(Canvas canvas) {
  88. super.onDraw(canvas);
  89. canvas.drawRect(0, 0, getWidth(), getHeight(), mPaints);
  90. for (int i = 0; i < contentStr.length(); i++) {
  91. PointF pointF = isTranslateEnd ? queitList.get(i) : randowList.get(i);
  92. canvas.drawText(String.valueOf(contentStr.charAt(i)), pointF.x, pointF.y, mPaint);
  93. }
  94. }
  95. //开始动效方法
  96. public void start() {
  97. if (animator.isRunning()) {
  98. animator.cancel();
  99. }
  100. initTranslateAnimation(getWidth(), getHeight());
  101. }

 

 

参考文献

https://blog.csdn.net/u012702547/article/details/50821044

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

闽ICP备14008679号