如何在json中保存路径

时间:2016-06-18 08:17:30

标签: java android eclipse

现在我使用canvas.drawPath(路径路径,Paint paint)绘制路径 这是由用户使用指定的绘制创建的。 在关闭这个应用程序(如PhotoShop应用程序)之前,我想保存它 路径和油漆。下一次。所以,用户可以从他们的开始 上一个画布(屏幕)。

有人说他们应该转换为bytearray []并使用 FileOutputStream将其写入文件。 但我不知道该怎么做。

请帮帮我。我真的需要你的建议。 非常感谢。

----------------------划分2016 6 22 18:06:13 ---------------- -------

/.../

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getPressure() < 1) { /* 只能笔画 */
        if (!isShowing()&&touchable) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    LogUtils.d("Paint ACTION_DOWN");
                    Touch_Down(x, y);
                    invalidate();
                    break;

                case MotionEvent.ACTION_MOVE:
                    LogUtils.d("Paint ACTION_MOVE");
                    Touch_Move(x, y);
                    invalidate();
                    break;

                case MotionEvent.ACTION_UP:
                    LogUtils.d("Paint ACTION_UP");
                    if (IsPaint) {
                        Touch_Up(mPaint);
                    } else {
                        Touch_Up(mErase);
                    }
                    invalidate();
                    break;
            }
        }
        return true;
    } else {
        return super.onTouchEvent(event);
    }
}

private void Touch_Down(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
    if (IsRecordPath && listener != null) {
        listener.addNodeToPath(x, y, MotionEvent.ACTION_DOWN, IsPaint);
    }
}


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;
        if (IsRecordPath && listener != null) {
            listener.addNodeToPath(x, y, MotionEvent.ACTION_MOVE, IsPaint);
        }
    }
}

private void Touch_Up(Paint paint) {
    mPath.lineTo(mX, mY);
    mCanvas.drawPath(mPath, paint);
    mPath.reset();
    if (IsRecordPath && listener != null) {
        listener.addNodeToPath(mX, mY, MotionEvent.ACTION_UP, IsPaint);
    }
}

public void preview(ArrayList<Node> arrayList) {
    touchable = true;
    IsRecordPath = false;
    PreviewThread previewThread = new PreviewThread(this, arrayList);
    Thread thread = new Thread(previewThread);
    thread.start();
}

private Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case INDIVIDE:
                ((View) msg.obj).invalidate();
                break;
            case CHOOSEPATH:
                JsonToPathNode(msg.obj.toString());
                break;
        }
        super.handleMessage(msg);
    }

};

 class PreviewThread implements Runnable {
    private long            time;
    private ArrayList<Node> nodes;
    private View            view;

    public PreviewThread(View view, ArrayList<Node> arrayList) {
        this.view = view;
        this.nodes = arrayList;
    }

    public void run() {
        time = 0;
        IsShowing = true;
        clean();
        if (mBitmapInit != null) {
            drawBitmapToCanvas(mBitmapInit);
        }
        if (nodes!=null) {
            for (int i = 0; i < nodes.size(); i++) {
                Node node = nodes.get(i);
                float x = dip2px(node.x);
                float y = dip2px(node.y);
            /*Log.e("pre" + x, "pre" + y);
            if (i < nodes.size() - 1) {
                time = nodes.get(i + 1).time - node.time;
            }*/
                IsPaint = node.IsPaint;
                if (node.IsPaint) {
                    //UserInfo.PaintColor = node.PenColor;
                    //UserInfo.PaintWidth = node.PenWidth;
                    initPaint(2);
                    //Init_Paint(node.PenColor, node.PenWidth);
                } else {
                    //UserInfo.EraserWidth = node.EraserWidth;
                    initErase();
                    //Init_Eraser(node.EraserWidth);
                }
                switch (node.TouchEvent) {
                    case MotionEvent.ACTION_DOWN:
                        Touch_Down(x, y);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Touch_Move(x, y);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (node.IsPaint) {
                            Touch_Up(mPaint);
                        } else {
                            Touch_Up(mErase);
                        }
                        break;
                }
                Message msg = new Message();
                msg.obj = view;
                msg.what = INDIVIDE;
                handler.sendMessage(msg);
                if (!ReDoOrUnDoFlag) {
                    try {
                        //Thread.sleep(time);
                        Thread.sleep(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        ReDoOrUnDoFlag = false;
        IsShowing = false;
        IsRecordPath = true;
    }
}

/.../

1 个答案:

答案 0 :(得分:0)

已经有很多答案可以告诉你如何做到这一点。第一个出现在Google上的是 Saving and Reading Bitmaps/Images from Internal memory in Android