Android 电子罗盘--指南针(方向传感器的应用)

转载自:

http://blog.csdn.net/onlyonecoder/article/details/8475653

图片神马的在网上搜个指南针图片就好了,本来没有注释,发上来之后感觉不行,还是加上吧,方便学习



main.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:gravity="center"  
  7.     >  
  8.   
  9.     <ImageView  
  10.         android:id="@+id/compass_imageView"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:src="@drawable/compass" />  
  14.   
  15. </LinearLayout>  


compass.Activity

[java]  view plain  copy
  1.   
[java]  view plain  copy
  1. import android.app.Activity;  
  2. import android.hardware.Sensor;  
  3. import android.hardware.SensorEvent;  
  4. import android.hardware.SensorEventListener;  
  5. import android.hardware.SensorManager;  
  6. import android.os.Bundle;  
  7. import android.view.animation.Animation;  
  8. import android.view.animation.RotateAnimation;  
  9. import android.widget.ImageView;  
  10.   
  11. /** 
  12.  * 电子罗盘 方向传感器 
  13.  */  
  14. public class ComPassActivity extends Activity implements SensorEventListener {  
  15.     private ImageView imageView;  
  16.     private float currentDegree = 0f;  
  17.   
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.compass);  
  21.         imageView = (ImageView) findViewById(R.id.compass_imageView);  
  22.   
  23.         // 传感器管理器  
  24.         SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);  
  25.         // 注册传感器(Sensor.TYPE_ORIENTATION(方向传感器);SENSOR_DELAY_FASTEST(0毫秒延迟);  
  26.         // SENSOR_DELAY_GAME(20,000毫秒延迟)、SENSOR_DELAY_UI(60,000毫秒延迟))  
  27.         sm.registerListener(ComPassActivity.this,  
  28.                 sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),  
  29.                 SensorManager.SENSOR_DELAY_FASTEST);  
  30.   
  31.     }  
  32.     //传感器报告新的值(方向改变)  
  33.     public void onSensorChanged(SensorEvent event) {  
  34.         if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {  
  35.             float degree = event.values[0];  
  36.               
  37.             /* 
  38.             RotateAnimation类:旋转变化动画类 
  39.               
  40.             参数说明: 
  41.  
  42.             fromDegrees:旋转的开始角度。 
  43.             toDegrees:旋转的结束角度。 
  44.             pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
  45.             pivotXValue:X坐标的伸缩值。 
  46.             pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。 
  47.             pivotYValue:Y坐标的伸缩值 
  48.             */  
  49.             RotateAnimation ra = new RotateAnimation(currentDegree, -degree,  
  50.                     Animation.RELATIVE_TO_SELF, 0.5f,  
  51.                     Animation.RELATIVE_TO_SELF, 0.5f);  
  52.             //旋转过程持续时间  
  53.             ra.setDuration(200);  
  54.             //罗盘图片使用旋转动画  
  55.             imageView.startAnimation(ra);  
  56.               
  57.             currentDegree = -degree;  
  58.         }  
  59.     }  
  60.     //传感器精度的改变  
  61.     public void onAccuracyChanged(Sensor sensor, int accuracy) {  
  62.   
  63.     }  
  64. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值