通过Maps API v2处理SurfaceView中的触摸事件

Miš*_*eić 11 android google-maps google-maps-android-api-2

我在创建事件处理程序时遇到问题,该事件处理程序将在用户移动地图时触发.有一个OnCameraChangeListener,但它会地图移动停止触发.

我创建SurfaceView了地图,现在我不知道如何处理onScroll事件OnGestureListener.

以下是示例代码:

SurfaceView sv = new SView();
sv.setZOrderOnTop(true);
mapView.addView(sv);
Run Code Online (Sandbox Code Playgroud)

...

public class SView extends SurfaceView implements Runnable, android.view.GestureDetector.OnGestureListener {
...
@Override
public boolean onTouchEvent(MotionEvent event) {
    //this worked in Overlay in Maps API v1
    if(gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return false;
}
...

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    Log.i("test","onScroll");
    return false;
}

...
}
Run Code Online (Sandbox Code Playgroud)

如果我return true进入onTouchEvent,onScroll被调用但是我无法移动Map(很明显,事件被消耗),而且我不知道如何将事件调度到Map对象.

任何人都知道SurfaceView在移动地图时如何更新?

Pav*_*dka 15

我已经实现了一些其他解决方案来解决这个问题.我做了什么 - 我将我的GoogleMap放在自定义RelativeLayout中.之后onInterceptTouchEvent回调就像魅力一样,底层地图按预期接收触摸事件

这是我的xml:

<com.my.package.name.map.MapRelativeLayout
    android:id="@+id/map_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</com.my.package.name.map.MapRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

和自定义相对布局:

package com.my.package.name.map;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

public class MapRelativeLayout extends RelativeLayout
{
    private GestureDetector gestureDetector;

    public MapRelativeLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        gestureDetector.onTouchEvent(ev);
        return false;
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY)
        {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY)
        {
            //do whatever you want here
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)