android 方向传感器应用

1.首先我们创建一个传感器管理器和一个传感器监听器,管理器用来管理传感器以及创建各种各样的传感器,监听器用来监视传感器的变化并且进行相应的操作

private SensorManager manager; 
private SensorListener listener = new SensorListener();

2、这个监听器当然是我们自己定义的,在方向感应器感应到手机方向有变化的时候,我们可以采取相应的操作,这里紧紧是将x,y,z的值打印出来

private final class SensorListener implements SensorEventListener { 
private float predegree = 0; 
@Override 
public void onSensorChanged(SensorEvent event) { 
    /** 
    * values[0]: x-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; 
    } 
    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    } 
}

    /** 
*/ 

3、在onResume方法中创建一个方向传感器,并向系统注册监听器

@Override 
protected void onResume() { 
    /** 
    * 获取方向传感器 
    * 通过SensorManager对象获取相应的Sensor类型的对象 
    */ 
    Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 
    //应用在前台时候注册监听器 
    manager.registerListener(listener, sensor, 
    SensorManager.SENSOR_DELAY_GAME); 
    super.onResume(); 

4、最后我们在onPause()中注销所有传感器的监听,释放方向感应器资源!

@Override 
protected void onPause() { 
    //应用不在前台时候销毁掉监听器 
    manager.unregisterListener(listener); 
    super.onPause(); 

 
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 方向加速度 
    */ 
    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; 

    /** 
*/ 
    } 
    @Override 
    public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    } 
} 
} 


猜你喜欢

转载自blog.csdn.net/dengcanjun6/article/details/46371633