Android多指手势

时间:2014-08-05 04:25:50

标签: android gesture

我正在研究多指手势,我尝试使用Google手势生成器,但它不支持多指手势,如何识别Android中的两个手指V形手势。?

2 个答案:

答案 0 :(得分:4)

我确信你可以使用ScaleGestureDetector来获取此信息。

毕竟从顶部开始的“V”只是一个在Y轴上有一些平移的夹点。

所以我认为你可以分析焦点和比例因子来确定“V”已经发生。

这是一个工作示例。最后我不需要看规模。您可以调整几个灵敏度值,例如可接受的角度范围和Y到X的移动比率。

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.TextView;

public class MainActivity extends Activity {

    public static class VListener extends
            ScaleGestureDetector.SimpleOnScaleGestureListener {
        private float initialFocusX;
        private float initialFocusY;

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            Log.d(TAG, String.format("%.2f,%.2f s:%.2f", detector.getFocusX(),
                    detector.getFocusY(), detector.getScaleFactor()));
            initialFocusX = detector.getFocusX();
            initialFocusY = detector.getFocusY();
            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            float deltaX = detector.getFocusX() - initialFocusX;
            float deltaY = detector.getFocusY() - initialFocusY;

            if (deltaY == 0) {
                Log.d(TAG, "Not a V, no Y movement");
                onNonV();
                return;
            }

            float yMovementRatio = Math.abs(deltaY / deltaX);
            if (yMovementRatio < 4) {
                Log.d(TAG,
                        String.format(
                                "Not a V, the ratio of Y movement to X was not high enough: %.2f",
                                yMovementRatio));
                onNonV();
                return;
            }

            float angle = (float) Math.toDegrees(Math.atan2(deltaY, deltaX));
            if (angle > 80 && angle < 100) {
                Log.d(TAG, "V!");
                onV();
                return;
            } else {
                Log.d(TAG,
                        String.format(
                                "Not a V, the angle shows the V was drawn in the wrong direction: %.2f",
                                angle));
                onNonV();
            }
        }

        protected void onV() {
        }

        protected void onNonV() {
        }
    }

    protected static final String TAG = "MainActivity";

    private ScaleGestureDetector mScaleGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView t = (TextView) findViewById(R.id.vTextIndicator);

        mScaleGestureDetector = new ScaleGestureDetector(this, new VListener() {
            @Override
            protected void onV() {
                t.setText("V!");
            }

            @Override
            protected void onNonV() {
                t.setText("Non V");
            }
        });
    }

    public boolean onTouchEvent(MotionEvent event) {
        boolean retVal = mScaleGestureDetector.onTouchEvent(event);
        return retVal || super.onTouchEvent(event);
    }

}

activity_main.xml布局只是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/vTextIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

答案 1 :(得分:0)

示例应用 - InteractiveChart.zip

此链接可能有所帮助 - Draggin & ScalingHandling Multi-Touch Gestures