0

我有一个将两个类加载到 FrameLayout 视图中的 Main 活动。

    //Create Intance of Camera
        camPreview = new CamLayer(this.getApplicationContext());

        //Create Instance of OpenGL
        glView = new GLLayer(this);

        //FrameLayOut for holding everything
        FrameLayout frame = new FrameLayout(this);
        // set as main view
        setContentView(frame);

        // add Camera to view 
        frame.addView(camPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        frame.addView(glView);

从 glView 我启动了我的传感器管理器,称为 PhoneOrientation,如下所示:

    public GLLayer(Context context) {
    super(context);

    this.context = context;
    this.square = new Square();
    phoneOri=new PhoneOrientation(context); // sensor manager and interpreter

    // settings for translucent glView
    this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    this.getHolder().setFormat(PixelFormat.TRANSLUCENT);

    // set render to inline 
    this.setRenderer(this);
    phoneOri.start(context);

}

我的 phoneOrientation 类:

    public class PhoneOrientation {
private SensorManager sensorMan;
private Sensor sensorAcce;
private Sensor sensorMagn;
private SensorEventListener listener;
private float matrix[]=new float[16];
private Context ctx;

public PhoneOrientation(Context context) {
    ctx = context;
}

public void start(Context context) {
    listener = new SensorEventListener() {
        private float orientation[]=new float[3];
        private float acceleration[]=new float[3];

        public void onAccuracyChanged(Sensor arg0, int arg1){}

        public void onSensorChanged(SensorEvent evt) {
            int type=evt.sensor.getType();

            //Smoothing the sensor data a bit seems like a good idea.
            if (type == Sensor.TYPE_MAGNETIC_FIELD) {
                orientation[0]=(orientation[0]*1+evt.values[0])*0.5f;
                orientation[1]=(orientation[1]*1+evt.values[1])*0.5f;
                orientation[2]=(orientation[2]*1+evt.values[2])*0.5f;
            } else if (type == Sensor.TYPE_ACCELEROMETER) {
                acceleration[0]=(acceleration[0]*2+evt.values[0])*0.33334f;
                acceleration[1]=(acceleration[1]*2+evt.values[1])*0.33334f;
                acceleration[2]=(acceleration[2]*2+evt.values[2])*0.33334f;
            }
            if ((type==Sensor.TYPE_MAGNETIC_FIELD) || (type==Sensor.TYPE_ACCELEROMETER)) {
                float newMat[]=new float[16];
                //Toast toast = Toast.makeText(ctx.getApplicationContext(), "accel", Toast.LENGTH_SHORT);
                //toast.show();
                SensorManager.getRotationMatrix(newMat, null, acceleration, orientation);
                SensorManager.remapCoordinateSystem(newMat,
                        SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                        newMat);
                matrix=newMat;
            }
        }
    };

    sensorMan = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    sensorAcce = sensorMan.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    sensorMagn = sensorMan.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).get(0);

    sensorMan.registerListener(listener, sensorAcce, SensorManager.SENSOR_DELAY_FASTEST);
    sensorMan.registerListener(listener, sensorMagn, SensorManager.SENSOR_DELAY_FASTEST);       
}

public float[] getMatrix() {
    return matrix;
}

public void finish() {
    sensorMan.unregisterListener(listener);
}

}

我的 phoniOrientationClass 本质上是我的传感器管理器,从那里我想将一些传感器数据吐回我的应用程序的标题栏,即。

    context.setTitle("x: "+ xData); // or something?  

不过,我似乎无法从那里拨打这个电话?我是 java 新手,我看到很多人在教程/示例中内联他们的类。我个人不喜欢做那么多,因为我相信它会使代码变得混乱。但我想,既然我将我的应用程序上下文从我的主要活动传递给我的 glView,然后再传递给我的传感器管理器,我就可以进行这个调用?有人可以向我解释我该怎么做吗?

埃尔

4

1 回答 1

1

方法下方的“大红线”表示:“方法 setTitle(String) 未定义类型 Context”。幸运的是,这可以通过将上下文转换为 Activity 来快速修复。

Activity activity = (Activity) context;
activity.setTitle("x: "+ xData);
于 2012-02-19T22:54:51.230 回答