Android指南针小程序源代码 -----------------Compass.java-------------------------------
l;
import android.app.Activity;
t.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
public class Compass extends Activity {
final static int MENU_QUIT = 1;
CompassView view;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
//setContentView(R.layout.main);
//表面屏幕自动翻转导致的错位问题
//效果与l 设置android:screenOrientation 属性一样
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
/
/setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
WindowManager windowManager = getWindowManager();
Display display = DefaultDisplay();
int screenWidth = Width();
int screenHeight = Height();
//LocationManager myLocationManager=null;
//myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//Location l = LastKnownLocation("network");
//String url = l.getLatitude() + "," + l.getLongitude(); 这里获取到了精度和维度
view = new CompassView(this,screenWidth,screenHeight); 
setContentView(view); 
}
/* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//CreateOptionsMenu(menu);
//menu.add(0, MENU_NEW_GAME, 0, "New Game");
menu.add(0, MENU_QUIT, 0, "退出");
return true;
}
/* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//OptionsItemSelected(item);
switch (ItemId()) {
//case MENU_NEW_GAME:     
// newGame();     
/
/ return true; 
case MENU_QUIT:     
finish();
return true; 
return false;
}
//
//activity的卸载点,不要在这里保存东西,调用了finish()之后会来到这里
//
@Override
protected void onDestroy() {
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
}
@Override
protected void onRestart() {
/
/ TODO Auto-generated method stub
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
}
@Override
protected void onStop(
) {
// TODO Auto-generated method stub
}
}
----------------------CompassView.java-----------------------------------
l;
t.Context;
aphics.Bitmap;
aphics.BitmapFactory;
安卓课程设计源代码aphics.Canvas;
aphics.Color;
aphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CompassView extends SurfaceView implements SurfaceHolder.Callback,
SensorEventListener {
SurfaceHolder mholder;
Bitmap background1;
SensorManager sensorManager;
Sensor orieSensor;
int screenWidth =0;
int screenHeight =0;
float degree = 0;
private static final Object LOCK = new Object();
public CompassView(Context context, int screenWidth, int screenHeight) {
super(context);
background1 = BitmapFactory.Resources(),
R.drawable.back1);
getHolder().addCallback(this);
this.screenWidth = screenWidth; // Width();
this.screenHeight = screenHeight; // Height();
sensorManager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
orieSensor = DefaultSensor(Sensor.TYPE_ORIENTATION);
if (orieSensor != null) {
SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
synchronized (LOCK) {
mholder = holder;
}
// PaintThread thread= new PaintThread();
// thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
synchronized (LOCK) {
mholder = null;
}
}
public void removeSensorListener() {
sensorManager.unregisterListener(this);
}
// class PaintThread extends Thread{
// public void run() {
//       
// }
// }
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
// event.timestamp The time in nanosecond at which the event
// happened
if (Math.abs(event.values[1]) > 32 || // 围绕x轴的偏转(-180 to 180)
Math.abs(event.values[2]) > 20) { // 围绕y轴的偏转 (-90 to 90),
// event.values[0] Azimuth, angle between the magnetic north
// direction and the Y axis, around the Z axis (0 to 359). 0=North,
// 90=East, 180=South, 270=West
synchronized (LOCK) {
if (mholder != null) {
Canvas can = mholder.lockCanvas();
if (can == null)
return;
can.drawRGB(0, 0, 0);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GREEN);
can.drawText("我的指南针 by widebright", 20, 20, paint);
can.drawText("你的屏幕要水平放置!", 20,
screenHeight / 2, paint);
mholder.unlockCanvasAndPost(can);
}
}
} else {
synchronized (LOCK) {
if (mholder != null &&
(Math.abs (event.values[0] - degree) > 0.4)) { //避免屏幕不动时重复绘制
degree = event.values[0];
Canvas can = mholder.lockCanvas();
if (can == null)
return;
can.drawRGB(0, 0, 0);
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GREEN);
can.drawText("我的指南针", 20, 20, paint);
// can.drawLine(0, 0, 100, 100, paint);
can.save();
screenHeight / 2);
can.drawBitmap(background1, screenWidth / 2
- Width() / 2, screenHeight / 2
-
Height() / 2, null);
mholder.unlockCanvasAndPost(can);
}
}
}
}
} #Java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。