本篇文章已授权微信公众号 hongyangAndroid (鸿洋)独家公布

转载请标明出处:

http://blog.csdn.net/zxt0601/article/details/53040506

本文出自:【张旭童的CSDN】(http://blog.csdn.net/zxt0601)

代码传送门:喜欢的话。随手点个star。多谢

https://github.com/mcxtzhang/PathAnimView

一 概述

原本仅仅是想模仿一下我魂牵梦萦的StoreHouse效果。没想到意外撸出来一个工具库。

最简单使用方法,给我一个path(能够有多段),我还你一个动画。

I have a path.I have a view. (Oh~),Path(Anim)View.

  1. <com.mcxtzhang.pathanimlib.PathAnimView
  2. android:id="@+id/pathAnimView1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="60dp"
  5. android:background="@color/blue"
  6. android:padding="5dp"/>
  1. Path sPath = new Path();
  2. sPath.moveTo(0, 0);
  3. sPath.addCircle(40, 40, 30, Path.Direction.CW);
  4. pathAnimView1.setSourcePath(sPath);

先看效果图:(真机效果更棒哦,我自己的手机是去年某款599的手机,算是低端的了,6个View一起动画,不会卡,查看GPU呈现模式,95%时间都处于16ms线下面。性能还能够的)



当中

图1 是普通逐渐填充的效果,无限循环。

图2 是仿StoreHouse 残影流动效果。

(但与原版并非全然一模一样,预计原版不是用Path做的)

图3 是逐渐填充的效果,设置了仅仅运行一次。

图4 是仿StoreHouse效果。数据源来自R.array.xxxx

图5 是还有一种自己定义PathAnimHelper实现的自己定义动画效果。

相似Android L+ 系统进度条效果。

图6 是仿StoreHouse效果,可是将动画时长设置的非常大,所以能看到它逐渐的过程。

參数

眼下可配參数:

1 绘制方面,支持绘制Path的前景 背景色。

  1. //设置颜色
  2. fillView2.setColorBg(Color.WHITE).setColorFg(Color.BLACK);

2 动画方面,眼下支持设置动画的时长,是否无限循环等。

  1. //设置了动画总时长,仅仅运行一次的动画
  2. fillView2.setAnimTime(3000).setAnimInfinite(false).startAnim();

3 仿StoreHouse风格的View,还支持设置残影的长度

  1. //设动画时长。设置了stoneHouse残影长度
  2. storeView3.setPathMaxLength(1200).setAnimTime(20000).startAnim();

4 当然你能够拿到Paint自己搞事情:

  1. //当然你能够自己拿到Paint,然后搞事情。我这里设置线条宽度
  2. pathAnimView1.getPaint().setStrokeWidth(10);

数据源:

PathAnimView的数据源是Path

(给我一个Path。还你一个动画View)

所以内置了几种将别的资源->Path的方法。

1 直接传string。

StoreHouse风格支持的A-Z,0-9 “.” “- ” ” “(源自百万大神的库文末也有鸣谢。)

  1. //依据String 转化成Path
  2. setSourcePath(PathParserUtils.getPathFromArrayFloatList(StoreHousePath.getPath("ZhangXuTong", 1.1f, 16)));

2 定义在R.array.xxx里

  1. //动态设置 从StringArray里取
  2. storeView2.setSourcePath(PathParserUtils.getPathFromStringArray(this, R.array.storehouse, 3));

3 简单的SVG(半成品)

曾经从gayHub上找了一个SVG-PATH的转换类:SvgPathParser,如今派上了用场,简单的SVG-PATH。能够,复杂的还有问题。还须要继续寻找更加方案。

20170104更新:

完好的方案已经找到,博文:http://blog.csdn.net/zxt0601/article/details/54018970

轻松实现图片->SVG->PATH.

  1. //SVG转-》path
  2. //还在完好中。我从github上找了例如以下工具类,发现简单的SVG能够转path。复杂点的 就乱了
  3. /* SvgPathParser svgPathParser = new SvgPathParser();
  4. try {
  5. Path path = svgPathParser.parsePath("M1,1 L1,50 L50,50 L50,50 L50,1 Z");
  6. storeView3.setSourcePath(path);
  7. } catch (ParseException e) {
  8. e.printStackTrace();
  9. }*/

简单使用方法API

1 xml定义

普通PathAnimView

效果如图1 3。

动画是 进度填充直到满的效果。

  1. <com.mcxtzhang.pathanimlib.PathAnimView
  2. android:id="@+id/pathAnimView1"
  3. android:layout_width="wrap_content"
  4. android:layout_height="60dp"
  5. android:background="@color/blue"
  6. android:padding="5dp"/>

高仿StoreHouse风格AnimView:

这样的View显示出来的效果如图2 4 6 。动画是 残影流动的效果。

  1. <com.mcxtzhang.pathanimlib.StoreHouseAnimView
  2. android:id="@+id/storeView3"
  3. android:layout_width="wrap_content"
  4. android:layout_height="60dp"
  5. android:background="@android:color/black"
  6. android:padding="5dp"/>

2 開始动画

  1. fillView1.startAnim();

3 停止动画

  1. fillView1.stopAnim();

4 清除并停止动画

  1. fillView1.clearAnim();

略微 搞基 高级点的使用方法预览

看到这里细心的朋友可能会发现,上一节。我没有提第5个图View是怎么定义的, 而且第五个View的效果。貌似和其它的不一样,细致看动画是不是像Android L+的系统自带进度条ProgressBar的效果?

那说明它的动画效果和我先前提到的两种不一样,是的,一開始我撸是照着StoreHouse那种效果撸的,这是我第二天才扩展的。

高级的使用方法,就是本控件动画的扩展性

你全然能够通过继承PathAnimHelper类,重写onPathAnimCallback()方法。扩展动画。图5就是这么来的。

先讲使用方法预览。稍后章节会具体解释。

使用方法

对随意一个普通的PathAnimView,设置一个自己定义的PathAnimHelper类就可以:

  1. //代码演示样例 动态对path加工,通过Helper
  2. pathAnimView1.setPathAnimHelper(new CstSysLoadAnimHelper(pathAnimView1, pathAnimView1.getSourcePath(), pathAnimView1.getAnimPath()));

自己定义的PathAnimHelper类:

  1. /**
  2. * 介绍:自己定义的PathAnimHelper,实现相似Android L+ 进度条效果
  3. * 作者:zhangxutong
  4. * 邮箱:zhangxutong@imcoming.com
  5. * 时间: 2016/11/3.
  6. */
  7. public class CstSysLoadAnimHelper extends PathAnimHelper {
  8. public CstSysLoadAnimHelper(View view, Path sourcePath, Path animPath) {
  9. super(view, sourcePath, animPath);
  10. }
  11. public CstSysLoadAnimHelper(View view, Path sourcePath, Path animPath, long animTime, boolean isInfinite) {
  12. super(view, sourcePath, animPath, animTime, isInfinite);
  13. }
  14. @Override
  15. public void onPathAnimCallback(View view, Path sourcePath, Path animPath, PathMeasure pathMeasure, ValueAnimator animation) {
  16. float value = (float) animation.getAnimatedValue();
  17. //获取一个段落
  18. float end = pathMeasure.getLength() * value;
  19. float begin = (float) (end - ((0.5 - Math.abs(value - 0.5)) * pathMeasure.getLength()));
  20. animPath.reset();
  21. animPath.lineTo(0, 0);
  22. pathMeasure.getSegment(begin, end, animPath, true);
  23. }
  24. }

伸手党看到这里假设感兴趣,就能够直接一步gayhub了

(https://github.com/mcxtzhang/PathAnimView)

后文比較长。须要自带耐心观看。

二 架构预览

这里我简单画了一下本文介绍的几个类的类图:

对于重要方法和属性标注了一下。



我们的主角PathAnimView继承自View,是一个自己定义View。

它内部持有一个PathAnimHelper,专注做Path动画。它默认的实现是 逐渐填充 的动画效果。

普通情况下仅仅须要更换PathAnimHelper,PathAnimView就可以做出不同的动画。(图1第5个View)

可是假设须要扩充一些动画属性供用户设置,比如仿StoreHouse风格的动画View。想暴露 残影长度 属性供设置。

我这里採用的是:继承自PathAnimView。并添加属性get、set 方法,并重写getInitAnimHeper()方法,返回自己定义的PathAnimHelper

StoreHouseAnimView继承自PathAnimView,添加了残影长度的get、set方法。并重写getInitAnimHeper()方法,返回StoreHouseAnimHelper对象。 StoreHouseAnimHelper类继承的是PathAnimHelper

三 基础类的实现:

基础类是PathAnimViewPathAnimHelper

1 PathAnimView

先看PathAnimView:

这里我将一些不重要的get、set方法和构造方法剔除,留下比較重要的方法。

一个做路径动画的View

* 利用源Path绘制“底”

* 利用动画Path 绘制 填充动画

* 通过mPathAnimHelper 对SourcePath做动画:

* 一个SourcePath 内含多段Path,循环取出每段Path,并做一个动画


代码本身不难,凝视也比較具体,核心的话,就是onDraw()方法咯:

我这里用平移做的paddingLeft、paddingTop。

先利用源Path(mSourcePath)绘制底边的样子。

再利用变化的animPath(mAnimPath)绘制前景。这样animPath不断变化,而且重绘View->onDraw(),前景就会不断变化,形成动画效果。

那么核心就是animPath的的变化了。animPath的变化交由 mPathAnimHelper去做。


核心源代码例如以下:

  1. public class PathAnimView extends View {
  2. protected Path mSourcePath;//须要做动画的源Path
  3. protected Path mAnimPath;//用于绘制动画的Path
  4. protected Paint mPaint;
  5. protected int mColorBg = Color.GRAY;//背景色
  6. protected int mColorFg = Color.WHITE;//前景色 填充色
  7. protected PathAnimHelper mPathAnimHelper;//Path动画工具类
  8. protected int mPaddingLeft, mPaddingTop;
  9. public PathAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
  10. super(context, attrs, defStyleAttr);
  11. init();
  12. }
  13. /**
  14. * 这种方法可能会经经常使用到,用于设置源Path
  15. *
  16. * @param sourcePath
  17. * @return
  18. */
  19. public PathAnimView setSourcePath(Path sourcePath) {
  20. mSourcePath = sourcePath;
  21. initAnimHelper();
  22. return this;
  23. }
  24. /**
  25. * INIT FUNC
  26. **/
  27. protected void init() {
  28. //Paint
  29. mPaint = new Paint();
  30. mPaint.setAntiAlias(true);
  31. mPaint.setStyle(Paint.Style.STROKE);
  32. //动画路径仅仅要初始化就可以
  33. mAnimPath = new Path();
  34. //初始化动画帮助类
  35. initAnimHelper();
  36. }
  37. /**
  38. * 初始化动画帮助类
  39. */
  40. protected void initAnimHelper() {
  41. mPathAnimHelper = getInitAnimHeper();
  42. //mPathAnimHelper = new PathAnimHelper(this, mSourcePath, mAnimPath, 1500, true);
  43. }
  44. /**
  45. * 子类可通过重写这种方法,返回自己定义的AnimHelper
  46. *
  47. * @return
  48. */
  49. protected PathAnimHelper getInitAnimHeper() {
  50. return new PathAnimHelper(this, mSourcePath, mAnimPath);
  51. }
  52. /**
  53. * draw FUNC
  54. **/
  55. @Override
  56. protected void onDraw(Canvas canvas) {
  57. super.onDraw(canvas);
  58. //平移
  59. canvas.translate(mPaddingLeft, mPaddingTop);
  60. //先绘制底。
  61. mPaint.setColor(mColorBg);
  62. canvas.drawPath(mSourcePath, mPaint);
  63. //再绘制前景,mAnimPath不断变化,不断重绘View的话,就会有动画效果。
  64. mPaint.setColor(mColorFg);
  65. canvas.drawPath(mAnimPath, mPaint);
  66. }
  67. /**
  68. * 设置动画 循环
  69. */
  70. public PathAnimView setAnimInfinite(boolean infinite) {
  71. mPathAnimHelper.setInfinite(infinite);
  72. return this;
  73. }
  74. /**
  75. * 设置动画 总时长
  76. */
  77. public PathAnimView setAnimTime(long animTime) {
  78. mPathAnimHelper.setAnimTime(animTime);
  79. return this;
  80. }
  81. /**
  82. * 运行循环动画
  83. */
  84. public void startAnim() {
  85. mPathAnimHelper.startAnim();
  86. }
  87. /**
  88. * 停止动画
  89. */
  90. public void stopAnim() {
  91. mPathAnimHelper.stopAnim();
  92. }
  93. /**
  94. * 清除并停止动画
  95. */
  96. public void clearAnim() {
  97. stopAnim();
  98. mAnimPath.reset();
  99. mAnimPath.lineTo(0, 0);
  100. invalidate();
  101. }
  102. }

2 PathAnimHelper

看看最基础的PathAnimHelper类是怎么做的,一样省略一些代码:

它是一个PathAnimView的Path动画的工具类

* 一个SourcePath 内含多段(一段)Path,循环取出每段Path。并做一个动画,

* 默认动画时间1500ms,无限循环

* 能够通过构造函数改动这两个參数

* 对外暴露 startAnim() 和 stopAnim()两个方法

* 子类可通过重写onPathAnimCallback()方法,对animPath进行再次操作,从而定义不同的动画效果

值得一提的是,这里的动画时间,是指循环取出SourcePath里的N段Path的总时间。


startAnim()方法是入口,这种方法会在PathAnimView里被调用。

startAnim()方法里,先初始化一个PathMeasure,以及重置animPath

然后利用PathMeasure.nextContour()方法,循环一遍SourcePath的Path段数count,

利用这个count求出每段小Path应该运行的动画时间:totalDuaration / count

然后便调用loopAnim()方法,循环取出每一段path 。并运行动画。


loopAnim()方法里,定义一个无限循环的属性动画mAnimator

为其设置AnimatorUpdateListeneronAnimationRepeat,监听动画的更新和反复。

重点就在这两个监听器里:

  1. public void onAnimationUpdate(ValueAnimator animation) {
  2. //添加一个callback 便于子类重写搞事情
  3. onPathAnimCallback(view, sourcePath, animPath, pathMeasure, animation);
  4. //通知View刷新自己
  5. view.invalidate();
  6. }

动画每次Update的时候,回调onPathAnimCallback()方法。在里面对animPath做处理。

对AnimPath处理以后,就能够让View绘制新animPath形成动画了:

然后就是让View重绘。这样就会重走onDraw()方法,就是上一节提到的内容。


onPathAnimCallback()方法也非常easy,按动画进度值。取出当前这一小段的path的部分路径,赋值给animPath。

  1. public void onPathAnimCallback(View view, Path sourcePath, Path animPath, PathMeasure pathMeasure, ValueAnimator animation) {
  2. float value = (float) animation.getAnimatedValue();
  3. //获取一个段落
  4. pathMeasure.getSegment(0, pathMeasure.getLength() * value, animPath, true);
  5. }

在Repeat监听器里:

  1. public void onAnimationRepeat(Animator animation) {
  2. //绘制完一条Path之后,再绘制下一条
  3. pathMeasure.nextContour();
  4. //长度为0 说明一次循环结束
  5. if (pathMeasure.getLength() == 0) {
  6. if (isInfinite) {//假设须要循环动画
  7. animPath.reset();
  8. animPath.lineTo(0, 0);
  9. pathMeasure.setPath(sourcePath, false);
  10. } else {//不须要就停止(由于repeat是无限 须要手动停止)
  11. animation.end();
  12. }
  13. }
  14. }

由于SourcePath里是可能含有1+段Path的,这里是合适的时机。利用pathMeasure.nextContour();循环取出下一段Path, 推断一下新Path的长度,假设为0,说明这一次大循环结束,即用户视觉上的一次动画进度100%了。

这里推断我们设置的isInfinite属性,

假设是true。说明是循环动画。那么做初始化工作:

清空我们的animPath,初始化pathMeasure。(和startAnim()方法里的初始化工作一致)。

假设是false,说明动画须要停止,那么手动调用animation.end()停止动画。(图1,第三个动画)


核心源代码例如以下:

  1. public class PathAnimHelper {
  2. protected static final long mDefaultAnimTime = 1500;//默认动画总时间
  3. protected View mView;//运行动画的View
  4. protected Path mSourcePath;//源Path
  5. protected Path mAnimPath;//用于绘制动画的Path
  6. protected long mAnimTime;//动画一共的时间
  7. protected boolean mIsInfinite;//是否无限循环
  8. protected ValueAnimator mAnimator;//动画对象
  9. public PathAnimHelper(View view, Path sourcePath, Path animPath, long animTime, boolean isInfinite) {
  10. if (view == null || sourcePath == null || animPath == null) {
  11. Log.e(TAG, "PathAnimHelper init error: view 、sourcePath、animPath can not be null");
  12. return;
  13. }
  14. mView = view;
  15. mSourcePath = sourcePath;
  16. mAnimPath = animPath;
  17. mAnimTime = animTime;
  18. mIsInfinite = isInfinite;
  19. }
  20. /**
  21. * 运行动画
  22. */
  23. public void startAnim() {
  24. startAnim(mView, mSourcePath, mAnimPath, mAnimTime, mIsInfinite);
  25. }
  26. /**
  27. * 一个SourcePath 内含多段Path,循环取出每段Path。并做一个动画
  28. * 自己定义动画的总时间
  29. * 和是否循环
  30. *
  31. * @param view 须要做动画的自己定义View
  32. * @param sourcePath 源Path
  33. * @param animPath 自己定义View用这个Path做动画
  34. * @param totalDuaration 动画一共的时间
  35. * @param isInfinite 是否无限循环
  36. */
  37. protected void startAnim(View view, Path sourcePath, Path animPath, long totalDuaration, boolean isInfinite) {
  38. if (view == null || sourcePath == null || animPath == null) {
  39. return;
  40. }
  41. PathMeasure pathMeasure = new PathMeasure();
  42. //先重置一下须要显示动画的path
  43. animPath.reset();
  44. animPath.lineTo(0, 0);
  45. pathMeasure.setPath(sourcePath, false);
  46. //这里仅仅是为了 计算一下每一段的duration
  47. int count = 0;
  48. while (pathMeasure.getLength() != 0) {
  49. pathMeasure.nextContour();
  50. count++;
  51. }
  52. //经过上面这段计算duration代码的折腾 须要又一次初始化pathMeasure
  53. pathMeasure.setPath(sourcePath, false);
  54. loopAnim(view, sourcePath, animPath, totalDuaration, pathMeasure, totalDuaration / count, isInfinite);
  55. }
  56. /**
  57. * 循环取出每一段path ,并运行动画
  58. *
  59. * @param animPath 自己定义View用这个Path做动画
  60. * @param pathMeasure 用于測量的PathMeasure
  61. */
  62. protected void loopAnim(final View view, final Path sourcePath, final Path animPath, final long totalDuaration, final PathMeasure pathMeasure, final long duration, final boolean isInfinite) {
  63. //动画正在运行的话,先stop吧。万一有人要使用新动画呢,(正经用户不会这么用。

  64. stopAnim();
  65. mAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
  66. mAnimator.setInterpolator(new LinearInterpolator());
  67. mAnimator.setDuration(duration);
  68. mAnimator.setRepeatCount(ValueAnimator.INFINITE);
  69. mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  70. @Override
  71. public void onAnimationUpdate(ValueAnimator animation) {
  72. //添加一个callback 便于子类重写搞事情
  73. onPathAnimCallback(view, sourcePath, animPath, pathMeasure, animation);
  74. //通知View刷新自己
  75. view.invalidate();
  76. }
  77. });
  78. mAnimator.addListener(new AnimatorListenerAdapter() {
  79. @Override
  80. public void onAnimationRepeat(Animator animation) {
  81. //每段path走完后,要补一下 某些情况会出现 animPath不满的情况
  82. pathMeasure.getSegment(0, pathMeasure.getLength(), animPath, true);
  83. //绘制完一条Path之后,再绘制下一条
  84. pathMeasure.nextContour();
  85. //长度为0 说明一次循环结束
  86. if (pathMeasure.getLength() == 0) {
  87. if (isInfinite) {//假设须要循环动画
  88. animPath.reset();
  89. animPath.lineTo(0, 0);
  90. pathMeasure.setPath(sourcePath, false);
  91. } else {//不须要就停止(由于repeat是无限 须要手动停止)
  92. animation.end();
  93. }
  94. }
  95. }
  96. });
  97. mAnimator.start();
  98. }
  99. /**
  100. * 停止动画
  101. */
  102. public void stopAnim() {
  103. if (null != mAnimator && mAnimator.isRunning()) {
  104. mAnimator.end();
  105. }
  106. }
  107. /**
  108. * 用于子类继承搞事情,对animPath进行再次操作的函数
  109. *
  110. * @param view
  111. * @param sourcePath
  112. * @param animPath
  113. * @param pathMeasure
  114. */
  115. public void onPathAnimCallback(View view, Path sourcePath, Path animPath, PathMeasure pathMeasure, ValueAnimator animation) {
  116. float value = (float) animation.getAnimatedValue();
  117. //获取一个段落
  118. pathMeasure.getSegment(0, pathMeasure.getLength() * value, animPath, true);
  119. }
  120. }

