Android 在compass中添加文本视图
我得申请一个罗盘指示西点的地方。 这是我的密码Android 在compass中添加文本视图,android,textview,compass-geolocation,Android,Textview,Compass Geolocation,我得申请一个罗盘指示西点的地方。 这是我的密码 public class CustomCompassActivity extends Activity implements SensorEventListener { Float azimut; // View to draw a compass public class CustomDrawableView extends View { Paint paint = new Paint(); TextView t1; public Cus
public class CustomCompassActivity extends Activity implements SensorEventListener {
Float azimut; // View to draw a compass
public class CustomDrawableView extends View {
Paint paint = new Paint();
TextView t1;
public CustomDrawableView(Context context) {
super(context);
paint.setColor(0x8a2be2);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(4);
paint.setAntiAlias(true);
};
protected void onDraw(Canvas canvas) {
int width = getWidth();
int height = getHeight();
int centerx = width/2;
int centery = height/2;
// Rotate the canvas with the azimut
if (azimut != null)
canvas.rotate(-azimut*360/(2*3.14159f), centerx, centery);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.rose4);
canvas.drawBitmap(bmp,centerx- bmp.getWidth()/2,centery - bmp.getHeight()/2, paint);
canvas.drawText("W", centerx-0, centery-55, paint);
paint.setColor(0xffd02090);
}
}
CustomDrawableView mCustomDrawableView;
private SensorManager mSensorManager;
Sensor accelerometer;
Sensor magnetometer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCustomDrawableView = new CustomDrawableView(this);
setContentView(mCustomDrawableView); // Register the sensor listeners
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
}
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
float[] mGravity;
float[] mGeomagnetic;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
azimut = orientation[0]; // orientation contains: azimut, pitch and roll
}
}
mCustomDrawableView.invalidate();
}
}
我需要添加textview。但我不能在程序中添加xml或使用textview。我如何在指南针的上层添加textview?当您说“既不能在程序中添加xml也不能使用textview”时,您是什么意思。为什么不呢?我不知道。我试图从Id调用的
main
视图显示textview。它不起作用。在我用setText
尝试textview之后。它仍然显示强制关闭使用textview是相当基本的。确保您以正确的方式创建它(例如谷歌代码)。另外,我建议在需要添加textview的地方使用XMLView?我尝试将textview放置在程序的不同位置。它在运行后总是显示错误。在布局中。例如谷歌。android开发者文档是一个非常好的起点。