Android的,保存路径数据库 [英] Android, save Path to database

查看:106
本文介绍了Android的,保存路径数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

私人路径的mpath =新路径();

我要保存路径(的mpath ),以数据库为未来的读取和重绘。

I want to save path(mPath) to database for future read and redraw.

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

Is there any way to separate path to x,y and then collect to Path, or any other method?

感谢

推荐答案

请序列化的自定义路径类,并保存到数据库

Make serializable custom path class and save that to database

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

    }
}

这篇关于Android的,保存路径数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