garciam202
garciam202

Reputation: 631

Save path to reuse on canvas

I am working on a canvas. I permit draw paths like this:

// when ACTION_DOWN start touch according to the x,y values
    private void startTouch(float x, float y) {
        trazo.moveTo(x, y);
        mX = x;
        mY = y;
    }

    // when ACTION_MOVE move touch according to the x,y values
    private void moveTouch(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOLERANCE || dy >= TOLERANCE) {
            trazo.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    // when ACTION_UP stop touch
    private void upTouch() {
        trazo.lineTo(mX, mY);
        mCircles.add(new Trazo(trazo, colorActual, pincel));
        trazo = new Path();
    }

I want to save this "trazo" in local DB to reuse later on a canvas. How can i save it? I think if a save the first point (x,y) and second point (x,y), it only will draw a line between two points. I need to save all the path. I dont want save canvas as image because i want to reuse it later and permit modify it.

Upvotes: 0

Views: 1231

Answers (3)

garciam202
garciam202

Reputation: 631

At the end I save an ArrayList of Points and then I iterate this List to construct the Path.

Upvotes: 0

sohailehmad
sohailehmad

Reputation: 2042

You can marshal you trazo object to json using google Gson.
Gradle dependency:

compile 'com.google.code.gson:gson:2.8.0'

Usage:

Gson gson = new Gson();
String trazoJson = gson.toJson(trazo);

Then you can persist you trazoJson to database and you can easily unmarshal trazoJson to trazo as.

Gson gson = new Gson();
Path trazo = gson.fromJson(trazoJson, Path.class);

Upvotes: 1

Franz Deschler
Franz Deschler

Reputation: 2574

You could try to save the path as String like in svg. https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths

Upvotes: 0

Related Questions