中博教育南京中博培训学校招生官方网站

中博教育全国示范校区 全国就业基地 IT职业教育和谐品牌 软件工程师培训基地

安卓培训:Android利用Sensor实现指南针功能

时间:2013-05-11 16:18来源:未知 作者:中博IT教育 点击:

step2:设计应用的UI界面,main.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ?xml version= 1.0 encoding= utf-8 ? LinearLayout xmlns:android= http://schemas.android.com/apk/res/android android:orientation= vertical android:layout_width= fill_pare

step2:设计应用的UI界面,main.xml

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

step3:MainActivity.java

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
81
82
83
84
85
86
87
package cn.roco.sensor;
 
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
 
public class MainActivity extends Activity {
 
    private ImageView imageView;
 
    /** 传感器管理器 */
    private SensorManager manager;
 
    private SensorListener listener = new SensorListener();
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        imageView = (ImageView) this.findViewById(R.id.imageView);
        imageView.setKeepScreenOn(true);//屏幕高亮
        //获取系统服务(SENSOR_SERVICE)返回一个SensorManager 对象
        manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }
 
    @Override
    protected void onResume() {
        /**
         *  获取方向传感器
         *  通过SensorManager对象获取相应的Sensor类型的对象
         */
        Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        //应用在前台时候注册监听器
        manager.registerListener(listener, sensor,
                SensorManager.SENSOR_DELAY_GAME);
        super.onResume();
    }
 
    @Override
    protected void onPause() {
        //应用不在前台时候销毁掉监听器
        manager.unregisterListener(listener);
        super.onPause();
    }
 
    private final class SensorListener implements SensorEventListener {
 
        private float predegree = 0;
 
        @Override
        public void onSensorChanged(SensorEvent event) {
            /**
             *  values[0]: x-axis 方向加速度
               values[1]: y-axis 方向加速度
               values[2]: z-axis 方向加速度
             */
            float degree = event.values[0];// 存放了方向值
            /**动画效果*/
            RotateAnimation animation = new RotateAnimation(predegree, degree,
                    Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            animation.setDuration(200);
            imageView.startAnimation(animation);
            predegree=-degree;
             
            /**
            float x=event.values[SensorManager.DATA_X];
            float y=event.values[SensorManager.DATA_Y];
            float z=event.values[SensorManager.DATA_Z];
            Log.i("XYZ", "x="+(int)x+",y="+(int)y+",z="+(int)z);
            */
        }
 
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
 
        }
 
    }
}

step4:AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.roco.sensor"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
 
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
    </application>
</manifest>
(责任编辑:中博IT教育)
本文标签:
分享到:
顶一下
(0)
0%
踩一下
(0)
0%
在线咨询
在线报名
  • 姓  名:*
  • 年  龄:*
  • 电  话:*
  • 地  址:*
  • 开班信息
  • 讲座试听
12-21java系统班限招
12-2java系统班限招
11-25java系统班热招
11-16网络营销班限招
10-30就业定制班热招

时 间:周日上午10:00
主 题:专业技术讲座
主讲人:中博教育技术讲师
电 话:025-84200066
说 明:限30人 电话订座