如何检测它是在Android中的旋转手势还是缩放手势

时间:2013-08-12 12:27:42

标签: android

我是android新手。我正在开展手势部分。我卡住了,无法找到在Android中检测其Zoom或Rotation Gesture(Two Finger)的方法。

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;
    }
}

1 个答案:

答案 0 :(得分: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)));
                }
            }