【问题标题】:Android Implement onTouchListener on path objectsAndroid 在路径对象上实现 onTouchListener
【发布时间】:2014-01-17 11:10:06
【问题描述】:

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

我只知道在位图上实现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】:

路径对象上没有 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;
}

【讨论】:

  • 您好,谢谢您的回复。我已经对代码进行了修改,并在上面进行了更新。这是正确的吗?它似乎不起作用..请指教。此外,这是否适用于其他形状,如圆形或重叠圆形?
  • ypu 必须在路径的arrayList 上应用循环。这就是我所说的在修饰事件中循环执行的意思。
  • 我已经对上面的代码进行了更正。这是正确的吗?我选择使用 arraylist 来存储边界坐标,因为除了矩形之外我还有其他形状。但是,它仍然不起作用。请指正并纠正我,谢谢
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-24
  • 2013-08-26
  • 2011-09-27
  • 2011-09-03
相关资源
最近更新 更多