至此两个最基础的类就讲完了。如此简单就可实现图1第1、3个动画效果。

四 实现StoreHouse风格

我们前面提过,扩展动画。核心是继承PathAnimHelper 重写onPathAnimCallback()方法就可以,所以实现StoreHouse风格。核心类就是StoreHouseAnimHelper

1 StoreHouseAnimHelper

  • 介绍:仿StoreHouse风格的PathAnimHepler
  • 添加了一个动画残影长度的属性:mPathMaxLength,默认值是400
  • 因没有找到实用的API,这里实现StoreHouse的方法。是手工计算的,不是非常爽。
  • 思路是是循环一遍AnimPath,记录里面每一段小Path的length。
  • 然后再逆序遍历AnimPath,从后面截取 残影长度 的Path,
  • 再复制给AnimPath。

核心代码例如以下:

  1. public class StoreHouseAnimHelper extends PathAnimHelper {
  2. private final static long MAX_LENGTH = 400;
  3. private long mPathMaxLength;//残影路径最大长度
  4. Path mStonePath;//暂存一下路径,终于要复制给animPath的
  5. PathMeasure mPm;
  6. private ArrayList<Float> mPathLengthArray;//路径长度array
  7. private SparseArray<Boolean> mPathNeedAddArray;//路径是否须要被所有Add的Array
  8. private int partIndex;//残缺的index
  9. private float partLength;//残缺部分的长度
  10. public StoreHouseAnimHelper(View view, Path sourcePath, Path animPath, long animTime, boolean isInfinite) {
  11. super(view, sourcePath, animPath, animTime, isInfinite);
  12. mPathMaxLength = MAX_LENGTH;
  13. mStonePath = new Path();
  14. mPm = new PathMeasure();
  15. mPathLengthArray = new ArrayList<>();//顺序存放path的length
  16. mPathNeedAddArray = new SparseArray<>();
  17. }
  18. @Override
  19. public void onPathAnimCallback(View view, Path sourcePath, Path animPath, PathMeasure pathMeasure, ValueAnimator animation) {
  20. super.onPathAnimCallback(view, sourcePath, animPath, pathMeasure, animation);
  21. //仿StoneHouse效果 ,如今的做法非常挫
  22. //重置变量
  23. mStonePath.reset();
  24. mStonePath.lineTo(0, 0);
  25. mPathLengthArray.clear();
  26. //循环一遍AnimPath,记录里面每一段小Path的length。
  27. mPm.setPath(animPath, false);
  28. while (mPm.getLength() != 0) {
  29. mPathLengthArray.add(mPm.getLength());
  30. mPm.nextContour();
  31. }
  32. //逆序遍历AnimPath。记录哪些子Path是须要add的,而且记录那段须要部分add的path的下标
  33. mPathNeedAddArray.clear();
  34. float totalLength = 0;
  35. partIndex = 0;
  36. partLength = 0;
  37. for (int i = mPathLengthArray.size() - 1; i >= 0; i--) {
  38. if (totalLength + mPathLengthArray.get(i) <= mPathMaxLength) {//加上了也没满
  39. mPathNeedAddArray.put(i, true);
  40. totalLength = totalLength + mPathLengthArray.get(i);
  41. } else if (totalLength < mPathMaxLength) {//加上了满了。可是不加就没满
  42. partIndex = i;
  43. partLength = mPathMaxLength - totalLength;
  44. totalLength = totalLength + mPathLengthArray.get(i);
  45. }
  46. }
  47. //循环Path,并得到终于要显示的AnimPath
  48. mPm.setPath(animPath, false);
  49. int i = 0;
  50. while (mPm.getLength() != 0) {
  51. if (mPathNeedAddArray.get(i, false)) {
  52. mPm.getSegment(0, mPm.getLength(), mStonePath, true);
  53. } else if (i == partIndex) {
  54. mPm.getSegment(mPm.getLength() - partLength, mPm.getLength(), mStonePath, true);
  55. }
  56. mPm.nextContour();
  57. i++;
  58. }
  59. animPath.set(mStonePath);
  60. }
  61. }

