Android:将Paint路径保存到device / sharedpreferences

时间:2015-01-03 12:49:32

标签: android canvas path sharedpreferences

我正在制作一个绘画应用程序,在doodleView中,绘制的路径及其各自的颜色和宽度存储如下:

变量

private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private Map<Path, Integer> colorsMap = new HashMap<Path, Integer>();
private Map<Path, Integer> widthMap = new HashMap<Path, Integer>();
private Map<Path, Integer> styleMap = new HashMap<Path, Integer>();

MotionEvent

   private void touch_start(float x, float y) 
   {
       undonePaths.clear();
       mPath.reset();
       mPath.moveTo(x, y);
       mX = x;
       mY = y;
   }

   private void touch_move(float x, float y) 
   {
       float dx = Math.abs(x - mX);
       float dy = Math.abs(y - mY);
       if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) 
       {
           mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
           mX = x;
           mY = y;

           startdrawing = true;
       }
   }

   private void touch_up() 
   {
       mPath.lineTo(mX, mY);      
       mCanvas.drawPath(mPath, Utilities.mPaint); 
       paths.add(mPath);
       colorsMap.put(mPath,selectedColor);
       widthMap.put(mPath,selectedWidth);
       styleMap.put(mPath, selectedStyle);
       mPath = new Path(); 
   }

   public void onClickUndo() 
   { 
       if (paths.size()>0) 
        { 
           undonePaths.add(paths.remove(paths.size()-1));
           invalidate();
        }      
       else 
       {
           String xx = getContext().getString(R.string.toast_nothing_to_undo);
           Activity activity = (Activity) context_new;
           Utilities.custom_toast(activity, ""+ xx, "gone!", "Short"); 
       }
    }

   public void onClickRedo()
   {
       if (undonePaths.size()>0) 
       { 
           paths.add(undonePaths.remove(undonePaths.size()-1)); 
           invalidate();
       } 
       else 
       {
           String xx = getContext().getString(R.string.toast_nothing_to_redo);
           Activity activity = (Activity) context_new;
           Utilities.custom_toast(activity, ""+ xx, "gone!", "Short"); 
       }
    }  

问题:

以上代码运作良好。但是,从主页按钮/通知栏返回,有时会从内存中释放路径。

我想问一下如何保存路径以便路径可以重新创建onCreate?是否可以保存到SharedPreference?

1 个答案:

答案 0 :(得分:0)

我认为你应该为你的路径变量做一个Pojo类,然后jsonify这个类。并将此json保存到Sharedpref,就是这样!!