rosu alin
rosu alin

Reputation: 5830

How can I send an android.graphics.Path as a String or Json to another phone?

I have an array list of paths, which I need to send form one phone to another, so that the second phone could draw the picture that the user of the first phone has drawn. Now I have a function that is capable of sending a String message from one phone to another. How can I transform the path in a string, and then get all the necessary data to recreate it on the other phone?

Upvotes: 2

Views: 1372

Answers (1)

rosu alin
rosu alin

Reputation: 5830

I send an Array list of DrawEntities (object to draw) as a gson, and then process it:

 ArrayList<DrawEntity> pathsToDraw = paintv.getPaths();
                Gson gson = new Gson();
                String json = gson.toJson(pathsToDraw);

Now to get the path, in my paint view I save the X and Y of the coordinates of the path in every MOVE motion event:

  private void touch_move(float x, float y) {
    LogService.log(TAG, "touchmove" + y);
    xlist.add(x);
    ylist.add(y);
    float dx, dy;
    dx = Math.abs(x - mX);
    dy = Math.abs(y - mY);
    if ((dx >= 20) || (dy >= 20)) {
        hasmoved = true;
    }
    if (mPath == null) {
        mPath = new Path();
        mPath.moveTo(x, y);
    }
    if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
    LogService.log(TAG, "touchmove" + ylist.size());
}

 private void touch_up() {
    LogService.log(TAG, "touch up");
    mPath.lineTo(mX, mY);
    mPath.moveTo(mX, mY);
    DrawEntity draw = new DrawEntity(mPath, paint, stroke, xlist, ylist);
    draw.color = paint.getColor();
    if (hasmoved) {
        pathsToDraw.add(draw);
    }
    LogService.log(TAG, "touch up SIZE: " + pathsToDraw.size());
    xlist = null;
    ylist = null;
}

As you see, then I create the DrawEntity (which I will send) including those 2 lists (x, y) in the constructor.

Upvotes: 3

Related Questions