【问题标题】:How can I capture mouse wheel events in Android? [duplicate]如何在 Android 中捕获鼠标滚轮事件? [复制]
【发布时间】:2020-01-27 20:31:58
【问题描述】:

我正在编写一个 Android 应用程序,用于 Chromebook,它使用谷歌地图。

我可以启用地图缩放控件,还可以启用“捏合”手势来放大或缩小地图。

我想做的是让用户的鼠标滚轮也可以放大和缩小地图。

Android 有办法监听鼠标滚轮事件吗?

【问题讨论】:

  • 是的,成功了。我将发布我的代码作为答案。谢谢莫里森!

标签: java android google-maps


【解决方案1】:

感谢用户 Morrison Chang 指出this post 有答案。

我在 Map 片段的 OnCreatewView 覆盖中附加了一个 OnGeneralMotion 处理程序:

        rootView.setOnGenericMotionListener(new View.OnGenericMotionListener() {
            //
            // Listen for mouse wheel events. 
            @Override
            public boolean onGenericMotion(View v, MotionEvent event) {
                if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_SCROLL:
                            if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f)
                                mMap.animateCamera(CameraUpdateFactory.zoomOut());
                            else
                                mMap.animateCamera(CameraUpdateFactory.zoomIn());
                            return true;
                    }
                }
                return false;
            }
        });

【讨论】:

    【解决方案2】:

    我不确定,但我认为您可以通过使用此代码 sn-p 来实现这一点。

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
     if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
      switch (event.getAction()) {
       case MotionEvent.ACTION_SCROLL:
         //your code here
        return true;
       }
     } 
     return super.onGenericMotionEvent(event);
    

    }

    【讨论】:

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