多点触控 Canvas 旋转 Android

Multitouch Canvas Rotation in Android

我是 Android 的新手,我正在尝试掌握多点触控输入的窍门。我从一个简单的应用程序开始,该应用程序允许用户通过用一根手指拖动和释放来在 Canvas 上创建矩形,我正在使用它。为了扩展这一点,我现在希望用户能够使用第二根手指旋转他们正在绘制的矩形,这就是我的问题开始的地方。就目前而言,添加第二根手指会导致多个矩形旋转,而不仅仅是当前的矩形,但是一旦松开第二根手指,它们就会恢复到默认方向。

我已经研究了一段时间,我认为我的核心问题是我对两个(或更多手指)附带的多个 MotionEvents 处理不当。我留下的记录语句用于在屏幕上显示每个事件的坐标,与触摸屏幕的第一根手指相关联,而不是切换到第二根手指。我尝试了多种访问和更改事件指针 ID 的配置,但仍然没有成功。如果有人能在正确的方向上提供一些指导,我将不胜感激。

我的代码如下:

public class BoxDrawingView extends View {

    private static final String TAG = "BoxDrawingView";
    private static final int INVALID_POINTER_ID = -1;

    private int mActivePointerId = INVALID_POINTER_ID;
    private Box mCurrentBox;
    private List<Box> mBoxen = new ArrayList<>();
    private Float mLastTouchX;
    private Float mLastTouchY;

    ...

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(MotionEventCompat.getActionMasked(event)) {
            case MotionEvent.ACTION_DOWN:
                mActivePointerId = MotionEventCompat.getPointerId(event, 0);
                current = new PointF(MotionEventCompat.getX(event, mActivePointerId),
                    MotionEventCompat.getY(event, mActivePointerId));
                action = "ACTION_DOWN";

                // Reset drawing state
                mCurrentBox = new Box(current);
                mBoxen.add(mCurrentBox);

                mLastTouchX = MotionEventCompat.getX(event, MotionEventCompat.getPointerId(event, 0));
                mLastTouchY = MotionEventCompat.getY(event, MotionEventCompat.getPointerId(event, 0));
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                action = "ACTION_POINTER_DOWN";
                mActivePointerId = MotionEventCompat.getPointerId(event, 0);

                mLastTouchX = MotionEventCompat.getX(event, MotionEventCompat.getPointerId(event, 0));
                mLastTouchY = MotionEventCompat.getY(event, MotionEventCompat.getPointerId(event, 0));
                break;

            case MotionEvent.ACTION_MOVE:
                action = "ACTION_MOVE";
                current = new PointF(MotionEventCompat.getX(event, mActivePointerId),
                    MotionEventCompat.getY(event, mActivePointerId));

                if (mCurrentBox != null) {
                    mCurrentBox.setCurrent(current);
                    invalidate();
                }
                if(MotionEventCompat.getPointerCount(event) > 1) {

                    int pointerIndex = MotionEventCompat.findPointerIndex(event, mActivePointerId);
                    float currX = MotionEventCompat.getX(event, pointerIndex);
                    float currY = MotionEventCompat.getY(event, pointerIndex);


                    if(mLastTouchX < currX) {
                        // simplified: only use x coordinates for rotation for now.  
                        // +X for clockwise, -X for counter clockwise
                        Log.d(TAG, "Clockwise");
                        mRotationAngle = 30;
                    }
                    else if (mLastTouchX > getX()) {
                        Log.d(TAG, "Counter clockwise");
                        mRotationAngle = -30;
                    }
                }
                break;

            case MotionEvent.ACTION_UP:
                action = "ACTION_UP";
                mCurrentBox = null;
                mLastTouchX = null;
                mLastTouchY = null;
                mActivePointerId = INVALID_POINTER_ID;
                break;

            case MotionEvent.ACTION_POINTER_UP:
                action = "ACTION_POINTER_UP";
                int pointerIndex = event.getActionIndex();
                int pointerId = event.getPointerId(pointerIndex);
                if(pointerId == mActivePointerId){
                    mActivePointerId = INVALID_POINTER_ID;
                }
                break;

            case MotionEvent.ACTION_CANCEL:
                action = "ACTION_CANCEL";
                mCurrentBox = null;
                mActivePointerId = INVALID_POINTER_ID;
                break;
        }

        return true;
    }


    @Override
    protected void onDraw(Canvas canvas){
        // Fill the background
        canvas.drawPaint(mBackgroundPaint);

        for(Box box : mBoxen) {
            // Box is a custom object.  Origin is the origin point, 
            // Current is the point of the opposite diagonal corner

            float left = Math.min(box.getOrigin().x, box.getCurrent().x);
            float right = Math.max(box.getOrigin().x, box.getCurrent().x);
            float top = Math.min(box.getOrigin().y, box.getCurrent().y);
            float bottom = Math.max(box.getOrigin().y, box.getCurrent().y);

            if(mRotationAngle != 0) {
                canvas.save();
                canvas.rotate(mRotationAngle);
                canvas.drawRect(left, top, right, bottom, mBoxPaint);
                canvas.rotate(-mRotationAngle);
                canvas.restore();
                mRotationAngle = 0;
            } else {
                canvas.drawRect(left, top, right, bottom, mBoxPaint);
            }
        }
    }
}