2 StoreHouseAnimView

直接上码了,得益于我们的设计。非常easy:

重写getInitAnimHeper() 返回我们的StoreHouseAnimHelper,并添加残影长度的get、set方法。

  1. public class StoreHouseAnimView extends PathAnimView {
  2. public StoreHouseAnimView(Context context) {
  3. this(context, null);
  4. }
  5. public StoreHouseAnimView(Context context, AttributeSet attrs) {
  6. this(context, attrs, 0);
  7. }
  8. public StoreHouseAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
  9. super(context, attrs, defStyleAttr);
  10. }
  11. /**
  12. * GET SET FUNC
  13. **/
  14. public long getPathMaxLength() {
  15. return ((StoreHouseAnimHelper) mPathAnimHelper).getPathMaxLength();
  16. }
  17. /**
  18. * 设置残影最大长度
  19. *
  20. * @param pathMaxLength
  21. * @return
  22. */
  23. public StoreHouseAnimView setPathMaxLength(long pathMaxLength) {
  24. ((StoreHouseAnimHelper) mPathAnimHelper).setPathMaxLength(pathMaxLength);
  25. return this;
  26. }
  27. @Override
  28. protected PathAnimHelper getInitAnimHeper() {
  29. return new StoreHouseAnimHelper(this, mSourcePath, mAnimPath);
  30. }
  31. }

