Moving MapFragment (SurfaceView) causes black background flickering

后端 未结 16 1435
误落风尘
误落风尘 2020-12-04 09:32

I\'m trying to implement new Android Google Maps API (v2). However it doesn\'t seem to go well with SlidingMenu. As you may know, MapFragment implementation is

16条回答
  •  没有蜡笔的小新
    2020-12-04 09:52

    Here's a very simple workaround I used to get rid of the flashing in a ViewPager. I would imagine the same will hold true for any Fragment or other dynamically added MapView.

    The problem doesn't really seem to be the MapView itself, but the resources needed to run the MapView. These resources are only loaded the first time you fire up a MapView in your activity and re-used for each subsequent MapView, as you would expect. This loading of resources is what causes the screen to flash.

    So to remove the flashing, I just included another MapView in the layout of my Activity (location in the activity is irrelevant). As soon as the Activity has loaded, I just set the Visibility of the extra MapView to GONE. This means all the resources needed for your MapView are ready for when you fire up any of your Fragments using MapViews with no lag, no flashing, all happiness.

    This is of course a workaround and not a real 'solution' to the underlying problem but it will resolve the side effects.

    So to cover off the code used for completeness:

    Randomly (but cleanly) placed in my Activity Layout:

    
    

    Then in my Activity Class

    private MapView flashMap;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
                // Code omitted
    
        try {
            MapsInitializer.initialize(this);
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
        flashMap = (MapView)findViewById(R.id.flash_map);
        flashMap.onCreate(savedInstanceState);
    }
    
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
            flashMap.onSaveInstanceState(outState);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        flashMap.onResume();
        flashMap.setVisibility(View.GONE); // Just like it was never there...
    }
    
    @Override
    public void onPause() {
        flashMap.onPause();
        super.onPause();
    }
    
    @Override
    public void onDestroy() {
        flashMap.onDestroy();
        super.onDestroy();
    }
    
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        flashMap.onLowMemory();
    }
    

提交回复
热议问题