我如何从glView设置从子类
问题描述:
我有一个加载两个类成的FrameLayout视图中的主要活动标题栏的文本..我如何从glView设置从子类
//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);
启动我的传感器管理器,称为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新手,我看到很多人在tutorials/examples中嵌入他们的类。我个人不喜欢这么做,因为我认为这会让代码变得糟糕。但我认为,因为我将我的应用程序上下文从我的主要活动传递到了我的glView,然后再次传递给我的传感器管理器,我可以进行此调用?有人可以向我解释我可以吗?
EL
答
“大红线”下方法意思是:“该方法的setTitle(字符串)是未定义的类型上下文”。幸运的是,这可以通过将上下文转换为Activity来很快修复。
Activity activity = (Activity) context;
activity.setTitle("x: "+ xData);
+1
谢谢..我知道这意味着它不可用..但那就是我正在寻找! – erik 2012-02-20 00:50:54
你有什么错误吗? – Snicolas 2012-02-19 17:54:26
不,但多数民众赞成,因为我没有调用它呢..我问如何引用标题栏从phoneOrientation类(即sensorManager)我想我可以说context.setTitle(“你好”);但是setTitle在eclipse下面得到一个很大的红线,当我写这个.. – erik 2012-02-19 20:27:08