Android中PathMeasure仿支付宝支付动画

  

<强>前言

  

在Android自定义视图中,路径可能用的比较多,PathMeasure可能用的比较少,就我而言,以前也没有使用过PathMeasure这个api,看到别人用PathMeasure和ValueAnimator结合在一起完成了很好的动画效果,于是我也学习下PathMeasure,此处记录下。

  

<强> PathMeasure

  

构造器:

  

 Android中PathMeasure仿支付宝支付动画

  

forceClosed含义:

     //创建一个路径对象   路径=new路径();   路径。函数(20、20);   路径。画线(200年,20);   路径。画线(200、400);      

在onDraw(帆布画布)中绘制路径

        @Override   保护无效alt=" Android中PathMeasure仿支付宝支付动画">

  

 Android中PathMeasure仿支付宝支付动画

  

当pathMeasure.setPath(路径,真)时:
  

  

 Android中PathMeasure仿支付宝支付动画

  

 Android中PathMeasure仿支付宝支付动画

  

可以看到:当forceClosed=true时,路径进行了闭合,相应的路径长度也变长了,即算上了斜边的长度。

  

<>强仿支付宝支付动画视图——LoadingView

  

效果:
  

  

 Android中PathMeasure仿支付宝支付动画

  

<>强思路:

  

绘制对号,叉号,主要是通过ValueAnimator结合getSegment()不断绘制新的弧形段,其中,叉号由两个路径组成,在第一个路径绘制完成时,需要调用pathMeasure.nextContour()跳转到另一个路径。

  

<强> getSegment()将获取的片段填充到destPath中,在Android 4.4及以下版本中,不能绘制,需要调用destPath.reset (), destPath.line(0, 0)

  

LoadingView完整代码:

  

        公开课LoadingView扩展视图{      私人最终int DEFAULT_COLOR=Color.BLACK;//默认圆弧颜色      私人最终int DEFAULT_STROKE_WIDTH=dp2Px (2);//默认圆弧宽度      私人最终布尔DEFAULT_IS_SHOW_RESULT=false;//默认不显示加载结果      私人最终int DEFAULT_VIEW_WIDTH=dp2Px (50);//控件默认宽度      私人最终int DEFAULT_VIEW_HEIGHT=dp2Px (50);//控件默认高度      私人int颜色;//圆弧颜色      私人int strokeWidth;//圆弧宽度      私人布尔isShowResult;//是否显示加载结果状态      私人油漆涂料;//画笔      私人int mWidth;//控件宽度      私人int mHeight;//控件高度      私人int半径;//圆弧所在圆的半径      私人int halfStrokeWidth;//画笔宽度的一半         私人int rotateDelta=4;      私人int curAngle=0;      私人int minAngle=-90;      私人int startAngle=-90;//上方顶点      私人int endAngle=0;      私人RectF RectF;      私人StateEnum StateEnum=StateEnum.LOADING;      私人successPath路径;      私人rightFailPath路径;      私人leftFailPath路径;      私人ValueAnimator successAnimator;      私人ValueAnimator rightFailAnimator;      私人ValueAnimator leftFailAnimator;      私人PathMeasure PathMeasure;      私人successValue浮动;      私人rightFailValue浮动;      私人leftFailValue浮动;      私人destPath路径;      私人AnimatorSet AnimatorSet;      公共LoadingView(上下文语境){   这(上下文,null);   }      公共LoadingView(上下文语境,AttributeSet attrs) {   超级(上下文,attrs);   init(上下文,attrs);   }         私人空间init(上下文语境,AttributeSet attrs) {   TypedArray TypedArray=零;   尝试{   typedArray=上下文。obtainStyledAttributes (attrs R.styleable.LoadingView);   颜色=typedArray.getColor (R.styleable。LoadingView_color DEFAULT_COLOR);   strokeWidth=(int) typedArray.getDimension (R.styleable。LoadingView_storkeWidth DEFAULT_STROKE_WIDTH);   isShowResult=typedArray.getBoolean (R.styleable。LoadingView_isShowResult DEFAULT_IS_SHOW_RESULT);   }捕捉(异常e) {   e.printStackTrace ();   最后}{   如果(typedArray !=null) {   typedArray.recycle ();   }   }   油漆=createPaint(颜色、strokeWidth Paint.Style.STROKE);   }         @Override   保护无效onSizeChanged (w int, int, int oldw int oldh) {   超级。onSizeChanged (w h oldw oldh);   mWidth=w;   mHeight=h;   日志。我(“调试”、“getMeasureWidth ()=" + getMeasuredWidth ());   日志。我(“调试”、“getMeasureHeight ()=" + getMeasuredHeight ());      半径=数学。分钟(mWidth mHeight)/2;   halfStrokeWidth=strokeWidth/2;      rectF=new rectF (halfStrokeWidth -半径,halfStrokeWidth半径,   ——halfStrokeWidth半径,半径- halfStrokeWidth);//成功路径   successPath=新路径();   successPath。函数(半径* 2/3 f, 0 f);   successPath。画线(半径半径/8 f/2 f);   successPath。画线(/2半径,半径/3);//失败路径,对上向左下   rightFailPath=新路径();   rightFailPath。函数f (f/3半径,半径/3);   rightFailPath。画线(半径/3 f,半径/3 f);//失败路径,左到右底部   leftFailPath=新路径();   leftFailPath。函数(半径/3 f,半径/3 f);   leftFailPath。画线(f/3半径,半径/3 f);      pathMeasure=new pathMeasure ();      destPath=新路径();      initSuccessAnimator ();   initFailAnimator ();   }      私人空间initSuccessAnimator () {//pathMeasure。setPath (successPath假);   successAnimator=ValueAnimator。ofFloat f (0, 1);   successAnimator.setDuration (1000);   successAnimator。setInterpolator(新LinearInterpolator ());   successAnimator。addUpdateListener(新ValueAnimator.AnimatorUpdateListener () {      @Override   公共空间onAnimationUpdate (ValueAnimator动画){   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android中PathMeasure仿支付宝支付动画