Android在路径对象上实现onTouchListener

问题描述 投票:2回答:1

我创建了一个如下所示的路径对象,它绘制了不同的形状。不同的按钮响应在画布上绘制不同的形状。我想移动我在画布中创建的路径对象,但我不知道如何。

我只知道在位图上而不是在路径对象上实现ontouchlistener的方法。

我的代码如下:

ArrayList<Path> paths = new ArrayList<Path>();
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);

                paintColor.setColor(Color.RED);
                paintColor.setStrokeWidth(2);
                paintColor.setStyle(Paint.Style.STROKE);


    if (MainActivity.isRectangle) {

                Path path = new Path();
                path.moveTo(1, 1);
                path.lineTo(90, 1);
                path.lineTo(90, 60);
                path.lineTo(1, 60);

                path.close();



               paths.add(path);

            }

 for (Path p : paths) {
            canvas.drawPath(p, paintColor );
        }


     canvas.drawPath(path, paintColor);
     invalidate();


    }

//mainActivity
rectbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // TODO Auto-generated method stub
                isTriangle = false;
                isRectangle = true;
                isCircle= false;
                isParallelogram = false;
                isTapezium = false;


                dv.invalidate();


            }// onclick

        });

的onTouchEvent

@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();
              for (Path p : paths) {

            path.moveTo(x,y);
            }

        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;

}

请指教。谢谢。

android path android-canvas ontouchlistener
1个回答
4
投票

路径对象上没有onTouchListener。但是,请按照步骤实现此功能

1.)覆盖onTouchEvent()方法以查找您触摸的坐标。此链接可能有所帮助。 http://developer.android.com/training/graphics/opengl/touch.html

2.)创建一个RectF boundsRect,并为每个路径对象存储其边界坐标

Path.getBounds(boundsRect);     

方法并同时检查触摸坐标是否位于上述方法的循环中的rectF boundsRect(使用bounds.contains(x,y))。

3.)选择该路径并立即对其进行所需操作。

编辑代码

@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;
}
© www.soinside.com 2019 - 2024. All rights reserved.