zoukankan      html  css  js  c++  java
  • 安卓传感器开发之指南针

    代码很简单,主要是一些常用传感器的监听,指南针还是挺好用的。


    布局代码

    <?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="#fff"
    	>
    <ImageView
    	android:id="@+id/znzImage"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:scaleType="fitCenter"
    	android:src="@drawable/compass" />
    </LinearLayout>



    程序代码:

    package zhinanzheng.com;
    
    import android.app.Activity;
    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;
    /**
     * @author coderwry 653866417@qq.com
     * @version  1.0
     */
    public class Zhinanzheng extends Activity implements SensorEventListener{
    	
    	ImageView image;  //指南针图片
    	float currentDegree = 0f; //指南针图片转过的角度
    	
    	SensorManager mSensorManager; //管理器
    	
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            image = (ImageView)findViewById(R.id.znzImage);
            mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); //获取管理服务
        }
        
        @Override 
        protected void onResume(){
        	super.onResume();
        	//注册监听器
        	mSensorManager.registerListener(this
        			, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
        }
        
        //取消注册
        @Override
        protected void onPause(){
        	mSensorManager.unregisterListener(this);
        	super.onPause();
        	
        }
        
        @Override
        protected void onStop(){
        	mSensorManager.unregisterListener(this);
        	super.onStop();
        	
        }
    
        //传感器值改变
    	@Override
    	public void onAccuracyChanged(Sensor sensor, int accuracy) {
    		// TODO Auto-generated method stub
    		
    		
    	}
        //精度改变
    	@Override
    	public void onSensorChanged(SensorEvent event) {
    		// TODO Auto-generated method stub
    		//获取触发event的传感器类型
    		int sensorType = event.sensor.getType();
    		
    		switch(sensorType){
    		case Sensor.TYPE_ORIENTATION:
    			float degree = event.values[0]; //获取z转过的角度
    			//穿件旋转动画
    			RotateAnimation ra = new RotateAnimation(currentDegree,-degree,Animation.RELATIVE_TO_SELF,0.5f
    					,Animation.RELATIVE_TO_SELF,0.5f);
    		 ra.setDuration(100);//动画持续时间
    		 image.startAnimation(ra);
    		 currentDegree = -degree;
    		 break;
    		
    		}
    	}
    }

    效果图


  • 相关阅读:
    OGRE 3D 1.7 Beginner‘s Guide中文版 第一章
    一个人的成功取决于晚上的8点至10点--经典语录必读
    学历代表过去、能力代表现在、学习力代表未来
    理财达人五步走
    Ogre场景、节点、摄像机通过自动、鼠标、键盘控制移动
    QT按钮背景颜色设置及文字显示位置设置
    Qt一个工程调用另一个工程的类成员变量
    C++搜索字符串中的汉字
    Q窗口操作函数(窗口最大化,全屏,隐藏最大化最小化按钮)
    PAT(Advance Level)Practice1001
  • 原文地址:https://www.cnblogs.com/emoji/p/4436847.html
Copyright © 2011-2022 走看看