Skip to content Skip to sidebar Skip to footer

Android Implement Ontouchlistener On Path Objects

I have created a path objects as shown below which draws a different shapes.Different buttons response to drawing different shapes on the canvas. I would like to shift the path obj

Solution 1:

There is no onTouchListener on path object.However, Follow the steps to achive this functionality

1.) override onTouchEvent() method to Find the coordinates you have touched. This link may help. http://developer.android.com/training/graphics/opengl/touch.html

2.) create a RectF boundsRect and for each path object store its boundary coordinates by using

Path.getBounds(boundsRect);     

method and simultaneously check whether the touch coordinates lie in the rectF boundsRect(using bounds.contains(x,y)) in a loop in the above method.

3.)select that path and do desired operation on it now.

Edited Code

@Override
public boolean onTouch(View v,MotionEvent event){

    switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:

            //screen touch get x of the touch event
            x = event.getX();
            //screen touch get y of the touch event
            y = event.getY();

            //check if touch point intersects the path boundsfor (Path p : paths) {
                RectF pBounds = new RectF();
                p.computeBounds(pBounds,true);
                if(pBounds.contains(x,y)){
                //select path
                selected Path = p;// where selectedPath is assumed declared.break;
            }

        dv.invalidate();    
        break;

        case MotionEvent.ACTION_UP:
            //screen touch get x of the touch event
            x = event.getX();
            //screen touch get y of the touch event
            y = event.getY();
            break;

        case MotionEvent.ACTION_MOVE:
            //screen touch get x of the touch event
            x = event.getX();
            //screen touch get y of the touch event
            y = event.getY();
            break;
        }
    }
    returntrue;
}

Post a Comment for "Android Implement Ontouchlistener On Path Objects"