Android v2 MapFragment shaking when scrolling in Scrollview

前端 未结 2 1168
忘了有多久
忘了有多久 2021-01-06 21:53

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 scrol

2条回答
  •  既然无缘
    2021-01-06 22:35

    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
    

提交回复
热议问题