五 动态扩展动画效果

前面提过,如图1第五个动画的效果,就是后期我添加扩展的。分析一下这样的效果,它和普通的PathAnimView的效果仅仅有动画不同,也不须要额外引入属性暴露出去供设置,所以这样的场景,我们仅仅须要重写一个PathAnimHelper类,set给PathAnimView就可以。

代码第一章节也提过。


一点注意的地方就是,这里没有同第四章节那样调用super.onPathAnimCallback(view, sourcePath, animPath, pathMeasure, animation);

由于第四章仿StoreHouse的效果,是在第三章的效果基础之上加工而成的。所以须要PathAnimHeper先处理一下。

而我们这里实现的仿系统ProgressBar的效果。则是全然重写的。

核心方法例如以下重写,非常easy,不再赘述:

  1. @Override
  2. public void onPathAnimCallback(View view, Path sourcePath, Path animPath, PathMeasure pathMeasure, ValueAnimator animation) {
  3. float value = (float) animation.getAnimatedValue();
  4. //获取一个段落
  5. float end = pathMeasure.getLength() * value;
  6. float begin = (float) (end - ((0.5 - Math.abs(value - 0.5)) * pathMeasure.getLength()));
  7. animPath.reset();
  8. animPath.lineTo(0, 0);
  9. pathMeasure.getSegment(begin, end, animPath, true);
  10. }

