在mapview android(action_move)中丢失触摸事件

时间:2014-07-17 18:46:40

标签: android android-mapview osmdroid touch-event panning

我有一个活动获取触摸事件(int x,int y,int类型的事件)并管理mapview(osmdroid)或具有给定信息的按钮。我必须覆盖mapview,所以我将它放在framelayout和place和upper image上。如果上面的图像可见,我无法平移下面的mapview(但我能够zoomIn和zoomOut)。

这是我的activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.client.Client"
tools:ignore="MergeRootFrame" >

<Button
            android:id="@+id/Disconnect"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Disconnect/ Reconnect" 
            android:onClick="disconnect"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frameLayout"
android:layout_below="@+id/disconnect">

<LinearLayout
    android:id="@+id/memeContent"
    android:layout_width="535px"
    android:layout_height="350px"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" 
    >

    <org.osmdroid.views.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="300px" 
        >
    </org.osmdroid.views.MapView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50px"
         >

        <Button
            android:id="@+id/ZoomIn"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="5"
            android:onClick="ZoomIn"
            android:text="Zoom In"
            android:textSize="10dp" />

        <Button
            android:id="@+id/ZoomOut"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="5"
            android:text="Zoom out"
            android:onClick="ZoomOut" 
            android:textSize="10dp" />

    </LinearLayout>

</LinearLayout>
  <ImageView
    android:id="@+id/imageViewFront"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/image"
    android:scaleType="centerCrop" 
    android:focusable="false"
    android:focusableInTouchMode="false"/>



</FrameLayout>

</RelativeLayout>

这是我的MainActivity.java

public class MainActivity extends Activity {
LinearLayout memecontentView;   
FrameLayout frameLayout;
ImageView imageViewFront;
View v;
MapView mapView;

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_client_late);
    imageViewFront= (ImageView) findViewById(R.id.imageViewFront);
    //imageViewFront.setVisibility(View.GONE);
    mapView  = (MapView) findViewById(R.id.mapView);
    mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
    mapView.setMultiTouchControls(true);
    mapView.setUseDataConnection(true);
    memecontentView=(LinearLayout) findViewById(R.id.memeContent);
    mapView.setBuiltInZoomControls(false);
}
//look for items in given coordinates
public void lookForButton(String msg){
    String[] strArray = msg.split(" ");
    final MotionEvent m;
    final int x=Integer.valueOf(strArray[1]);
    final int y=Integer.valueOf(strArray[2]);
    int type=Integer.valueOf(strArray[3]);
    switch (type){
        case 2:
            m = MotionEvent.obtain(226707,226707,0/*ACTION_DOWN*/,x,y,0);
            runOnUiThread(new Runnable() {
                public void run() {
                    memecontentView.dispatchTouchEvent(m);
                }
            });
            break;
    case 3:
        m = MotionEvent.obtain(226707,226707,1/*ACTION_DOWN*/,x,y,0);
        runOnUiThread(new Runnable() {
            public void run() {
                memecontentView.dispatchTouchEvent(m);
            }
        }); 
        break;
    case 5:
        m = MotionEvent.obtain(226707,226707,2/*ACTION_MOVE*/,x,y,0);
        runOnUiThread(new Runnable() {
            public void run() {
                memecontentView.dispatchTouchEvent(m);
            }
        }); 
        break;  
    }

}
public void ZoomIn(View v){
    mapView.getController().zoomIn();
}
public void ZoomOut(View v){
    mapView.getController().zoomOut();
}
}

如果上面的图像不可见(imageViewFront.setVisibility(View.INVISIBLE);),上面的代码效果很好,但如果我评论该行(我需要这样做),我就无法平移mapview。我不知道上面的图像是否正在窃取它的触发器。我怎么能防止这种情况?或者即使mapView在?

下如何使MapView的触摸事件工作

1 个答案:

答案 0 :(得分:0)

尝试设置onTouchEventListener,为所有触摸返回false。将其应用于可能位于mapview上方的任何视图,包括布局。这应该让触摸转到它下面的视图,最终会触及mapview。

frameLayout.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
                 return false;
        }
    });