Android v2 MapFragment shaking when scrolling in Scrollview

前端 未结 2 1155
忘了有多久
忘了有多久 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:27

    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;
        }
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题