Android Implement onTouchListener on path objects

前端 未结 1 621
说谎
说谎 2020-12-17 06:30

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 p

相关标签:
1条回答
  • 2020-12-17 07:03

    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 bounds
                for (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;
            }
        }
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题