如何序列化 android.graphics.Path 的对象 [英] how to serialize an object of android.graphics.Path

查看:23
本文介绍了如何序列化 android.graphics.Path 的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Android.graphics.Path 的对象存储在内部设备内存中.有谁知道如何序列化一个 android.graphics.Path 对象?而且,还有其他方法可以存储 Path 对象吗?谢谢.

I'm trying to store objects of Android.graphics.Path in internal device memory. Does anyone know how to serialize a android.graphics.Path object? And also, is there any other way of storing a Path object? Thanks.

推荐答案

我这样做的方法是从原始 Path 类中识别出我需要的方法,然后简单地覆盖这些方法,如下所示:

The way I did it is to identify the methods that I needed from the original Path class and then simply override those methods as follows:

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

}
}

在我的示例中,我需要moveTo"和lineTo",因此在这种情况下,我只需将绘图信息保存在列表中.此列表包含 Path 所需的绘图信息(我称之为操作"),以便将绘图恢复为对象序列化之前的样子.然后,当我反序列化 CustomPath 类型的对象时,我确保让 默认序列化协议调用drawThisPath",所以路径被重绘.

In my example I need "moveTo" and "lineTo", so in this case I simply hold the drawing information in a list. This list contains the drawing information (what I call "action") needed by Path in order to restore the drawing as it looked like before the object was serialized. Then when I deserialize my objects of type CustomPath i make sure to let the default serialization protocol call "drawThisPath", so the path is redrawn.

这篇关于如何序列化 android.graphics.Path 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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