在默认的SupportMapFragment
中,当片段实例化时,将加载并创建mappview,从而在主线程上执行繁重的工作。
下面的日志发生在调用getMapAsync(this);
之前,这是主线程冻结约2秒的时刻。日志以某种方式证明,所有的东西都是在getMapAsync()
之前创建的。
难道地图不应该只在调用getMapAsync()
时加载吗?
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
D/OpenGLRenderer: endAllActiveAnimators on 0xb7966f90 (RippleDrawable) with handle 0xb7988688
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
I/zzy: Making Creator dynamically
W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
W/art: Suspending all threads took: 5.704ms
D/ChimeraCfgMgr: Loading module com.google.android.gms.maps from APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-fcfb08c37b9ca44c48d9936b0e1895ef8b9cffe0/MapsModule.apk
D/ChimeraModuleLdr: Loading module APK /data/data/com.google.android.gms/app_chimera/chimera-module-root/module-fcfb08c37b9ca44c48d9936b0e1895ef8b9cffe0/MapsModule.apk
D/ChimeraFileApk: Primary ABI of requesting process is armeabi-v7a
D/ChimeraFileApk: Classloading successful. Optimized code found.
I/Google Maps Android API: Google Play services client version: 7895000
I/Google Maps Android API: Google Play services package version: 8115236
I/e: Token loaded from file. Expires in: 393978062 ms.
I/e: Scheduling next attempt in 393678 seconds.
I/Choreographer: Skipped 44 frames! The application may be doing too much work on its main thread.
这是片段:
public class StoreMapFragment extends SupportMapFragment
implements OnMapReadyCallback {
private GoogleMap mMap;
public static StoreMapFragment newInstance() {
StoreMapFragment fragment = new StoreMapFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
loadStores();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
public void loadStores() {
if(mMap == null)
return;
// Add markers
}
}
相关:
First launch of Activity with Google Maps is very slow
what makes my map fragment loading slow?
最佳答案
通过延迟片段实例化来解决。
首先,我在另一个片段中添加了supportMapFragment,然后按照建议延迟了SupportMapFragment
实例化。
StoreMapFragment.java版本
public class StoreMapFragment extends Fragment
implements OnMapReadyCallback {
SupportMapFragment mMapFragment;
private GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_map, container, false);
if (mMap == null) {
final OnMapReadyCallback listener = this;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (isAdded()) {
mMapFragment = new SupportMapFragment();
FragmentManager fm = getChildFragmentManager();
fm.beginTransaction()
.replace(R.id.map, mMapFragment).commit();
mMapFragment.getMapAsync(listener);
}
}
}, 1000);
}
return view;
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
loadStores();
}
}
片段映射.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
关于java - 在主线程上加载SupportMapFragment并卡住接口(interface)(忽略getMapAsync),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32929558/