Android drawing an animated line

前端 未结 4 527
时光说笑
时光说笑 2020-12-23 10:24

I\'m currently working with graphics and paths, and I can successufully display whatever I want.

But instead of drawing a line directly on my SurfaceView, I\'d like

4条回答
  •  Happy的楠姐
    2020-12-23 11:04

    I just have resolve this problem, here what I do:

    private float[] mIntervals = { 0f, 0f };
    private float drawSpeed = 2f;
    private int currentPath = -1;
    private PathMeasure mPathMeasure = new PathMeasure();
    private ArrayList mListPath = new ArrayList(this.pathCount);
    
    
    @Override
    protected void onDraw(Canvas canvas) {
       if (mIntervals[1] <= 0f && currentPath < (pathCount - 1)) {
         // Set the current path to draw
         // getPath(int num) a function to return a path.
         Path newPath = this.getPath(mListPath.size());
         this.mListPath.add(newPath);
         this.mPathMeasure.setPath(newPath, false);
         mIntervals[0] = 0;
         mIntervals[1] = this.mPathMeasure.getLength();
       }
    
      if (mIntervals[1] > 0) {
         // draw the previous path
         int last = this.mListPath.size();
         for (int i = 0; i < last; i++) {
            canvas.drawPath(this.mListPath.get(i), mPaint);
         }
         // partially draw the last path
         this.mPaint.setPathEffect(new DashPathEffect(mIntervals, 0f));
    
         canvas.drawPath(this.mListPath.get(last), mPaint);
    
         // Update the path effects values, to draw a little more
         // on the path.
         mIntervals[0] += drawSpeed;
         mIntervals[1] -= drawSpeed;
    
         super.invalidate();
      } else {
         // The drawing have been done, draw it entirely
         for (int i = 0; i < this.mListPath.size(); i++) {
            canvas.drawPath(this.mListPath.get(i), mPaint);
         }
      }
    }
    

    This example, is an adaptation of what I've done (to simplify the example). Hope you will understand it. Since I've just made this function working, It lacks of optimizations and things like that.

    Hope it will help ;-)

提交回复
热议问题