java - How can I draw directional path and detect if traced - Android -
i working on application draws letters on canvas , detect if user traces letter in correct form. tried different techniques , can't seem find best approach cover letters. letters in comic sans font , due formations in f , s pretty difficult. it's lowercase letters
there number of attempts answer needed answer posted here: path measure example
this did accomplish task:
get starting point of path
then created separate path started @ point when user touch close start point (lets call trail path)
finally went on calculate point of path ahead of user trail path , see if difference in touch , expected point close.
here bit of code
path measure pm = new pathmeasure(mypath, false); pm.getpostan(0, floatpoints, null); //this functions says whether offset path great 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 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 selected set path 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 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 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 ok continue trail path if(!ismajoroffset(point.x, x, touch_tolerance) && !ismajoroffset(point.y, y, touch_tolerance)) { //move current path point trailpath.lineto(point.x, point.y); } else { pathstarted = false; toast.maketext(this.getcontext(), "ended", toast.length_short).show(); } }
hope helps someone!
Comments
Post a Comment