1

我正在 android canvas 中绘制路径。这工作正常。现在我想检测何时在屏幕上做出滑动手势是否是在绘制的路径上做出的。我正在使用手势监听器来检测 FlingMovement。

private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 250;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) 
        {
            try 
            {
                if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH){
                    return false;
                }

                if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
                {
                    //Here find whether the swipe occured on any of the paths drawn in canvas
                }
            }
            catch(Exception e)
            {

            }
            return true;
        }
}

在 FlingMovement 中,我将获得滑动的起点和终点。然后我需要检查这些点形成的线是否与画布中绘制的任何路径相交。我怎样才能做到这一点?

4

2 回答 2

0

试试这个方法

Path pp; // your path variable which is drawn on canvas

        RectF rectPathBounds=new RectF();
        pp.computeBounds(rectPathBounds,true);

        if(rectPathBounds.contains((int) event.getX(), (int) event.getY())){
            //your action
        }
于 2013-09-10T11:16:02.797 回答
-1

试试这个代码,它检测一条线是否与路径相交,

import android.graphics.Point;
import java.util.List;

static Boolean isPathComplex(List<Point> path) {

    if (path == null || path.size() <= 2) {
        return false;
    }

    int len = path.size();  

   for (int i = 1; i < len; i++) {
        Point lineAStart = path.get(i - 1);
        Point lineAEnd = path.get(i);

        for (int j = i + 1; j < len; j++) {
            Point lineBStart = path.get(j - 1);
            Point lineBEnd = path.get(j);
            if (lineSegmentsIntersect(lineAStart, lineAEnd, lineBStart, lineBEnd)) {
                return true;
            }

        } // inner loop

   } // outer loop

}

static Boolean lineSegmentsIntersect(Point aInitial, Point aFinal, Point bInitial, Point bFinal) {
    // left as an exercise to the reader
}
于 2013-09-10T11:10:02.450 回答