【问题标题】:How can I draw directional path and detect if traced - Android如何绘制方向路径并检测是否被跟踪 - Android
【发布时间】:2016-01-31 11:29:48
【问题描述】:

我正在开发一个在画布上绘制字母并检测用户是否以正确形式跟踪字母的应用程序。我尝试了不同的技术,但似乎找不到覆盖所有字母的最佳方法。字母是comic sans字体,由于f和s的格式,这很困难。是小写字母

【问题讨论】:

  • 这里描述了如何使用手势来完成。也许您会想使用他们宣传的解决方案:gesturekit.com/…
  • 谢谢,但手势无法工作,仅仅是因为手势无法根据路径进行测量。这就是我想要完成的目标

标签: java android


【解决方案1】:

由于发布在此处的答案,我进行了多次尝试以最终得到我需要的答案:Path Measure example

这是我为完成任务所做的:

  1. 获取路径的起点

  2. 然后我创建了一个单独的路径,该路径在用户触摸接近起点时开始(我们称之为轨迹路径)

  3. 最后,我继续计算稍微领先于用户轨迹路径的路径点,看看触摸点和预期点的差异是否相当接近。

这是一段代码

    Path Measure pm = new PathMeasure(myPath, false);
    pm.getPosTan(0, floatPoints, null);

    //This functions says whether the offset from path was too great to accept
    private boolean isMajorOffset(float point1, float point2, int tolerance){
                float difference = Math.abs(point1 - point2);
                return tolerance < Math.floor(difference) && tolerance < Math.ceil(difference);
            }

private void touch_start(float x, float y){
        //get point as float
        float floatPoints[] = {0f, 0f};
        //get current start point
        pm.getPosTan(0, floatPoints, null);

        Point startPoint = new Point((int)floatPoints[0], (int)floatPoints[1]);

        //if startPoint is selected then set path as started
        if((startPoint.x >= x - TOUCH_TOLERANCE && startPoint.x <= x + TOUCH_TOLERANCE)
                && (startPoint.y >= y - TOUCH_TOLERANCE && startPoint.y <= y + TOUCH_TOLERANCE))
        {
            PathStarted = true;
            Toast.makeText(this.getContext(), "Started", Toast.LENGTH_SHORT).show();

            //move trail path to this point
            trailPath.moveTo(startPoint.x, startPoint.y);
        }
    }

private void touch_move(float x, float y){
        if(PathStarted==false){
            return;
        }
        //get lenght of trail path
        PathMeasure tm = new PathMeasure(trailPath, false);

        //get point as float
        float floatPoints[] = {0f, 0f};
        //get current start point
        pm.getPosTan(tm.getLength() + 1, floatPoints, null);

        Point point = new Point((int)floatPoints[0], (int)floatPoints[1]);

        //if offset is ok continue with trail path
        if(!isMajorOffset(point.x, x, TOUCH_TOLERANCE) && !isMajorOffset(point.y, y, TOUCH_TOLERANCE))
        {
            //move current path to this point
            trailPath.lineTo(point.x, point.y);
        }
        else {
            PathStarted = false;
            Toast.makeText(this.getContext(), "Ended", Toast.LENGTH_SHORT).show();

        }
    }

希望这对某人有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多