1

I am drawing a smooth line over a customImageView and wanted to save the path in shared preferences so that I can redraw the same path whenever required.

Method for drawing smooth line :

protected Path mPath;
protected Paint mPaint;
protected Paint mPaintFinal;
protected Bitmap mBitmap;
protected Canvas mCanvas;
private SharedPreferences  mPreferences;

private void onTouchEventSmoothLine(MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            isDrawing = true;
            mStartX = mx;
            mStartY = my;

            mPath.reset();
            mPath.moveTo(mx, my);
            Log.i("::ACTION_DOWN::", mStartX +" // " +mStartY);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:

            float dx = Math.abs(mx - mStartX);
            float dy = Math.abs(my - mStartY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mStartX, mStartY, (mx + mStartX) / 2, (my + mStartY) / 2);
                mStartX = mx;
                mStartY = my;
            }
            mCanvas.drawPath(mPath, mPaint);
            Log.i("::ACTION_MOVE::", mStartX +" // " +mStartY);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            isDrawing = false;
            mPath.lineTo(mStartX, mStartY);
            Log.i("::ACTION_UP::", mStartX +" // " +mStartY);
            mCanvas.drawPath(mPath, mPaintFinal);
            saveCustomPath(mPath);
            mPath.reset();
            invalidate();
            break;
    }
}

Method for saving path:

public void saveCustomPath(Path mPath){
    SharedPreferences.Editor prefsEditor = mPreferences.edit();

    Gson gson = new Gson();
    String json = gson.toJson(mPath);
    prefsEditor.putString("MyPath", json);
    prefsEditor.commit();
}

Method for retrieving path:

public Path retrieveCustomPath(){
    if(mPreferences != null) {
        Gson gson = new Gson();
        String json = mPreferences.getString("MyPath", "");
        Path path = gson.fromJson(json, Path.class);
        return path;
    }
    return null;
} 

While, Path object can not be saved/retrieved directly to/from disk/shared prefernces without serializing/deserializing, I am unable to implement it as suggested here, any help would be highly appreciated.

Atul Kaushik
  • 5,181
  • 3
  • 29
  • 36
  • dont convert the path to json simply save path as it is in one string .and save path as string and retrive it from your preferences. – Bhagyashri Mar 12 '18 at 10:04
  • @pskink because I almost never used java serialization, instead always went ahead with parcelable. Here, I am using MoveTo, LineTo QuadTo and Reset methods of android.graphics.Path, I have little idea of QuadTo but almost none for Reset. How you will call this extended CustomPath methods to get what I am getting using android.graphics.Path api – Atul Kaushik Mar 12 '18 at 15:59
  • @userI Really liked your idea, if I don't get any solution for CustomPath then will go with your suggestion..thanks – Atul Kaushik Mar 12 '18 at 16:01
  • @AtulKaushik did it work? :D – Amin Keshavarzian Feb 08 '21 at 21:07

0 Answers0