Android,保存数据库的路径

时间:2012-08-09 20:55:28

标签: android

我有private Path mPath = new Path();

我想将路径(mPath)保存到数据库以供将来阅读和重绘。

有没有办法将路径分隔为x,y然后收集到Path或任何其他方法?

由于

1 个答案:

答案 0 :(得分:5)

制作可序列化的自定义路径类并将其保存到数据库

public class CustomPath extends Path implements Serializable {

    private static final long serialVersionUID = -5974912367682897467L;

    private ArrayList <PathAction> actions = new ArrayList <CustomPath.PathAction> ();

    private void readObject(ObjectInputStream in ) throws IOException, ClassNotFoundException { in .defaultReadObject();
        drawThisPath();
    }

    @Override
    public void moveTo(float x, float y) {
        actions.add(new ActionMove(x, y));
        super.moveTo(x, y);
    }

    @Override
    public void lineTo(float x, float y) {
        actions.add(new ActionLine(x, y));
        super.lineTo(x, y);
    }

    private void drawThisPath() {
        for (PathAction p: actions) {
            if (p.getType().equals(PathActionType.MOVE_TO)) {
                super.moveTo(p.getX(), p.getY());
            } else if (p.getType().equals(PathActionType.LINE_TO)) {
                super.lineTo(p.getX(), p.getY());
            }
        }
    }

    public interface PathAction {
        public enum PathActionType {
            LINE_TO, MOVE_TO
        };
        public PathActionType getType();
        public float getX();
        public float getY();
    }

    public class ActionMove implements PathAction, Serializable {
        private static final long serialVersionUID = -7198142191254133295L;

        private float x, y;

        public ActionMove(float x, float y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public PathActionType getType() {
            return PathActionType.MOVE_TO;
        }

        @Override
        public float getX() {
            return x;
        }

        @Override
        public float getY() {
            return y;
        }

    }

    public class ActionLine implements PathAction, Serializable {
        private static final long serialVersionUID = 8307137961494172589L;

        private float x, y;

        public ActionLine(float x, float y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public PathActionType getType() {
            return PathActionType.LINE_TO;
        }

        @Override
        public float getX() {
            return x;
        }

        @Override
        public float getY() {
            return y;
        }

    }
}