如何在Android中捕获鼠标滚轮事件?

时间:2020-01-27 20:31:58

标签: java android google-maps

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

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

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

Android是否可以侦听鼠标滚轮事件?

2 个答案:

答案 0 :(得分:0)

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

我在地图片段的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;
            }
        });

答案 1 :(得分:0)

我不确定,但我认为您可以使用此代码段来实现这一目标。

@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);

}