Android系统的指南针小应用
来源:互联网 发布:tomcat绑定域名 编辑:程序博客网 时间:2024/04/30 14:14
思路:一般智能手机上都会有方向传感器用于感应设计设备的摆放状态。方向传感器可以返回三个角度,这三个角度即可确定手机的摆放状态。
其中返回的第一个角度表示手机顶部朝向与正北方的夹角。当着手机绕着Z轴旋转是,该角度的值发生改变。当该角度为0是表明手机顶部朝向整备;该角度为90度是,代表手机顶部朝向正东,依次类推。故先准备一张指南图片,该图片上方向指针指向北方,接下来开发一个检测方向的传感器,程序检测到手机顶部绕Z轴转过多少度,让指南针图片反向转过多少度即可。
需要准备:一张指南针图片
XML文件配置:
<?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:background="#000000"><ImageView android:id="@+id/znzImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitCenter" android:src="@drawable/znz" /></LinearLayout>
Activity文件:
package com.example.compass1;import android.app.Activity;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.media.ImageReader;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.widget.ImageView;/** * * @author duhuan * */public class MainActivity extends Activity implements SensorEventListener {//定义指南针的图片private ImageView image;//定义指南针转过的角度private float currentDegree= 0f;//定义Senor管理器private SensorManager mSensorManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取界面中显示指南针的图片image=(ImageView)findViewById(R.id.znzImage);//获取传感器管理器服务mSensorManager =(SensorManager)getSystemService(SENSOR_SERVICE);}@Overrideprotected void onResume() {super.onResume();//为系统的管理器注册监听器mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);}@Overrideprotected void onStop() {//取消为系统设置的监听器mSensorManager.unregisterListener(this);super.onStop();}@Overridepublic void onSensorChanged(SensorEvent event) {// 获取触发event的传感器类型int sensorType = event.sensor.getType();switch(sensorType){case 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);//运行动画image.startAnimation(ra);currentDegree=-degree;break;}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}}
0 0
- Android系统的指南针小应用
- Android指南针应用
- 指南针的应用
- Android 电子罗盘 --指南针(方向传感器的应用)
- Android 电子罗盘--指南针(方向传感器的应用)
- Android 电子罗盘--指南针(方向传感器的应用)
- Android 电子罗盘 --指南针(方向传感器的应用)
- 猫猫学iOS 之CoreLocation指南针小应用
- Android指南针程序的实现
- android开发(28) 做个 指南针 应用
- wp8中指南针传感器的应用
- android 指南针
- android指南针
- android指南针
- Android 指南针
- AJ学IOS 之CoreLocation指南针小应用
- Android传感器介绍及指南针的实现
- Android传感器介绍及指南针的实现
- 程序异常导致奔溃
- String工具类StringHelper
- MSDN: Limitations on Raw Sockets
- memcached安装及使用
- web应用json转换引起jar包冲突的问题及解决方法
- Android系统的指南针小应用
- mysql时间转化
- (总结)CentOS Linux搭建SVN Server配置详解
- SNMP学习之Snmp4j
- Maven与Ant比较
- Struts2不扫描jar包中的action
- contendprovider
- UnOnline.cpp
- JS Selectors API