注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

给我的Android手机做个指南针小程序(源代码)  

2010-08-28 00:12:44|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

貌似网上有人做个“超级指南针”还挺热门,不过好像有广告。

去 Android 官方系统下载SDK回来,装好Eclipse的SDK就可以开始了。

自带文档是比较详细的了,Eclipse的插件也比较完善,支持可视化编辑layout下面的界面xml文件,自动编译生成apk安装包,调试与启动都自动在模拟器中完美支持。

配置"run configuration"或者"debug configuration“ 指定传给模拟器的参数 “ -avd Milestone -scale 0.6“   以便采用自己建立的虚拟设备启动。注意的是, 程序看到屏幕的像素不一定是和物理像素一样的,

所以最好不要在程序中最好不要使用固定像素的绘制办法。
自己看“develop guide中的Supporting Multiple Screens”一文吧
(Android SDK\android-sdk-windows\docs\guide\practices\screens_support.html)

其他的就不说,看文档吧。

给我的Android手机做个指南针小程序(源代码) - widebright - widebright的个人空间

给我的Android手机做个指南针小程序(源代码) - widebright - widebright的个人空间

程序在我的手机上运行起来还不错,暂时没有发现什么大问题。

早上又改了一点点,应该不会有什么问题吧,回去再试一下。再这里再鄙视一下公司,电脑竟然在bios里面禁用了usb功能了!!!!代码很简单:

-----------------Compass.java-------------------------------

package widebright.tool;

import android.app.Activity;
import android.content.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) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
       
        //表面屏幕自动翻转导致的错位问题
        //效果与AndroidManifest.xml 设置android:screenOrientation 属性一样
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
       
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
      
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();
        //LocationManager myLocationManager=null;
        //myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
     //Location l = myLocationManager.getLastKnownLocation("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) {
   //return super.onCreateOptionsMenu(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) {
   //return super.onOptionsItemSelected(item);
     switch (item.getItemId()) {
     //case MENU_NEW_GAME:       
     // newGame();       
     // return true;   
     case MENU_QUIT:       
     finish();
        return true;   
     }   
     return false;
}
   
//
//activity的卸载点,不要在这里保存东西,调用了finish()之后会来到这里
//
@Override
protected void onDestroy() {
   super.onDestroy();
    view.removeSensorListener();
   
}

@Override
protected void onPause() {
   // TODO Auto-generated method stub
   super.onPause();
}

@Override
protected void onRestart() {
   // TODO Auto-generated method stub
   super.onRestart();
}

@Override
protected void onResume() {
   // TODO Auto-generated method stub
   super.onResume();
}

@Override
protected void onStart() {
   // TODO Auto-generated method stub
   super.onStart();
}

@Override
protected void onStop() {
   // TODO Auto-generated method stub
   super.onStop();
}
}

----------------------CompassView.java-----------------------------------

package widebright.tool;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.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.decodeResource(this.getResources(),
     R.drawable.back1);
   getHolder().addCallback(this);
   this.screenWidth = screenWidth; // this.getWidth();
   this.screenHeight = screenHeight; // this.getHeight();

   sensorManager = (SensorManager) context
     .getSystemService(Context.SENSOR_SERVICE);
   orieSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

   if (orieSensor != null) {
    sensorManager.registerListener(this, orieSensor,
      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();
      can.rotate(-1 * event.values[0], screenWidth / 2,
        screenHeight / 2);
      can.drawBitmap(background1, screenWidth / 2
        - background1.getWidth() / 2, screenHeight / 2
        - background1.getHeight() / 2, null);
      can.restore();
      mholder.unlockCanvasAndPost(can);

     }
    }

   }
}

}

  评论这张
 
阅读(2441)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017