问题
I am using the SupportMapFragment to display a static map in a ScrollView. I do not like to move / zoom the Map, just showing where the location is.
When I am scrolling down/up the map shakes inside its bounds, it feels pretty laggy. My question is, how is ist possible to remove this lag, or how to use a static version of the v2 api map.
This is my layout:
<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>
This is not really related to MapFragment in ScrollView, even though i used this trick to remove the black clipping on older devices, as you can see with the transparent view.
I also disabled all gestures and the click-ability of the maps view:
getMap().getUiSettings().setAllGesturesEnabled(false);
getMap().getUiSettings().setZoomControlsEnabled(false);
getMap().getUiSettings().setMyLocationButtonEnabled(false);
...
mapView.setClickable(false);
mapView.setFocusable(false);
mapView.setDuplicateParentStateEnabled(false);
回答1:
With the new release of the Google Play Services, Google added a snapshot method in order to retrieve a Bitmap of the currenrt shown map, which instead may be shown. So interaction is not possible but at leased the map is not shaking anymore.
use
GoogleMap.snapshot (GoogleMap.SnapshotReadyCallback callback)
and implement the SnapshotReadyCallback. Once the Snapshot is delivered, replace the MapFragment with an ImageView containing the Snapshot.
This example works pretty well in the onResume method:
@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();
}
}
Sidenote: To use the new release run an Update of the Android SDK Manager, and when using maven run the maven-android-sdk-deployer with:
mvn install -fae
回答2:
Try using this class, it should look better at least when scrolling right and left in our scrollview:
public class TransparentSupportMapFragment extends SupportMapFragment {
public TransparentSupportMapFragment() {
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle savedInstance) {
View layout = super.onCreateView(inflater, view, savedInstance);
FrameLayout frameLayout = new FrameLayout(getActivity());
frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return layout;
}
}
来源:https://stackoverflow.com/questions/17829715/android-v2-mapfragment-shaking-when-scrolling-in-scrollview