在画布上绘制文本并更新字符串值

时间:2014-09-05 13:51:53

标签: android canvas

我是android的新手,并开始使用传感器项目,在这个项目中,当我使用时,我无法在画布上绘制文字 canvas.drawtext("加里",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)

1 个答案:

答案 0 :(得分:1)

Canvas.drawText的第4个参数必须是Paint个对象。您无法通过null。所以你应该创建一个新对象并传递它:

Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawText("gary",200,200,paint);