Drawing multiple lines on canvas with delay in between
Asked Answered
N

1

1

after few months i finally managed to create my first non trivial app. Now I want to add some nice animation (not sure it is a proper term), like step-by-step drawing my graph. Gif below explains what I need. At this point my app draw the graph at once, without any delaying.

I need something like on this pic

In MyFragment class there is FrameLayout where custom View (my graph) is added:

public class MyFragment extends Fragment{
...
FrameLayout mFL;
MyDraw mydraw = new MyDraw(getContext(),floatArray_coord_X,floatArray_coord_Y);
mFL.addView(mydraw);
...

}

Then in my custom view class some calc are made and lines are drawn:

public class MyDraw extends View{
  private Bitmap mBitmap;
  private Canvas mCanvas;
  ...

  @Override
  public void onDraw(Canvas canvas){

    mBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
                                    Bitmap.Config.ARGB_8888); 
    mCanvas = new Canvas(bitmap);       
    ...

    // draw brown lines between two neighbour (X,Y) points 
    for (int i = 0; i < floatArray_coord_X.length - 1; i++) {
            mCanvas.drawLine(floatArray_coord_X[i],     floatArray_coord_Y[i],
                             floatArray_coord_X[i + 1], floatArray_coord_Y[i + 1],
                             mypaint0);
        }

    ...
    // draw black lines at calculated (X,Y) points
    for (int i = 0; i < floatArray_coord_X_calc.length - 1; i++) {
            mCanvas.drawLine(floatArray_coord_X_calc[i],     floatArray_coord_Y_calc[i],
                             floatArray_coord_X_calc[i + 1], floatArray_coord_Y_calc[i + 1],
                             mypaint1);
        }
    ...
    // draw blue lines at newly calculated (X,Y) points
    drawBlueLines(); // with extra calc
    ...     

    canvas.drawBitmap(bitmap, 0, 0, null);
}

}

I tried to accomplish that by using Handler/Runnable - now i know that calling it inside onDraw isn`t good (at least my attempt wasn't good)... Any suggestion how to achieve this?

Nubile answered 10/11, 2017 at 9:59 Comment(0)
K
1

Each call of View.onDraw() method should draw next line of your chart and this will looks like animation you described. To achieve delay between frames you can use View.postInvalidateDelayed():

private ArrayDeque<Point> mPoints = new ArrayDeque<>();
private Path mPath;
private Paint mPaint;
private int mBackgroundColor;
...
@Override
protected void onDraw(Canvas canvas) {
    Point nextPoint = mPoints.pollFirst();
    if (nextPoint != null) {
        mPath.lineTo(nextPoint.x, nextPoint.y);
        canvas.drawColor(mBackgroundColor);
        canvas.drawPath(mPath, mPaint);
        if (mPoints.size() > 0) {
            postInvalidateDelayed(TimeUnit.SECONDS.toMillis(1));
        }
    }
}

public void drawChart(List<Point> points, int backgroundColor, int foregroundColor) {
    mPoints.addAll(points);
    mBackgroundColor = backgroundColor;
    mPaint = new Paint();
    mPaint.setColor(foregroundColor);
    mPaint.setStrokeWidth(5);
    mPaint.setStyle(Paint.Style.STROKE);
    mPath = new Path();
    mPath.moveTo(0,0);
    postInvalidateDelayed(TimeUnit.SECONDS.toMillis(1));
}

Start animation from Activity:

ChartView chart = (ChartView) findViewById(R.id.chart);
List<Point> points = new ArrayList<>();
points.add(new Point(0, 200));
points.add(new Point(200, 200));
points.add(new Point(200, 0));
points.add(new Point(0,0));
chart.drawChart(points, Color.BLACK, Color.RED);
Katiekatina answered 10/11, 2017 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.