有多种绘制方法,不仅在 android 中,而且在 Java 中也是如此。问题是您试图通过旋转 Canvas 来绘制矩形。这是一种方式,但根据我个人的经验,我认为只有当你想旋转整个图片时,这才是一个不错的选择。如果没有,这可能会有点棘手,因为您需要放置一个旋转轴,您似乎没有使用它,因此 Android 将假设您想要从左上角或视图中心旋转(不记得了)

如果您选择了那个选项,您可以尝试这样做:

Matrix matrix = new Matrix();
matrix.setRotate(angle, rectangleCenterX, rectangleCenterY);
canvas.setMatrix(matrix);

但我建议您尝试不同的方法。通过计算多边形的轴,直接在您正在移动的矩形上进行旋转。您可以使用 Java 数学运算来做到这一点:

public void formShape(int cx[], int cy[], double scale) {
    double xGap = (width / 2) * Math.cos(angle) * scale;
    double yGap = (width / 2) * Math.sin(angle) * scale;
    cx[0] = (int) (x * scale + xGap);
    cy[0] = (int) (y * scale + yGap);
    cx[1] = (int) (x * scale - xGap);
    cy[1] = (int) (y * scale - yGap);
    cx[2] = (int) (x * scale - xGap - length * Math.cos(radians) * scale);
    cy[2] = (int) (y * scale - yGap - length * Math.sin(radians) * scale);
    cx[3] = (int) (x * scale + xGap - length * Math.cos(radians) * scale);
    cy[3] = (int) (y * scale + yGap - length * Math.sin(radians) * scale);
}

所以 (x,y) 是矩形的中心,高度告诉您它有多大。在 formShape(int[], int[], double) 方法中,cx 和 cy 将用于绘制形状,scale 是要在以后放大或缩小时使用的值,如果不只是使用 scale = 1;

现在绘制矩形,方法如下:

Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setStyle(Style.FILL);

int[] cx = new int[4];
int[] cy = new int[4];
Box box = yourBoxHere;
box.formShape(cx, cy, 1);
Path path = new Path();
path.reset(); // only needed when reusing this path for a new build
path.moveTo(cx[0], cy[0]); // used for first point
path.lineTo(cx[1], cy[1]);
path.lineTo(cx[2], cy[2]);
path.lineTo(cx[3], cy[3]);
path.lineTo(cx[0], cy[0]); // repeat the first point

canvas.drawPath(wallpath, paint);

对于多点触控旋转侦听器,您应该覆盖 Activity 或视图中的 2 个方法:

@Override
public boolean onTouch(View v, MotionEvent event) {
    if(event.getId() == MotionEvent.ACTION_UP)
        this.points = null;
    }
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if(event.getPointerCount() >= 2) {

        float newPoints[][] = new float[][] {
            {event.getX(0), event.getY(0)},
            {event.getX(1), event.getY(1)}
        };

        double angle = angleBetweenTwoPoints(newPoints[0][0], newPoints[0][1], newPoints[1][0], newPoints[1][1]);

        if(points != null) {
            double difference = angle - initialAngle;
            if(Math.abs(difference) > rotationSensibility) {
                listener.onGestureListener(GestureListener.ROTATION, Math.toDegrees(difference));
                this.initialAngle = angle;
            }
        } else {
            this.initialAngle = angle;
        }

        this.points = newPoints;
    }
}

public static double angleBetweenTwoPoints(double xHead, double yHead, double xTail, double yTail) {

    if(xHead == xTail) {
        if(yHead > yTail)
            return Math.PI/2;
        else
            return (Math.PI*3)/2;
    } else if(yHead == yTail) {
        if(xHead > xTail)
            return 0;
        else
            return Math.PI;
    } else if(xHead > xTail) {
        if(yHead > yTail)
        return Math.atan((yHead-yTail)/(xHead-xTail));
        else
            return Math.PI*2 - Math.atan((yTail-yHead)/(xHead-xTail));
    } else {
        if(yHead > yTail)
            return Math.PI - Math.atan((yHead-yTail)/(xTail-xHead));
        else
            return Math.PI + Math.atan((yTail-yHead)/(xTail-xHead));
    }
}

抱歉,这个答案越来越长,如果您对任何这些操作有进一步的疑问并且想更改解决方案的方法,请再次提问并在评论中告诉我。

希望对您有所帮助。