百度地图 Fragment之间切换黑屏现象解决方案

来源:互联网 发布:英美关系知乎 编辑:程序博客网 时间:2024/05/12 17:37

       用过百度地图的人多很忧伤,各种bug, 已无力吐槽,最无语的可能就是会出现黑屏现象,比如一个Activity包含三个Fragment, 其中一个Fragment嵌套MapView使用,在切换这三个Fragment时会出现明显的黑屏,这个问题出现很久了,很早的SDK版本就存在,现在最新的SDK版本依然存在这个问题,解决方案如下:


问题分析:百度地图切换界面出现黑屏原因是地图退出释放内存时渲染出现bug导致,因为是百度地图本身内部bug,所以我们无法从本质上去修复这个bug, 但是我们bug的出现黑屏现象隐藏起来,即:将地图的释放过程放入后台进行,从而避免黑屏的出现。


1,在使用MapView的Fragment的onResume/onPause方法中手动调用设置view是否可见,具体如下:

@Override  protected void OnPause(){      mMapView.setVisibility(View.INVISIBLE);      mMapView.onPause();      super.onPause();  }进入页面时@Override  protected void onResume(){          mMapView.setVisibility(View.VISIBLE);          mMapView.onResume();          super.onResume();  }

2, Activity对应XML布局中单独使用一个View来替换嵌套有MapView的Fragment,需要显示该Fragment 时,设置该View为可见,不使用时设置为不可见。

  <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <FrameLayout            android:id="@+id/mapFrameLayout"            android:layout_width="match_parent"            android:layout_height="match_parent" >        </FrameLayout>        <FrameLayout            android:id="@+id/otherFrameLayout"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:visibility="invisible" >        </FrameLayout>    </FrameLayout> 

3,从地图界面切换只其他界面时,手动隐藏地图界面,并在后台释放内存,完整代码如下。


import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.widget.FrameLayout;public class MainActivity extends FragmentActivity{FragmentManager manager;FrameLayout mapFrameLayout;FrameLayout otherFrameLayout;@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.activity_main);mapFrameLayout = (FrameLayout)findViewById(R.id.mapFrameLayout);otherFrameLayout = (FrameLayout)findViewById(R.id.otherFrameLayout);BaiduMapFragment mBaiduMapFragment = new BaiduMapFragment();manager = getSupportFragmentManager();manager.beginTransaction().add(R.id.mapFrameLayout, mBaiduMapFragment, "map_fragment").commit();}public void toMapFragment(View v){mapFrameLayout.setVisibility(View.VISIBLE);otherFrameLayout.setVisibility(View.INVISIBLE);BaiduMapFragment mBaiduMapFragment = new BaiduMapFragment();manager.beginTransaction().replace(R.id.mapFrameLayout, mBaiduMapFragment, "map_fragment").commit();}public void toOtherFragment(View v){mapFrameLayout.setVisibility(View.INVISIBLE);otherFrameLayout.setVisibility(View.VISIBLE);OtherFragment otherFragment = new OtherFragment();FragmentTransaction mTransaction = manager.beginTransaction();mTransaction.replace(R.id.otherFrameLayout, otherFragment, "other");Fragment fragment = manager.findFragmentByTag("map_fragment");if(fragment != null){mTransaction.remove(fragment);//释放百度地图 Fragment}mTransaction.commit();}}


Demo工程下载地址:http://download.csdn.net/detail/easyer2012/9275677





3 1
原创粉丝点击