我有一个获取触摸事件(int x,int y,int 类型的事件)并使用给定信息管理 mapview(osmdroid)或按钮的活动。我必须覆盖地图视图,因此我将其放置在框架布局上并放置和上图。如果上面的图像可见,我无法平移下面的地图视图(但我可以放大和缩小)。
这是我的活动.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 的触摸事件起作用?