android - setContentView 花费太多时间

标签 android android-layout android-mapview mapactivity

我有 MapView、TextView、3 个 ImageButton 和 1 个简单按钮的 Activity 。第一次为此 Activity 调用 setContentView 需要太多时间(将近 10 秒)。这是我的 View 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:padding="0px" >

    <com.google.android.maps.MapView
        android:id="@+id/map_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/txt_route_info"
        android:apiKey="MyAPIKeyGoesHere"
        android:clickable="true"
        android:enabled="true" />

    <Button
        android:id="@+id/btn_accept"
        android:layout_width="fill_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:layout_margin="2dip"
        android:enabled="false"
        android:text="@string/btn_accept" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/textEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:padding="0px" >

        <TextView
            android:id="@+id/txt_route_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textEdit"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:padding="0px" >

        <ImageButton
            android:id="@+id/btn_zoom_in"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:contentDescription="@string/btn_zoom_in_content_descriptor"
            android:src="@drawable/zoom_in" />

        <ImageButton
            android:id="@+id/btn_zoom_out"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:contentDescription="@string/btn_zoom_out_content_descriptor"
            android:src="@drawable/zoom_out" />

        <ImageButton
            android:id="@+id/btn_my_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:contentDescription="@string/btn_my_location_content_descriptor"
            android:src="@drawable/my_location" />
    </LinearLayout>

</RelativeLayout>

我不知道是什么让 Activity 加载如此缓慢。我读过这个主题:setContentView taking long time (10-15 seconds) to execute 但我仍然无法解决我的问题。如果您能给我任何提示或想法,我将不胜感激。

更新:

我的 Activity 代码中的 onCreate 方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.google_map_view);
    mapView = (MapView) this.findViewById(R.id.map_view);
    mapView.setBuiltInZoomControls(false);

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    if (locationManager == null) {
        this.finish();
        return;
    }

    final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
    myLocationOverlay.disableCompass();
    myLocationOverlay.enableMyLocation();

    acceptBtn = (Button) findViewById(R.id.btn_accept);
    final View.OnClickListener acceptBtnListener = new AcceptBtnListener();
    acceptBtn.setOnClickListener(acceptBtnListener);

    final ImageButton zoomInBtn = (ImageButton) findViewById(R.id.btn_zoom_in);
    final ImageButton zoomOutBtn = (ImageButton) findViewById(R.id.btn_zoom_out);
    myLocationBtn = (ImageButton) findViewById(R.id.btn_my_location);
    zoomInBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mapController.zoomIn();
        }
    });

    zoomOutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mapController.zoomOut();
        }
    });

    myLocationBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final GeoPoint myLocation = myLocationOverlay.getMyLocation();
            if (myLocation != null) {
                mapController.animateTo(myLocation);
                mapController.setCenter(myLocation);
            } else {
                progressDialog = ProgressDialog.show(GoogleMapActivity.this, "", getString(R.string.dlg_progress_obtaining_location), true);
            }
        }
    });

    mapController = this.mapView.getController();
    mapController.setZoom(MAP_ZOOM_LEVEL);

    final Intent intent = getIntent();
    isInRouteMode = intent.getBooleanExtra("isRouteMode", false);  //activity started to display route?
    if (isInRouteMode) {
        final String fromAddress = intent.getStringExtra("fromAddress");
        final String toAddress = intent.getStringExtra("toAddress");
        final GeoPoint fromPoint;
        final GeoPoint toPoint;
        try {
            fromPoint = getGeoPointByAddressString(fromAddress, this);
            toPoint = getGeoPointByAddressString(toAddress, this);
        } catch (IOException e) {
            Toast.makeText(this, getString(R.string.err_geocoder_not_available),
                    Toast.LENGTH_LONG).show();
            Log.e(CLASSTAG, e.getMessage());
            return;
        }
        new RouteCalculationTask().execute(fromPoint, toPoint);
    } else {  //activity started to select address
        addressItemizedOverlay = new AddressItemizedOverlay(getResources().getDrawable(R.drawable.red_pin));
        mapView.getOverlays().add(addressItemizedOverlay);
        mapView.getOverlays().add(myLocationOverlay);
        mapView.setOnTouchListener(new MapOnTouchListener());
    }
}

Priveous Activity 是只有四个按钮的简单 Activity ,所以我相信它不会减慢此 Activity 的速度。

最佳答案

您的观点是什么?

以前的 View /Activity/fragment 可能会减慢速度, map View 也可能会变慢,尤其是当 GPS 同时尝试获取锁定时

考虑一个加载对话框

关于android - setContentView 花费太多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12642034/

相关文章:

android-layout - Android 中带倒计时器的进度条

android - 如何从 MapView 中删除特定的 ItemizedOverlay?

java - Android:是否可以在 map View 的顶部绘制一个 View 作为覆盖

android - 如何在 Android 上创建照片库?

java - Matcher.replaceAll() 和 String.replaceAll() 似乎不适用于 Galaxy S 6

android - 在运行时构建布局 android

android - 为什么我的 map View 上的叠加层未显示?

android - 仅使用 mac 地址和密码在 android 应用程序上使用蓝牙

android - 增强现实

android - Android 中自定义 ListView 的 TableLayout 问题