六 总结&待完好:

总结起来就是 I have a path.I have a view. (Oh~),Path(Anim)View.

利用这条裤子,仅仅要传一个Path进去,就能够实现多姿多彩的酷炫Path动画,假设对动画不惬意,还能够自己动态扩展

眼下最急需完好的:

SVG->Android PATH的转换,

希望有知道的兄弟能够告知一下,多谢。

20170104更新:

完好的方案已经找到,博文:http://blog.csdn.net/zxt0601/article/details/54018970

轻松实现图片->SVG->PATH.

代码传送门:喜欢的话,随手点个star。多谢

https://github.com/mcxtzhang/PathAnimView

七 引用致谢

StoreHouse风格点阵资源引用处,也是我第一次看见StoreHouse动画:百万大神的库: https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh

一開始我看到这样的动画,我还是个小菜鸡。也不知道怎么实现的,可是一直在我脑海里挥之不去。

后来突然有一天想到能够用Path、PathMeasure做自己定义View动画来实现,就開始动笔写了起来。

发现Path的路径不太好获取,于是翻看百万大神的库。发现他并非使用的Path动画,可是的的确确是利用点阵设置数据源的。于是我就借助了这些最原始的点阵资源,撸出了这么一个Path动画。

最初仅仅是想实现这么一个效果。了却我的心愿,没想到还有意外收获。有了这个Path动画工具类库。

