【问题标题】:Using android gesture on top of menu buttons在菜单按钮顶部使用 android 手势
【发布时间】:2011-01-12 04:45:36
【问题描述】:

我想要的是有一个选项菜单,用户可以在其中选择在以下菜单之间导航:

  1. 触摸一个按钮,然后按下轨迹球将其选中
  2. 从 Gestures Builder 中绘制预定义的手势

就目前而言,我用OnClickListener 创建了我的按钮,用GestureOverlayView 创建了手势。然后我选择开始一个新的Activity,具体取决于使用按下按钮还是执行手势。但是,当我尝试绘制手势时,它没有被拾取。只有按下按钮才会被识别。以下是我的代码:

public class Menu extends Activity implements OnClickListener, OnGesturePerformedListener {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create TextToSpeech
    myTTS = new TextToSpeech(this, this);
    myTTS.setLanguage(Locale.US);

    //create Gestures
    mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
    if (!mLibrary.load()) {
     finish();
    }

    // Set up click listeners for all the buttons.
    View playButton = findViewById(R.id.play_button);
    playButton.setOnClickListener(this);
    View instructionsButton = findViewById(R.id.instructions_button);
    instructionsButton.setOnClickListener(this);
    View modeButton = findViewById(R.id.mode_button);
    modeButton.setOnClickListener(this);
    View statsButton = findViewById(R.id.stats_button);
    statsButton.setOnClickListener(this);
    View exitButton = findViewById(R.id.exit_button);
    exitButton.setOnClickListener(this);

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(this);
}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
  ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

 // We want at least one prediction
 if (predictions.size() > 0) {
 Prediction prediction = predictions.get(0);
 // We want at least some confidence in the result
 if (prediction.score > 1.0) {
  // Show the gesture
   Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
   //User drew symbol for PLAY
    if (prediction.name.equals("Play")) {
       myTTS.shutdown();
       //connect to game
      // User drew symbol for INSTRUCTIONS
     } else if (prediction.name.equals("Instructions")) {
           myTTS.shutdown();
           startActivity(new Intent(this, Instructions.class));
       // User drew symbol for MODE
   } else if (prediction.name.equals("Mode")){
      myTTS.shutdown();
      startActivity(new Intent(this, Mode.class));
       // User drew symbol to QUIT
  } else {
      finish();
   }
  }
 }
}

@Override
    public void onClick(View v) {
      switch (v.getId()){
      case R.id.instructions_button:
         startActivity(new Intent(this, Instructions.class));
         break;
      case R.id.mode_button:
         startActivity(new Intent(this, Mode.class));
         break;
      case R.id.exit_button:
         finish();
         break;
  }  
}

任何建议将不胜感激!

【问题讨论】:

  • 手势有效吗? onGesturePerformed 被调用了吗?
  • 你的布局是什么样的?很有可能您的手势叠加层不在用户绘制手势的位置。
  • 手势在许多 ui 控件上似乎效果不佳。我不得不恢复使用专用的“滑动我”控件,用户在其中滑动并显示“检测到手势”。然而,它吞噬了房地产。看看我的应用程序 CueBrain 看看它的样子。
  • 此问题缺少一半信息:请显示您的 xml 布局。

标签: android gesture-recognition


【解决方案1】:

如果我理解正确,您想在同一个视图上执行点击动作和手势动作,我不知道如何实现,但是,我们可以在按钮后面设置不可见的视图来处理手势,而顶部的按钮将像往常一样处理点击事件。

默认计算器应用程序使用这种方法在普通模式和高级模式之间切换。

Calculator Source

参考, com.android.calculator2.PanelSwitcher - 用于手势事件 com.android.calculator2.EventListener - 点击事件

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2021-05-03
    • 2017-12-27
    • 1970-01-01
    相关资源
    最近更新 更多