重力小球

package com.android.mypro;

import android.R.integer;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class GBall extends SurfaceView implements Callback, Runnable,
		SensorEventListener {

	private static final int TIME_IN_FRAME = 50;
	private Paint ballPaint = null;
	private SurfaceHolder surfaceHolder = null;
	private boolean running = false;
	private Canvas canvas = null;
	private SensorManager mSensorManager = null;
	private Sensor sensor = null;
	private int mWidth = 0;
	private int mHeight = 0;
	private int cx = 100;
	private int cy = 100;
	private int radius = 30;

	private float gx = 0;
	private float gy = 0;
	private float gz = 0;

	private Thread thread = null;

	private static final String TAG = "GBall";

	public GBall(Context context, SensorManager sensorManager) {
		super(context);
		// TODO Auto-generated constructor stub
		// get surfaceHolder
		surfaceHolder = this.getHolder();
		// add callback function
		surfaceHolder.addCallback(this);
		// init paint
		ballPaint = new Paint();
		ballPaint.setAntiAlias(true);
		ballPaint.setColor(Color.BLACK);

		thread = new Thread(this);
		mSensorManager = sensorManager;
		sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mSensorManager.registerListener(this, sensor,
				SensorManager.SENSOR_DELAY_GAME);
	}

	public void unregisterSensor() {
		mSensorManager.unregisterListener(this);
	}
	
	public void stopThread() {
		
		try {
			this.running = false;
			thread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void revise() {
		if (cx <= radius) {
			cx = radius;
			sx = -sx/2;
		} else if (cx >= (mWidth - radius)) {
			cx = mWidth - radius;
			sx = -sx/2;
		}

		if (cy <= radius) {
			cy = radius;
			sy = -sy/2;
		} else if (cy >= (mHeight - radius)) {
			cy = mHeight - radius;
			sy = -sy/2;
		}
	}

	protected void myDraw() {
		canvas = surfaceHolder.lockCanvas();
		// set screen to white
		canvas.drawColor(Color.WHITE);
		revise();
		canvas.drawCircle(cx, cy, radius, ballPaint);
		surfaceHolder.unlockCanvasAndPost(canvas);

	}
	
	private int sx = 0;
	private int sy = 0;

	public void run() {
		// TODO Auto-generated method stub
		while(running) {
			cx = cx + sx;
			cy = cy + sy;
			myDraw();
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		Log.d(TAG, "surfaceChanged");
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Log.d(TAG, "surfaceCreated");
		mWidth = getWidth();
		mHeight = getHeight();
		//+++++set background to white
		canvas = surfaceHolder.lockCanvas();
		canvas.drawColor(Color.WHITE);
		surfaceHolder.unlockCanvasAndPost(canvas);
		//-----
		running = true;
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		Log.d(TAG, "surfaceDestroyed");
		stopThread();
		unregisterSensor();
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		gx = event.values[SensorManager.DATA_X];
		gy = event.values[SensorManager.DATA_Y];
		gz = event.values[SensorManager.DATA_Z];
		sx += -(int)gx/2;
		sy += (int)gy/2;
//		Log.d(TAG, "" + gx + " " + gy + " " + gz);
	}

}

你可能感兴趣的:(重力小球)