2014-10-10 74 views
0

我正在实施AR应用演示,它可以工作。但是我发现这个应用启动非常缓慢,我做了一些调试,发现它可能是由多次毁坏的“onSensorChanged”造成的。Android启动非常缓慢,因为onSensorChanged在onCreate方法中运行多次

有人可以帮我吗?提前致谢!

这是代码!

package com.example.mazhi_000.cameraapplication; 

import android.app.Activity; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.SurfaceView; 
import android.view.SurfaceHolder; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.hardware.Camera; 
import android.util.Log; 
import android.widget.TextView; 


public class CameraActivity extends Activity { 

    SurfaceView cameraPreview; 
    SurfaceHolder previewHolder; 
    Camera camera; 
    boolean inPreview; 

    final static String TAG = "CameraSurfaceView"; 
    SensorManager sensorManager; 

    int orientationSensor; 
    float headingAngle; 
    float pitchAngle; 
    float rollAngle; 

    int accelerometrSensor; 
    float xAxis; 
    float yAxis; 
    float zAxis; 

    LocationManager locationManager; 
    double latitude; 
    double longitude; 
    double altitude; 

    TextView xAxisValue; 
    TextView yAxisValue; 
    TextView zAxisValue; 
    TextView headingValue; 
    TextView pitchValue; 
    TextView rollValue; 
    TextView altitudeValue; 
    TextView latitudeValue; 
    TextView longitudeValue; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_camera); 

     // cache textview 
     xAxisValue = (TextView) findViewById(R.id.xAxisValue); 
     yAxisValue = (TextView) findViewById(R.id.yAxisValue); 
     zAxisValue = (TextView) findViewById(R.id.zAxisValue); 
     headingValue = (TextView) findViewById(R.id.headingValue); 
     pitchValue = (TextView) findViewById(R.id.pitchValue); 
     rollValue = (TextView) findViewById(R.id.rollValue); 
     altitudeValue = (TextView) findViewById(R.id.altitudeValue); 
     longitudeValue = (TextView) findViewById(R.id.longitudeValue); 
     latitudeValue = (TextView) findViewById(R.id.latitudeValue); 

     locationManager = (LocationManager)getSystemService(LOCATION_SERVICE); 
     Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if (location != null) { 
      latitude = location.getLatitude(); 
      longitude = location.getLongitude(); 
      altitude = location.getAltitude(); 

      Log.d(TAG, "Latitude: " + String.valueOf(latitude)); 
      Log.d(TAG, "longitude: " + String.valueOf(longitude)); 
      Log.d(TAG, "Altitude: " + String.valueOf(altitude)); 

      latitudeValue.setText(String.valueOf(latitude)); 
      longitudeValue.setText(String.valueOf(longitude)); 
      altitudeValue.setText(String.valueOf(altitude)); 

     } 
     locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 2000, 2, locationListener); 

     sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
     orientationSensor = Sensor.TYPE_ORIENTATION; 
     accelerometrSensor = Sensor.TYPE_ACCELEROMETER; 

     sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL); 
     sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor),SensorManager.SENSOR_DELAY_NORMAL); 

     inPreview = false; 

     cameraPreview = (SurfaceView)findViewById(R.id.cameraPreview); 
     previewHolder = cameraPreview.getHolder(); 
     previewHolder.addCallback(surfaceCallback); 
     previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    LocationListener locationListener = new LocationListener() 
    { 
     @Override 
     public void onLocationChanged(Location location) { 
      latitude = location.getLatitude(); 
      longitude = location.getLongitude(); 
      altitude = location.getAltitude(); 

      Log.d(TAG, "Latitude: " + String.valueOf(latitude)); 
      Log.d(TAG, "longitude: " + String.valueOf(longitude)); 
      Log.d(TAG, "Altitude: " + String.valueOf(altitude)); 

      latitudeValue.setText(String.valueOf(latitude)); 
      longitudeValue.setText(String.valueOf(longitude)); 
      altitudeValue.setText(String.valueOf(altitude)); 
     } 

     @Override 
     public void onStatusChanged(String s, int i, Bundle bundle) { 

     } 

     @Override 
     public void onProviderEnabled(String s) { 

     } 

     @Override 
     public void onProviderDisabled(String s) { 

     } 
    }; 

    final SensorEventListener sensorEventListener = new SensorEventListener() 
    { 
     @Override 
     public void onSensorChanged(SensorEvent sensorEvent) 
     { 
      if(sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) { 
       headingAngle = sensorEvent.values[0]; 
       pitchAngle = sensorEvent.values[1]; 
       rollAngle = sensorEvent.values[2]; 

       Log.d(TAG, "headingAngle: " + String.valueOf(headingAngle)); 
       Log.d(TAG, "pitchAngle: " + String.valueOf(pitchAngle)); 
       Log.d(TAG, "rollAngle: " + String.valueOf(rollAngle)); 

       headingValue.setText(String.valueOf(headingAngle)); 
       pitchValue.setText(String.valueOf(pitchAngle)); 
       rollValue.setText(String.valueOf(rollAngle)); 
      } 
      else 
      { 
       if(sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
       { 
        xAxis = sensorEvent.values[0]; 
        yAxis = sensorEvent.values[1]; 
        zAxis = sensorEvent.values[2]; 

        Log.d(TAG, "xAxis: " + String.valueOf(xAxis)); 
        Log.d(TAG, "yAxis: " + String.valueOf(yAxis)); 
        Log.d(TAG, "zAxis: " + String.valueOf(zAxis)); 

        xAxisValue.setText(String.valueOf(xAxis)); 
        yAxisValue.setText(String.valueOf(yAxis)); 
        zAxisValue.setText(String.valueOf(zAxis)); 
       } 
      } 
     } 

     @Override 
     public void onAccuracyChanged(Sensor sensor, int i) 
     { 
      // not used 
     } 
    }; 

    SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() 
    { 
     public void surfaceCreated(SurfaceHolder holder) 
     { 
      try 
      { 
       camera.setPreviewDisplay(previewHolder); 
      } 
      catch (Throwable t) 
      { 
       Log.e("ProAndroidAR2Activity", "Exception in setPreviewDisplay()", t); 
      } 
     } 

     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
     { 
      Camera.Parameters parameters = camera.getParameters(); 
      Camera.Size size = getBestPreviewSize(width, height, parameters); 

      if(size != null) 
      { 
       parameters.setPreviewSize(size.width, size.height); 
       camera.setParameters(parameters); 
       camera.startPreview(); 
       inPreview = true; 
      } 
     } 

     public void surfaceDestroyed(SurfaceHolder holder) 
     { 
      // not userd 
//   camera.stopPreview(); 
//   camera.release(); 
//   camera = null; 
     } 
    }; 

    private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) 
    { 
     Camera.Size result=null; 
     //Camera.Parameters p = parameters; 
     for (Camera.Size size : parameters.getSupportedPreviewSizes()) 
     { 
      if (size.width <= width && size.height <= height) 
      { 
       if (result==null) 
       { 
        result=size; 
       } 
       else 
       { 
        int resultArea = result.width * result.height; 
        int newArea = size.width * size.height; 

        if (newArea>resultArea) { 
         result=size; 
        } 
       } 
      } 
     } 
     return result; 
    } 

    @Override 
    public void onResume() 
    { 
     super.onResume(); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener); 
     sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL); 
     sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor), SensorManager.SENSOR_DELAY_NORMAL); 

     camera = Camera.open(); 
    } 

    @Override 
    public void onPause() 
    { 
     if(inPreview) 
     { 
      camera.stopPreview(); 
     } 

     locationManager.removeUpdates(locationListener); 
     sensorManager.unregisterListener(sensorEventListener); 

     camera.release(); 
     camera = null; 
     inPreview = false; 

     super.onPause(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.camera, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

布局:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <SurfaceView 
     android:id="@+id/cameraPreview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
    <TextView 
     android:id="@+id/xAxisLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="18dp" 
     android:layout_marginTop="15dp" 
     android:text="@string/xAxis" /> 
    <TextView 
     android:id="@+id/yAxisLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/xAxisLabel" 
     android:layout_below="@+id/xAxisLabel" 
     android:text="@string/yAxis" /> 
    <TextView 
     android:id="@+id/zAxisLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/yAxisLabel" 
     android:layout_below="@+id/yAxisLabel" 
     android:text="@string/zAxis" /> 
    <TextView 
     android:id="@+id/headingLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/zAxisLabel" 
     android:layout_below="@+id/zAxisLabel" 
     android:layout_marginTop="19dp" 
     android:text="@string/heading" /> 
    <TextView 
     android:id="@+id/pitchLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/headingLabel" 
     android:layout_below="@+id/headingLabel" 
     android:text="@string/pitch" /> 
    <TextView 
     android:id="@+id/rollLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/pitchLabel" 
     android:layout_below="@+id/pitchLabel" 
     android:text="@string/roll" /> 
    <TextView 
     android:id="@+id/latitudeLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/rollLabel" 
     android:layout_below="@+id/rollLabel" 
     android:layout_marginTop="34dp" 
     android:text="@string/latitude" /> 
    <TextView 
     android:id="@+id/longitudeLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/latitudeLabel" 
     android:layout_below="@+id/latitudeLabel" 
     android:text="@string/longitude" /> 
    <TextView 
     android:id="@+id/altitudeLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/longitudeLabel" 
     android:layout_below="@+id/longitudeLabel" 
     android:text="@string/altitude" /> 
    <TextView 
     android:id="@+id/xAxisValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/xAxisLabel" 
     android:layout_marginLeft="56dp" 
     android:layout_toRightOf="@+id/longitudeLabel" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/yAxisValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/yAxisLabel" 
     android:layout_alignBottom="@+id/yAxisLabel" 
     android:layout_alignLeft="@+id/xAxisValue" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/zAxisValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/headingLabel" 
     android:layout_alignLeft="@+id/yAxisValue" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/headingValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/headingLabel" 
     android:layout_alignBottom="@+id/headingLabel" 
     android:layout_alignLeft="@+id/zAxisValue" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/pitchValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/pitchLabel" 
     android:layout_alignBottom="@+id/pitchLabel" 
     android:layout_alignLeft="@+id/headingValue" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/rollValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/latitudeLabel" 
     android:layout_alignLeft="@+id/pitchValue" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/latitudeValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/latitudeLabel" 
     android:layout_alignLeft="@+id/rollValue" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/longitudeValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/longitudeLabel" 
     android:layout_alignBottom="@+id/longitudeLabel" 
     android:layout_alignLeft="@+id/latitudeValue" 
     android:text="@string/empty" /> 
    <TextView 
     android:id="@+id/altitudeValue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/altitudeLabel" 
     android:layout_alignBottom="@+id/altitudeLabel" 
     android:layout_alignLeft="@+id/longitudeValue" 
     android:text="@string/empty" /> 
</RelativeLayout> 

清单

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.mazhi_000.cameraapplication" > 

    <uses-sdk android:minSdkVersion="7"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".CameraActivity" 
      android:screenOrientation = "landscape" 
      android:configChanges="keyboardHidden|orientation" 
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 

      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

    <uses-feature android:name="android.hardware.camera"/> 
    <uses-permission android:name="android.permission.CAMERA"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

</manifest> 

回答

0

你的代码看起来OK,除非你不需要

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener); 
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(orientationSensor), SensorManager.SENSOR_DELAY_NORMAL); 
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(accelerometrSensor), SensorManager.SENSOR_DELAY_NORMAL); 
中的onCreate

,因为你拥有它的onResume。 另外onSensorChanged应该运行很多次:)尝试设置一些填充...

+0

是的,这就是为什么我困惑。我看着我的代码。没有什么可以推迟发布,因为它只是一个演示(不是太多的逻辑)。但在点击应用程序图标直到启动时它确实非常慢。你有什么想法吗? – max 2014-10-11 04:01:45