将android.graphics.Path转换为String并返回Path

时间:2015-06-15 09:23:43

标签: android string path

所以我想将路径转换为字符串,现在在Sharedpreferences中保存在下一个活动中我得到了这个字符串,但我想再次将此字符串转换为Path。帮助我,我已经看过谷歌,但无法找到任何有用的东西

Class A

final Path currentPath = mCurrentPath;
        System.out.println("current path" + currentPath);
        SharedPreferences ap = ctx.getSharedPreferences("lastplayed",
                ctx.MODE_PRIVATE);
        Editor a = ap.edit();
        a.putString("pattern", currentPath + "");
        a.commit();

Class B

SharedPreferences ap = ctx.getSharedPreferences("lastplayed",
                ctx.MODE_PRIVATE);
String path=ap.getString("pattern","0");

现在我想将此字符串对象再次转换为路径。在此先感谢,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以按如下方式创建CustomPath类,并将其发送到下一个Activity。

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

}
}

答案 1 :(得分:-1)

您可以使用

Uri uri = Uri.parse(pathString);