0

2つのクラスを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 に答える