如何序列化android.graphics.Path的对象

如何序列化android.graphics.Path的对象,android,serialization,memory,path,internal,Android,Serialization,Memory,Path,Internal,我正在尝试将Android.graphics.Path的对象存储在内部设备内存中。有人知道如何序列化android.graphics.Path对象吗?还有,还有其他存储路径对象的方法吗?谢谢 对于Path类没有什么特别的。Use可以序列化实现可序列化接口的任何类 可序列化接口是一个标记接口。i、 它没有任何方法来实现。它只是表示该对象可以压缩为文件/内存,然后可以再次压缩为活动对象 您所做的只是创建一个扩展了android.graphics.Path类并实现可序列化接口的类。然后使用该类而不是您

我正在尝试将Android.graphics.Path的对象存储在内部设备内存中。有人知道如何序列化android.graphics.Path对象吗?还有,还有其他存储路径对象的方法吗?谢谢

对于Path类没有什么特别的。Use可以序列化实现可序列化接口的任何类

可序列化接口是一个标记接口。i、 它没有任何方法来实现。它只是表示该对象可以压缩为文件/内存,然后可以再次压缩为活动对象 您所做的只是创建一个扩展了android.graphics.Path类并实现可序列化接口的类。然后使用该类而不是您的android.graphics.Path。然后您将能够对其应用序列化

并查看以下链接以获取详细信息


希望这有帮助:)

我刚刚设法解决了这个问题。我的应用程序基于FingerPaintDemo,因此只使用moveTo和quadTo,但我认为您可以将此方法应用于任何路径函数

首先,按如下方式扩展路径:

import android.graphics.Path;

import java.util.ArrayList;

import java.io.Serializable;

public class SerializablePath extends Path implements Serializable {

    private ArrayList<float[]> pathPoints;

    public SerializablePath() {
        super();
        pathPoints = new ArrayList<float[]>();
    }

    public SerializablePath(SerializablePath p) {
        super(p);
        pathPoints = p.pathPoints;
    }

    public void addPathPoints(float[] points) {
        this.pathPoints.add(points);
    }

    public void loadPathPointsAsQuadTo() {
        float[] initPoints = pathPoints.remove(0);
        this.moveTo(initPoints[0], initPoints[1]);
        for (float[] pointSet : pathPoints) {
            this.quadTo(pointSet[0], pointSet[1], pointSet[2], pointSet[3]);
        } 
    }
}
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.graphics.Path;
导入java.util.ArrayList;
导入java.io.Serializable;
公共类SerializablePath扩展路径实现可序列化{
私有ArrayList路径点;
公共序列化路径(){
超级();
路径点=新的ArrayList();
}
公共SerializablePath(SerializablePath p){
超级(p);
路径点=p.pathPoints;
}
公共void addPathPoints(浮动[]点){
this.pathPoints.add(点);
}
public void loadPathPointsSquadTo(){
float[]initPoints=pathPoints.remove(0);
this.moveTo(initPoints[0],initPoints[1]);
for(float[]点集:路径点){
this.quadTo(点集[0]、点集[1]、点集[2]、点集[3]);
} 
}
}
我不认为我需要粘贴实现代码,但是如果您想看到它,请告诉我。基本上就像调用myPath.quadTo(x1,y1,x2,y2)一样,也可以调用myPath.addPathPoints(newfloat[]{x1,y1,x2,y2})


正常地将对象序列化到磁盘,当您读回它时,只需确保调用myPath.loadPathPointsAsQuadTo()。

我这样做的方式是从原始Path类中标识我需要的方法,然后简单地重写这些方法,如下所示:

import android.graphics.Path;

import java.util.ArrayList;

import java.io.Serializable;

public class SerializablePath extends Path implements Serializable {

    private ArrayList<float[]> pathPoints;

    public SerializablePath() {
        super();
        pathPoints = new ArrayList<float[]>();
    }

    public SerializablePath(SerializablePath p) {
        super(p);
        pathPoints = p.pathPoints;
    }

    public void addPathPoints(float[] points) {
        this.pathPoints.add(points);
    }

    public void loadPathPointsAsQuadTo() {
        float[] initPoints = pathPoints.remove(0);
        this.moveTo(initPoints[0], initPoints[1]);
        for (float[] pointSet : pathPoints) {
            this.quadTo(pointSet[0], pointSet[1], pointSet[2], pointSet[3]);
        } 
    }
}
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;
    }

}
}
公共类CustomPath扩展路径实现可序列化{
私有静态最终长serialVersionUID=-5974912367682897467L;
私有ArrayList操作=新建ArrayList();
私有void readObject(ObjectInputStream in)引发IOException、ClassNotFoundException{
in.defaultReadObject();
drawThisPath();
}
@凌驾
公共无效移动到(浮动x、浮动y){
添加(新的ActionMove(x,y));
super.moveTo(x,y);
}
@凌驾
公用空心线(浮动x、浮动y){
增加(新的行动线(x,y));
super.lineTo(x,y);
}
私有void drawThisPath(){
for(路径操作p:操作){
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());
}
}
}
公共接口路径操作{
公共枚举路径操作类型{LINE_TO,MOVE_TO};
公共路径ActionType getType();
公共浮点getX();
公共浮球(球);
}
公共类ActionMove实现PathAction,可序列化{
私有静态最终长serialVersionUID=-7198142191254133295L;
私有浮动x,y;
公共动作移动(浮动x,浮动y){
这个.x=x;
这个。y=y;
}
@凌驾
公共路径ActionType getType(){
返回PathActionType.MOVE\u TO;
}
@凌驾
公共浮点getX(){
返回x;
}
@凌驾
公共浮球{
返回y;
}
}
公共类ActionLine实现PathAction,可序列化{
私有静态最终长serialVersionUID=8307137961494172589L;
私有浮动x,y;
公共行动线(浮动x,浮动y){
这个.x=x;
这个。y=y;
}
@凌驾
公共路径ActionType getType(){
将PathActionType.LINE\u返回到;
}
@凌驾
公共浮点getX(){
返回x;
}
@凌驾
公共浮球{
返回y;
}
}
}

在我的示例中,我需要“moveTo”和“lineTo”,因此在本例中,我只需将图形信息保存在列表中。此列表包含Path所需的图形信息(我称之为“操作”),以便将图形恢复为序列化对象之前的样子。然后,当我反序列化CustomPath类型的对象时,我确保让调用“drawThisPath”,因此路径被重新绘制。

反序列化后,路径对象似乎为空,例如,没有任何直线和曲线。我认为,路径对象是直线、矩形、圆弧对象的容器,它们也应该是可序列化的。如果我是对的,路径序列化没有意义。。。请大家纠正我:)不幸的是,Javanator的方法不起作用,代码将序列化,但在读回时路径为空。当我陷入完全相同的问题时,我真的可以使用一些其他的想法。这是不可能的:Path只是Skia中本机路径的包装器,无法提取。我已经有一段时间没有看到这一点了,但看看这是否有帮助:@KazekageGaara brk3已经提到了这一点,您需要调用myPath.LoadPathPointsSquadto()还原反序列化路径。好方法。我只是美化了你的代码。我在这里的变体是,当我在文件中保存自定义路径时,kill app然后open it Path变为空。路径不为空,但操作为空,我无法重新绘制它。