Moving MapFragment (SurfaceView) causes black background flickering

后端 未结 16 1386
误落风尘
误落风尘 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:40

    I had same issue of black screen on scrolling of list view, i was using map fragment with list view in same screen i have resolved this issue with use of the magical property in xml where i am talking list view just we have to put android:scrollingCache="false".my issue is fixed try this property to stop lagging and flickering in your maps.

    0 讨论(0)
  • 2020-12-04 09:40

    Change Your FrameLayout to RelativeLayout if Possible

    0 讨论(0)
  • 2020-12-04 09:42

    try adding..

    getHolder().setFormat(PixelFormat.TRANSLUCENT);

    to your SurfaceView's constructor. The TextureView should also work, but will require you to drop support for sub api 14 devices.

    0 讨论(0)
  • 2020-12-04 09:42

    Guys the best solution I found was to instantiate your support map through this. Does wonders

    SupportMapFragment mapFragment = SupportMapFragment.newInstance(new GoogleMapOptions().zOrderOnTop(true));
    
    0 讨论(0)
  • 2020-12-04 09:43

    Taking screenshot of a map isn't possible, but maybe someone will get inspired by this.

    It's possible with the snapshot interface from GoogleMap. Now you can display a snapshot of the map while scrolling the page. With this I solved my problem with the ViewPager, maybe you can change the code to fit your SlidingMenu.

    Here is my code to setting the snapshot (it's in the same fragment as map, called FragmentMap.java):

    public void setSnapshot(int visibility) {
        switch(visibility) {
        case View.GONE:
            if(mapFragment.getView().getVisibility() == View.VISIBLE) {
                getMap().snapshot(new SnapshotReadyCallback() {
                    @Override
                    public void onSnapshotReady(Bitmap arg0) {
                        iv.setImageBitmap(arg0);
                    }
                });
                iv.setVisibility(View.VISIBLE);
                mapFragment.getView().setVisibility(View.GONE);
            }
            break;
        case View.VISIBLE:
            if(mapFragment.getView().getVisibility() == View.GONE) {
                mapFragment.getView().setVisibility(View.VISIBLE);
                iv.setVisibility(View.GONE);
            }
            break;
        }
    }
    

    Where "mapFragment" is my SupportedMapFragment and "iv" is an ImageView (make it match_parent).

    And here I am controlling the scroll:

    pager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 0 && positionOffsetPixels > 0 || position == 1 && positionOffsetPixels > 0) {
                    ((FragmentMap)adapter.getRegisteredFragment(1)).setSnapshot(View.GONE);
                } else if(position == 1 && positionOffsetPixels == 0) {
                    ((FragmentMap)adapter.getRegisteredFragment(1)).setSnapshot(View.VISIBLE);
                }
            }
            @Override
            public void onPageScrollStateChanged(int arg0) {}
            @Override
            public void onPageSelected(int arg0) {}
        });
    

    My fragment with map (FragmentMap) is on position 1, so I need to controll the scroll from position 0 to 1 and from position 1 to 2 (the first if-clause). "getRegisteredFragment()" is a function in my custom FragmentPagerAdapter, in which I have a SparseArray(Fragment) called "registeredFragments".

    So, whenever you scroll to or from your map, you always see a snapshot of it. This works very well for me.

    0 讨论(0)
  • 2020-12-04 09:48

    In my scenario, I had an initial Fragment, which on a button click swapped in a second fragment with a MapFragment inside it. I noticed that this flicker only occurred the first time you would swap the fragment - popping the back stack, and swapping in another fragment of the same type did not cause a flicker.

    So figuring it had something to do with adding an initial SurfaceView to the window, I tried adding an empty instance of a SurfaceView to my initially loaded fragment, behind it's main view. And bingo, no more flicker when animating in the next fragment!

    I know it's not the cleanest solution, but it works. The Google MapFragment (or SupportMapFragment in my case) still works fine, as the unused SurfaceView is in the previous fragment.

    I should add I am using the v2 API.

    Update @VenomVendor

    I add the empty SurfaceView to the first fragment to be shown, at index 0 - behind the main view. In my case, the Home fragment is the previous fragment to one that contains the Google Maps fragment, not sure if that matters:

    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class HomeFragment extends FragmentBase {
    private SurfaceView sview;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
    
        //this fixes google maps surface view black screen animation issue
        sview = new SurfaceView(getActivity());
        sview.setZOrderOnTop(true);    // necessary
        SurfaceHolder holder = sview.getHolder();
        holder.setFormat(PixelFormat.TRANSPARENT);
    
        container.addView(sview, 0);
        ...
    }
    
    0 讨论(0)
提交回复
热议问题