2

我是安卓新手。我正在做手势部分。我被卡住了,我找不到在 android 中检测其缩放或旋转手势(两指)的方法。

case MotionEvent.ACTION_MOVE:
    if(event.getPointerCount() == 2) {
        float currentX = event.getX();
        Log.v(GlobalVars.TAG, (currentX - lastX) + "");
        lastX = currentX;
        double currentAngle = getAngle(event.getX(), event.getY());
        Log.v(GlobalVars.TAG, "Rotation Y::" + (startAngle - currentAngle));

        if((startAngle - currentAngle) > 0 && mRotate.y > (-1 * 45) && )  {
                mRotate.y -= 1.0;
            } else if((startAngle - currentAngle) < 0 && mRotate.y < 45) {
                mRotate.y += 1.0; 
            }
        startAngle = currentAngle;
    }



private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mOrigin.z *= detector.getScaleFactor(); // scale change since previous event
        mOrigin.z = Math.max(MIN_ZOOM, Math.min(mOrigin.z, MAX_ZOOM)); //Max Zoom Value.
        return true;
    }
}
4

1 回答 1

1

您可以从指针移动方向(如左、右或上、下)检测它。如果第一个指针向左或向下移动,第二个指针向右或向上移动,这意味着用户正在尝试旋转图像。所以当时禁用缩放事件。我检测旋转和缩放的代码是这样的。我希望它对你有帮助。

private boolean isRotate(MotionEvent event){
    int dx1 = (int) (event.getX(0) - lastEvent[0]);
    int dy1 = (int) (event.getY(0) - lastEvent[2]);
    int dx2 = (int) (event.getX(1) - lastEvent[1]);
    int dy2 = (int) (event.getY(1) - lastEvent[3]);
    Log.d("dx1 ", ""+ dx1);
    Log.d("dx2 ", "" + dx2);
    Log.d("dy1 ", "" + dy1);
    Log.d("dy2 ", "" + dy2);
    //pointer 1
    if(Math.abs(dx1) > Math.abs(dy1) && Math.abs(dx2) > Math.abs(dy2)) {
        if(dx1 >= 2.0 && dx2 <=  -2.0){
            Log.d("first pointer ", "right");
            return true;
        }
        else if(dx1 <= -2.0 && dx2 >= 2.0){
            Log.d("first pointer ", "left");
            return true;
        }
    }
    else {
         if(dy1 >= 2.0 && dy2 <=  -2.0){
                Log.d("seccond pointer ", "top");
                return true;
            }
            else if(dy1 <= -2.0 && dy2 >= 2.0){
                Log.d("second pointer ", "bottom");
                return true; 
            }

    }

    return false;
}

然后你可以在 ACTION_MOVE 上检查它

if (mode == DRAG && !isZoomAndRotate) {
                isZoomAndRotate = false;
                view.setTranslationX((event.getX() - start.x) + view.getTranslationX());
                view.setTranslationY((event.getY() - start.y) + view.getTranslationY());
            } else if (mode == ZOOM && event.getPointerCount() == 2) {
                isZoomAndRotate = true;
                boolean isZoom = false;
                if(!isRotate(event)){
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        float scale = newDist / oldDist * view.getScaleX();
                        view.setScaleX(scale);
                        view.setScaleY(scale);
                        isZoom = true;
                    }
                }
                else if(!isZoom){
                    newRot = rotation(event);
                    view.setRotation((float)(view.getRotation() + (newRot - d)));
                }
            }
于 2014-06-26T05:52:33.677 回答