说实话写这么一个东西。我不知不觉也提升了,曾经可能不太会把层级分的这么开,利用继承组合的方式去扩展功能。

曾经大多还是C V 一份代码改一改,像图上的效果,我可能会分开自己定义三个View去做,复制一些反复代码也不在乎。看来坚持会有收获。

希望我们都一起进步。

看大神们都有QQ群,

向他们靠齐。

我也建了个QQ搞基交流群:

557266366 。

转载请标明出处:

http://blog.csdn.net/zxt0601/article/details/53040506

本文出自:【张旭童的CSDN】(http://blog.csdn.net/zxt0601)

代码传送门:喜欢的话,随手点个star。

多谢

https://github.com/mcxtzhang/PathAnimView

【Android】 给我一个Path,还你一个酷炫动画的更多相关文章

  1. jQuery Scroll Path 滚插视图酷炫

    jQuery Scroll Path是一个jQuery的滚动路径插件,可以让你自定义滚动路径.该插件是使用canvas flavored的语法来绘制路径.可以通过鼠标滚轮上/下箭头键和空格键来查看路径 ...

  2. Android官方教程翻译(1)——创建第一个Android应用

    转载请注明出处:http://blog.csdn.net/dawanganban/article/details/9822431 Building Your First App GETSTARTED ...

  3. android穿越之旅--如何弹出一个非比寻常的窗体

    上一篇中介绍了一种闻所未闻在android执行java命令的方法,虽然这是一种非常"高级"的技术,然后并没有什么卵用,因此被移除了博客园首页.实际上也并不是一点用处也没有,对已立即 ...

  4. Android开发自学笔记(Android Studio1.3.1)—2.开始第一个Android应用

    一.前言      使用Android Studio开发Android应用是一件非常简单的事情,因为它会帮你自动完成很多工作.本篇我们主要完成一个单击按钮在文本框显示当前时间的简单应用,借此来演示一下 ...

  5. 【Android Developers Training】 4. 启动另一个Activity

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  6. android一个app打开另一个app的指定页面

    一个app打开另一个app的指定页面方法 有以下几种 1.通过包名.类名 2.通过intent的 action 3.通过Url 方案1. ComponentName componentName = n ...

  7. Android一个工程引用另一个工程的方法

    一个工程包含另一个工程.相当于一个jar包的引用.但又不是jar包反而像个package 现在已经有了一个Android工程A.我们想扩展A的功能,但是不想在A的基础上做开发,于是新建了另外一个And ...

  8. 【Android Studio探索之路系列】之中的一个:Android Studio开篇

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...

  9. 我把阿里、腾讯、字节跳动、美团等Android性能优化实战整合成了一个PDF文档

    安卓开发大军浩浩荡荡,经过近十年的发展,Android技术优化日异月新,如今Android 11.0 已经发布,Android系统性能也已经非常流畅,可以在体验上完全媲美iOS. 但是,到了各大厂商手 ...

随机推荐

  1. NOI 1.5编程基础之循环控制 44:第n小的质数

    描述 输入一个正整数n,求第n小的质数. 输入 一个不超过10000的正整数n. 输出 第n小的质数. 样例输入 10 样例输出 29

  2. intellij idea android错误: Missing styles. Is the correct theme chosen for this layout?

    Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layou ...

  3. 【数位dp】hdu3555 Bomb

    题意就是找0到n有多少个数中含有49.数据范围接近10^20 DP的状态是2维的dp[len][3]dp[len][0] 代表长度为len不含49的方案数dp[len][1] 代表长度为len不含49 ...

  4. JavaSE目录

    常识,环境变量,注释 标示符,常量,进制转换,类型转换,位运算符,语句 数组,函数 面向对象 多线程 String 包装类 集合 其他对象 IO流,IO流--FileReader&&F ...

  5. ethtool 命令输出的注意点--网卡参数

    http://blog.csdn.net/msdnchina/article/details/70339689

  6. 数据库问题5-SYS.SYSPROCESSES使用和查找死锁

    http://blog.sina.com.cn/s/blog_62c4727d0100jc5z.html (一)理論部份 sys.sysprocesses (Transact-SQL) http:// ...

  7. ldr与adr的区别

    参考: http://coon.blogbus.com/logs/2738861.html http://hi.baidu.com/for_guanghui/item/73e60bbcc8be15a2 ...

  8. Install and Enable Telnet server in Ubuntu Linux

    转:http://ubuntuguide.net/install-and-enable-telnet-server-in-ubuntu-linux 参考:http://auxnet.org/index ...

  9. Swift,结构体与类

    1.结构体(小的类就是用struct来写) struct arrow{ var x=0,y=0 } 2.类(常用) class a{ var a=10 var b=20 } var b=a() //实 ...

  10. Android ViewStub的使用方法

    大家写项目的时候肯定会有一些东西提前写好,可是不到一定条件是不想让它显示出来的.我们可能的做法就是让它View.GONE 或View.INVISIBLE等到一定条件了在代码里面这设置View.VISI ...