【问题标题】:Convert android.graphics.Path to String And Back to Path将 android.graphics.Path 转换为字符串并返回到路径
【发布时间】:2015-08-30 17:43:39
【问题描述】:

所以我想将路径转换为保存在Sharedpreferences 中的字符串,现在在下一个活动中我正在获取此字符串,但我想再次将此字符串转换为路径。帮帮我,我在谷歌上看过,但找不到任何有用的东西

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");

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

【问题讨论】:

  • 创建应该是可串行化的自定义路径,并通过意图将此自定义路径发送到下一个活动。
  • 你能举个例子吗
  • 我的答案是否有效?
  • 我不知道如何实现它.. 你能做到吗?我会感谢的
  • 我没有得到你?

标签: android string path


【解决方案1】:

你可以使用

Uri uri = Uri.parse(pathString);

【讨论】:

  • 你需要知道Path和Uri的区别
【解决方案2】:

如下创建 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;
    }

}
}

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多