无法在Android的onTouch事件中获取指针的最后位置

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

我正在尝试在Android的onTouch事件上获取指针的最后位置。我的对象是尝试在第一个点和最后一个点之间绘制一条线,例如,我可以跟踪和绘制指针的运动。但是,我不能画一条线。我尝试在ACTION_POINTER_UP中获取Pointer的位置,但是,我看到Pointer的位置是相同的。我在不同类型的论坛上阅读了一些解决方案,并看到了建议,以跟踪ACTION_MOVE中的位置,然后采取最后保存的位置。看起来很逻辑,我以这种方式尝试过,但始终不变,我可以绘制Pointer的运动,但无法在特定时刻跟踪位置。看来极其诡异。任何人都可以帮助我,也许我在代码中做了一些我不认识的愚蠢的事情。这是我的代码

public boolean onTouchEvent(MotionEvent event){
    //get Pointer index from the event object
    int pointerIndex = event.getActionIndex();
    //get Pointer ID
    int pointerID = event.getPointerId(pointerIndex);
    //get masked (not specific to a pointer) action
    int maskedAction = event.getActionMasked();

    switch(maskedAction){
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:{
            //We have new pointer. Let add it to the list of Pointers
            PointF f = new PointF();
            f.x = event.getX(pointerIndex);
            f.y = event.getY(pointerIndex);
            Path path_cur = paths.get(pointerID);
            if (path_cur==null) path_cur= new Path();
            path_cur.moveTo(f.x,f.y);
            mActivePointers.put(pointerID,f);
            paths.put(pointerID,path_cur);
            break;
        }
        case MotionEvent.ACTION_MOVE:{
            //a Pointer was moved
            for (int size = event.getPointerCount(),i =0;i<size;i++){
                PointF point = mActivePointers.get(event.getPointerId(i));
                Path path_cur = paths.get(event.getPointerId(i));
                if (point!=null){
                    PointF pointTemp = new PointF();
                    pointTemp.x = event.getX(i);
                    pointTemp.y = event.getY(i);
                    path_cur.lineTo(pointTemp.x,pointTemp.y);
                    testPointers.put(event.getPointerId(i),pointTemp);
                    Log.d("teoleo","Come here already");
                }
            }
            break;
        }
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:{
            PointF pointDown = new PointF();
            pointDown.x = event.getX(pointerIndex);
            pointDown.y = event.getY(pointerIndex);
            PointF pointUp = testPointers.get(pointerID);
            Toast.makeText(this.getContext(),"First coordinate is "+ pointUp.x +" and" + pointUp.y + "second one is "
                    +pointDown.x+"and "+pointDown.y,Toast.LENGTH_LONG).show();
            ArrayList <PointF> tempList = new ArrayList<>();
            tempList.add(pointDown);
            tempList.add(pointUp);
            listDrawLine.add(tempList);
            mActivePointers.remove(pointerID);
            break;
        }
        case MotionEvent.ACTION_CANCEL:{
            mActivePointers.remove(pointerID);
            break;
        }
    }
    invalidate();
    return true;
}

这是onTouchEvent方法。这是onDraw()方法

protected void onDraw(Canvas canvas){
    super.onDraw(canvas);

    //draw all pointers
    for (int size =mActivePointers.size(),i=0;i<size;i++){
        PointF point =mActivePointers.valueAt(i);
        if (point!=null) {
            mPaint.setColor(colors[i % 9]);
            canvas.drawCircle(point.x, point.y, SIZE, mPaint);
        }
    }
    for (int size = paths.size(),i=0;i<size;i++){
        Path path = paths.valueAt(i);
        if (path!=null){
            mPaint.setColor(colors[i % 9]);
            canvas.drawPath(path,mPaint);
        }
    }
    for (ArrayList<PointF> list:listDrawLine){
        PointF pointDown = list.get(0);
        PointF pointUp = list.get(1);
        //Toast.makeText(this.getContext(),"trong day la "+ pointUp.x +" va" + pointUp.y + "them diem "
        //        +pointDown.x+"va "+pointDown.y,Toast.LENGTH_LONG).show();
        Paint gPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        gPaint.setColor(Color.RED);
        gPaint.setStyle(Paint.Style.STROKE);
        canvas.drawLine(pointDown.x,pointDown.y,pointUp.x,pointUp.y,gPaint);

    }
    canvas.drawText("Total Pointers: "+mActivePointers.size(),10,40,textPaint);
}
android pointers position ontouch
1个回答
0
投票

我已经找到了问题的答案。我在尝试获取最后一个指针的位置时犯了一个错误。答案是event.get(pointerId)返回指针的最后位置。

© www.soinside.com 2019 - 2024. All rights reserved.