Chinaunix首页 | 论坛 | 博客
  • 博客访问: 106889
  • 博文数量: 60
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-09 12:19
文章分类

全部博文(60)

文章存档

2015年(3)

2014年(41)

2013年(16)

我的朋友

分类: Android平台

2014-07-28 14:13:54

前段时间,有一位网友发私信给我(@伍歌),问我做过磁场传感器可以做过指南针吗?其实我第一节里面已经说过了,磁场传感器可以做,只是算法比较麻烦,最简单的指南针使用方向传感器做出,但是由于工作关系,一直没有来得及帮助他,现在就写一份简单指南针教程吧,先贴图:

.

布局文件很简单,就一张指南针的平面图片。

1

算法第一节里面也说过了,values[0]:该值表示方位,也就是手机绕着Z轴旋转的角度。 0表示北(North);90表示东(East);180表示南(South);270表示西(West)。如果values[0]的值正好是这4个值,并且手机是水平放置,表示手机的正前方就是这4个方向。可以利用这个特性来实现电子罗盘。如果还有什么疑问请看第一节内容。


具体方法代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void onSensorChanged(SensorEvent event) {
  
        // 如果真机上触发event的传感器类型为水平传感器类型
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            // 获取绕Z轴旋转的角度
            float degree = event.values[0];
            // 创建旋转动画(反向转过degree度)
            RotateAnimation ra = new RotateAnimation(currentDegree, -degree, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
            // 设置动画的持续时间
            ra.setDuration(200);
            // 设置动画结束后的保留状态
            ra.setFillAfter(true);
            // 启动动画
            image.startAnimation(ra);
            currentDegree = -degree;
        }
  
    }
思路就是获取了values[0],根据values[0]的值去旋转图片。所有代码如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class OrientationActivity extends Activity implements
        SensorEventListener {
  
    public static final String TAG = "OrientationActivity方向传感器";
  
    private TextView tv_context;
  
    private Sensor mAccelerometer;
  
    private SensorManager mSensorManager;
    // 记录指南针图片转过的角度
    private float currentDegree = 0f;
  
    private ImageView image;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
  
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_orien);
        infoViews();// 初始化控件
  
    }
  
    private void infoViews() {
  
        // btn = (Button) findViewById(R.id.btn_sensor);
        tv_context = (TextView) findViewById(R.id.tv_context);
        tv_context.setText("指南针");
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mAccelerometer = mSensorManager
                .getDefaultSensor(Sensor.TYPE_ORIENTATION);
        image = (ImageView) findViewById(R.id.main_iv);
    }
  
    @Override
    protected void onResume() {
  
        if (mAccelerometer != null) {
            mSensorManager.registerListener(this, mAccelerometer,
                    SensorManager.SENSOR_DELAY_NORMAL);
            Toast.makeText(getApplicationContext(), "此设备有方向传感器", 0).show();
        } else {
            Toast.makeText(getApplicationContext(), "此设备没有方向传感器", 0).show();
        }
  
        super.onResume();
    }
  
    protected void onPause() {
  
        super.onPause();
        mSensorManager.unregisterListener(this);
    }
  
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
    }
  
    public void onSensorChanged(SensorEvent event) {
  
        // 如果真机上触发event的传感器类型为水平传感器类型
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            // 获取绕Z轴旋转的角度
            float degree = event.values[0];
            // 创建旋转动画(反向转过degree度)
            RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            // 设置动画的持续时间
            ra.setDuration(200);
            // 设置动画结束后的保留状态
            ra.setFillAfter(true);
            // 启动动画
            image.startAnimation(ra);
            currentDegree = -degree;
        }
  
    }
}

很简单,但是如果我们需要优化的话,就需要调用Criteria这个类去加载location信息:


LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);//设置为最大精度
criteria.setAltitudeRequired(false);//不要求海拔信息
criteria.setBearingRequired(false);//不要求方位信息
criteria.setCostAllowed(true);//是否允许付费
criteria.setPowerRequirement(Criteria.POWER_LOW);//对电量的要求

location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));

然后去写location:


  1. LocationListener location= new LocationListener() {
  2. @Override
  3. public void onStatusChanged(String provider, int status, Bundle extras) {
  4. if (status != LocationProvider.OUT_OF_SERVICE) {
  5. updateLocation(mLocationManager
  6. .getLastKnownLocation(mLocationProvider));
  7. } else {
  8. mLocationTextView.setText(R.string.cannot_get_location);
  9. }
  10. }
  11. @Override
  12. public void onProviderEnabled(String provider) {
  13. }
  14. @Override
  15. public void onProviderDisabled(String provider) {
  16. }
  17. @Override
  18. public void onLocationChanged(Location location) {
  19. updateLocation(location);// 更新位置
  20. }
  21. };
  22. }
阅读(870) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~