Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中捕获鼠标滚轮事件?_Java_Android_Google Maps - Fatal编程技术网

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

Java 如何在Android中捕获鼠标滚轮事件?,java,android,google-maps,Java,Android,Google Maps,我正在编写一个Android应用程序,用于Chromebook,使用谷歌地图 我可以启用地图缩放控件,也可以启用“收缩”手势,以放大或缩小地图 我想做的是允许用户的鼠标滚轮放大和缩小地图 Android有办法监听鼠标滚轮事件吗?感谢用户Morrison Chang指出答案 我在映射片段的OnCreateView覆盖中附加了OnGeneralMotion处理程序: rootView.setOnGenericMotionListener(new View.OnGenericMotio

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

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

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


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

感谢用户Morrison Chang指出答案

我在映射片段的OnCreateView覆盖中附加了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;
            }
        });
rootView.setOnGenericMotionListener(新视图.OnGenericMotionListener(){
//
//收听鼠标滚轮事件。
@凌驾
公共布尔onGenericMotion(视图v、运动事件){
if(0!=(event.getSource()&InputDevice.SOURCE\u CLASS\u指针)){
开关(event.getAction()){
case MotionEvent.ACTION\u滚动:
if(event.getAxisValue(MotionEvent.AXIS_VSCROLL)<0.0f)
mMap.animateCamera(CameraUpdateFactory.zoomOut());
其他的
mMap.animateCamera(CameraUpdateFactory.zoomIn());
返回true;
}
}
返回false;
}
});

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

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

}

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