安卓:如何镜像你的触摸?(视觉指针问题)



我的平板电脑指针有问题。我正在设计一个艺术应用程序,我正试图找出如何反映我的动作(对称)。目前,如果你用手指或手写笔触摸屏幕,就会绘制一个图像(程序生成的圆圈),并将其自身更新到你移动手指/手写笔的任何位置。这非常有效。但现在我想添加两个新功能:水平对称和垂直对称。因此,如果选择水平对称,另一个指针将出现在相反的X位置,但沿相同的Y轴。因此,如果你触摸300x,250y,另一个指针将与原始指针一起显示在屏幕上的-300x,250y处。现在,当我尝试应用和测试它时,我仍然只得到原始指针。以下是我目前所拥有的:

rLO.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:               
            // Hardware accelerated at runtime
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            cursor.x = (int)event.getX() - (cursor.radius / 2);
            cursor.y = (int)event.getY() - (cursor.radius / 2);
            cursor.onDraw(cursor.e);
            rLO.addView(cursor);
            if (hSym > 0) {
                vSym = 0;
                cursorH.x = -cursor.x;
                cursorH.y = cursor.y;
                cursorH.onDraw(cursorH.e);
                rLO.addView(cursorH);
            }
            if (vSym > 0) {
                hSym = 0;
                cursorV.x = cursor.x;
                cursorV.y = -cursor.y;
                cursorV.onDraw(cursorV.e);
                rLO.addView(cursorV);
            }
            break;
        case MotionEvent.ACTION_MOVE:
            // Hardware accelerated at runtime
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            //update pointer to new position of stylus/finger
            cursor.x = (int)event.getRawX() - (cursor.radius / 2);
            cursor.y = (int)event.getRawY() - (cursor.radius / 2);
            rLO.removeView(cursor);
            rLO.addView(cursor);
            if (hSym > 0) {
                cursorH.x = -cursor.x;
                cursorH.y = cursor.y;
                rLO.removeView(cursorH);
                rLO.addView(cursorH);
            }
            if (vSym > 0) {
                cursorV.x = cursor.x;
                cursorV.y = -cursor.y;
                rLO.removeView(cursorV);
                rLO.addView(cursorV);
            }
            break;
        case MotionEvent.ACTION_UP:
            if (vSym > 0 || hSym > 0) {
                rLO.removeView(cursorH);
                rLO.removeView(cursorV);
            }
            rLO.removeView(cursor);
            break;
        } // end switch 

注意:我在代码中使用了"cursor"这个词来表示"pointer"。指针(cursor)是从另一个创建圆的类中以编程方式绘制的。

它可能工作,但问题是屏幕的左上角是(0,0)。任何负位置都不在屏幕上。相反,将屏幕的宽度或高度分成两半。

最新更新