【问题标题】:Android: Save the Paint paths to device / sharedpreferencesAndroid:将绘画路径保存到设备/共享首选项
【发布时间】:2015-03-01 13:04:26
【问题描述】:

我正在开发一个绘画应用程序,其中在 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>();

运动事件

   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"); 
       }
    }  

问题:

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

我想问一下如何保存路径以便可以在创建时重绘路径?可以保存到 SharedPreference 吗?

【问题讨论】:

    标签: android canvas path sharedpreferences


    【解决方案1】:

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

    【讨论】:

    • 我对JSON不是很熟悉...我先研究一下JSON是做什么的~thx
    猜你喜欢
    • 1970-01-01
    • 2012-03-29
    • 2014-01-11
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多