MapView的片段很慢

时间:2019-04-09 12:23:35

标签: android performance kotlin android-mapview

我正在制作一个应用程序,其中一项活动涉及所有有关抽屉等的动作,而其中三个片断显示了主要内容。在MainActivity中,带有图的片段放置在fragmentcontainer内。但是当我启动该应用程序时,在显示地图之前,屏幕会变白2至3秒钟。

我已经尝试过将getMapAsync与OnMapReadyCallback结合使用,但该方法不起作用,我也在寻找其他解决方案,但找不到任何有效的方法。

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
    setSupportActionBar(toolbar)
    supportActionBar?.title = getString(R.string.app_name)

    val toggle = ActionBarDrawerToggle(
        this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
    )
    drawer_layout.addDrawerListener(toggle)
    toggle.syncState()
    nav_view.setNavigationItemSelectedListener(this)

    supportFragmentManager.beginTransaction()
        .replace(R.id.fragmentContainer, MapFragment())
        .commit()

    if (!checkDataConnection()) {
        val dialogView = View.inflate(this, R.layout.dialog_no_data, null)
        val dialogBuilder = AlertDialog.Builder(this)
            .setView(dialogView)
            .setCancelable(false)
        val alertDialog = dialogBuilder.show()

        dialogView.rescanDataConnection.setOnClickListener {
            if (checkDataConnection()) {
                alertDialog.dismiss()
            }
        }
    }
}

MapFragment.kt

class MapFragment : Fragment(), OnMapReadyCallback {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_map, container, false)

        val mapView = view.findViewById<MapView>(R.id.mapView)
        mapView.onCreate(savedInstanceState)
        mapView.getMapAsync(this)

        try {
            MapsInitializer.initialize(this.activity)
        } catch (e: GooglePlayServicesNotAvailableException) {
            e.printStackTrace()
        }
        return view
    }

    override fun onMapReady(p0: GoogleMap?) {
        // Necessary function for loading map async
    }
}

那么有人可以帮助我提高性能吗?

0 个答案:

没有答案