0
我是新來的android,並開始使用傳感器項目,在項目中,我無法在畫布上繪製文本當我使用 canvas.drawtext(「gary」, 200200,NULL); 我的應用程序意外停止,實際上我想要在畫布中獲取加速度計的值。 請在我的代碼中提示我最新的錯誤,並指導我通過在畫布上獲取值。在畫布上繪製文本並更新字符串值
package com.one2;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class accelerometer extends Activity implements SensorEventListener {
float x,y,z,sensorX ;
float sensorY,sensorZ;
Bitmap bmp;
SensorManager sm ;
MyBringBackSurface OurSurfaceView;
static final float NS2S = 1.0f/1000000000.0f;
float[] last_values = null;
float[] velocity = null;
float[] position = null;
long last_timestamp = 0;
public class MyBringBackSurface extends SurfaceView implements Runnable{
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public MyBringBackSurface(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause(){
isRunning = false;
while(true){
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
public void resume(){
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(23, 33, 444);
canvas.drawText("gary",200 ,200 , null);
float cx = canvas.getWidth()/2;
float cy = canvas.getHeight()/2;
float fx = cy+sensorY;
float fy = cx+sensorX;
canvas.drawBitmap(bmp,-fx ,-fy,null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
OurSurfaceView = new MyBringBackSurface(this);
OurSurfaceView.resume();
setContentView(OurSurfaceView);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!= 0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
x = y =z= sensorX = sensorY = sensorZ = 0;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
sm.unregisterListener(this);
super.onPause();
}
@Override
public void onSensorChanged(SensorEvent e) {
// TODO Auto-generated method stub
try {
Thread.sleep(16);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sensorX = e.values[0];
sensorY = e.values[1];
sensorZ= e.values[2];
if(last_values != null){
float dt = (e.timestamp - last_timestamp) * NS2S;
for(int index = 0; index < 3;++index){
velocity[index] += (e.values[index] + last_values[index])/2 * dt;
position[index] += velocity[index] * dt;
}
}
else{
last_values = new float[3];
velocity = new float[3];
position = new float[3];
velocity[0] = velocity[1] = velocity[2] = 0f;
position[0] = position[1] = position[2] = 0f;
}
System.arraycopy(e.values, 0, last_values, 0, 3);
last_timestamp = e.timestamp;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
09-05 09:21:11.241: E/AndroidRuntime(960): FATAL EXCEPTION: Thread-89
09-05 09:21:11.241: E/AndroidRuntime(960): java.lang.NullPointerException
09-05 09:21:11.241: E/AndroidRuntime(960): at android.graphics.Canvas.drawText(Canvas.java:1377)
09-05 09:21:11.241: E/AndroidRuntime(960): at com.one2.accelerometer$MyBringBackSurface.run(accelerometer.java:65)
09-05 09:21:11.241: E/AndroidRuntime(960): at java.lang.Thread.run(Thread.java:841)
請你告訴我如何加速計值傳遞到字符串值中的DrawText ... – 2014-09-05 13:56:43
你可以用'String.valueOf'轉換浮動值到'String'對象。 – cliffroot 2014-09-05 13:58:27
將傳感器值更新從浮動到字符串。或我應該使用任何其他方法。 – 2014-09-05 14:26:07