How can I draw directional path and detect if traced - Android

时间:2015-10-30 23:09:11

标签: java android

I am working on an application that draws letters on a canvas and detect if user traces the letter in the correct form. I tried different techniques and can't seem to find the best approach to cover all letters. Letters are in the comic sans font and due to formations in f and s it is pretty difficult. It's lowercase letters

1 个答案:

答案 0 :(得分:0)

由于此处发布的答案,有许多尝试最终得到我需要的答案:Path Measure example

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

  1. 获取路径的起点

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

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

  4. 这是一些代码

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

    希望这有助于某人!