【问题标题】:Android multi finger gesture安卓多指手势
【发布时间】:2014-09-27 15:58:22
【问题描述】:

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

【问题讨论】:

  • 尝试编写一个自定义手势监听器,如以下问题所示:link 也参考 link 以使用多点触控
  • 你可以使用this
  • @yassine : 哪一个..?
  • @Vishwanath :Sam 的回答,他解释了它是如何工作的
  • @yassine__ 你能给我山姆的答案链接吗

标签: android gesture


【解决方案1】:

我相信您可以为此使用 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>

【讨论】:

    【解决方案2】:

    示例应用 - InteractiveChart.zip

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      相关资源
      最近更新 更多