我正在使用 SupportMapFragment 在 ScrollView 中显示静态地图。我不喜欢移动/缩放地图,只显示位置。
当我向下/向上滚动时,地图在其边界内晃动,感觉非常滞后。我的问题是,如何消除这种滞后,或者如何使用 v2 api 映射的静态版本。
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin_half"
android:background="@color/white"
android:orientation="vertical" >
...
<fragment
android:id="@+id/fragmentMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_below="@id/viewDivider"
android:layout_margin="@dimen/margin_half"
/>
<View
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignBottom="@id/fragmentMap"
android:layout_alignLeft="@id/fragmentMap"
android:layout_alignRight="@id/fragmentMap"
android:layout_alignTop="@id/fragmentMap"
android:background="@android:color/transparent" />
...
</RelativeLayout>
这实际上与ScrollView 中的 MapFragment,尽管我使用这个技巧来删除旧设备上的黑色剪辑,正如您在透明视图中看到的那样。
我还禁用了所有手势和地图视图的可点击性:
getMap().getUiSettings().setAllGesturesEnabled(false);
getMap().getUiSettings().setZoomControlsEnabled(false);
getMap().getUiSettings().setMyLocationButtonEnabled(false);
...
mapView.setClickable(false);
mapView.setFocusable(false);
mapView.setDuplicateParentStateEnabled(false);
随着新版本 Google Play 服务的推出,Google 添加了snapshot方法以检索当前显示的地图的位图,而该地图可能会被显示。所以交互是不可能的,但至少地图不再晃动了。
use
GoogleMap.snapshot (GoogleMap.SnapshotReadyCallback callback)
并实现 SnapshotReadyCallback。交付快照后,将 MapFragment 替换为包含快照的 ImageView。
这个例子在 onResume 方法中运行得很好:
@Override
public void onResume() {
super.onResume();
notifyDataChanged();
final ViewTreeObserver viewTreeObserver = fragmentMap.getView()
.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
onMesuredMap(this);
}
});
}
}
private void onMesuredMap(OnGlobalLayoutListener listener) {
if (fragmentMap.getView().getWidth() != 0
&& fragmentMap.getView().getHeight() != 0) {
fragmentMap.getView().getViewTreeObserver()
.removeOnGlobalLayoutListener(listener);
makeSnapShot();
}
}
private void makeSnapShot() {
Runnable action = new Runnable() {
@Override
public void run() {
fragmentMap.getMap().snapshot(new SnapshotReadyCallback() {
@Override
public void onSnapshotReady(Bitmap arg0) {
imageViewStaticMap.setImageBitmap(arg0);
removeMapFragment();
}
});
}
};
//litle hack to make sure that the map is loaded for sure
fragmentMap.getView().postDelayed(action, 1500);
}
private void removeMapFragment() {
if (fragmentMap.isVisible()) {
getChildFragmentManager()
.beginTransaction()
.remove(fragmentMap)
.commit();
}
}
边注:要使用新版本,请运行 Android SDK Manager 的更新,并在使用 maven 时运行maven-android-sdk-部署器 with:
mvn install -fae
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)