最近在处理相机拍照的方向问题,在Android Device的Orientation问题上有了些疑问,就顺便写个Demo了解下Android Device Orientation究竟是怎么个判断。

  Android Device Orientation的使用场景其实最常见的就是视频播放软件了,它会随着你摆弄手机的方向,来调整一个最适合的画面旋转让用户观看。官方API文档里对Android Device Orientation有这么一句话:

public abstract void onOrientationChanged (int orientation)

添加于 API 级别 3

Called when the orientation of the device has changed. orientation parameter is in degrees, ranging from 0 to 359. orientation is 0 degrees when the device is oriented in its natural position, 90 degrees when its left side is at the top, 180 degrees when it is upside down, and 270 degrees when its right side is to the top. ORIENTATION_UNKNOWN is returned when the device is close to flat and the orientation cannot be determined.

对于这句话,有一点不理解的就是natural position到底是一个什么个position,其实就是我们正常用手机的摆放方向,如下图:Orientation为0

Left Side Top, Orientation为90:

Right Side Top, Orientation为270:

Top Side Down,Orientation为180

好了,差不多就是这样了。再放上一次我测试时用的代码:

 public class MainActivity extends ActionBarActivity {

     private TextView textView = null;
private MyOrientationListener myOrientationListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textview);
myOrientationListener = new MyOrientationListener(this, SensorManager.SENSOR_DELAY_NORMAL);
if( myOrientationListener.canDetectOrientation()){
myOrientationListener.enable();
}
else{
Toast.makeText(this, "Can't Detect Orientation", Toast.LENGTH_LONG).show();
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
protected void onDestroy(){
super.onDestroy();
myOrientationListener.disable();
} @Override
protected void onResume(){
super.onResume();
if( myOrientationListener.canDetectOrientation()){
myOrientationListener.enable();
}
else{
Toast.makeText(this, "Can't Detect Orientation", Toast.LENGTH_LONG).show();
}
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} private class MyOrientationListener extends OrientationEventListener{ public MyOrientationListener(Context context, int rate) {
super(context, rate);
// TODO Auto-generated constructor stub
} @Override
public void onOrientationChanged(int orientation) {
// TODO Auto-generated method stub
textView.setText("" + orientation);
} }
}

 

Android Device Orientation的更多相关文章

  1. LED notification in Android device

    Code can control the LED notification in Android device, using android.app.Notification: 1 2 3 4 5 6 ...

  2. android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效的解决方法

    Android横竖屏幕切换时注意4.0以上配置configChanges要加上screenSize,要不还会调用onCreate(). <activity android:name=" ...

  3. android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效

    android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效    在之前的版本中都是在Man ...

  4. 解决Android Device Chooser 找不到设备问题

    第一种情况: 已经启动了官方的模拟器也进入了Android手机界面,可是在Android Device Chooser 看不到设备,怎么办? 例如以下图所看到的,使用Reset adb 或者在adb所 ...

  5. Android Device Chooser中显示Target unknown解决方法

    手机插在电脑上准备调试程序来着,通过eclipse运行时,弹出的Android Device Chooser中显示设备名是?????,Target未知,无法继续运行. 可以通过以下步骤解决(Ubunt ...

  6. 6.5、Android Studio的Android Device Monitor

    Android Device Monitor是一个独立的工具,可以对Android应用进行调试和分析.Android Device Monitor无需安装整合在一个IDE中,比如像Android St ...

  7. Android Device Administration 设备管理器——实现一键锁屏

    Android Device Administration 设备管理器--实现一键锁屏 最近研究了一下安全这一块的内容,当然,我是比较水的,所以也拿不出什么好知识点,但是有一些冷门的东西我还是可以聊聊 ...

  8. Android device debug (adb) by Charge Only mode

    Android device debug by Charge Only mode Method 1 Connect devices to computer and execute lsusb Find ...

  9. 解决 Android Device Monitor 常见问题

    Ø  简介 什么是 Android Device Monitor,中文意思就是安卓设备监视器,用于管理安装设备(手机.模拟器)用的.下面列出 Android Device Monitor 常见的一些问 ...

随机推荐

  1. 使用WinDbg获得托管方法的汇编代码

    概述:有时候,我们需要查看一个托管方法的汇编指令是怎么样的.记得在大学的时候,我们使用gcc -s和objdump来获得一个c程序代码的汇编指令.但是对于.NET程序来说,我们肯定无法轻松地获得这些内 ...

  2. VC中监测函数运行时间(一)—分钟,秒,毫秒

    //myTimer.h // [10/16/2013 Duan Yihao] #pragma once #include "StdAfx.h" ////////////////// ...

  3. 时间的函数,sleep,clock,gettickcount,QueryPerformanceCounter(转)

    介绍 我 们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都 会用到时间函数.还比如我们通过记录函数或者算 ...

  4. sqlserver2012

    sqlserver2012作死的节奏啊,微软最近怎么了?搞个sqlserver2012死活安装不上,无论如何都是.NET Framework 4报错,机器本身的版本比安装包的高就报错?微软的程序员思维 ...

  5. android 对一个合并后的联系人选择编辑,手机屏幕会缓慢变暗后再进入编辑界面的问题

    1.       手机上有一个合并过的联系人 2.       编辑合并后的联系人 3.       手机屏幕会缓慢变暗之后再进入编辑界面. 首先找到contacts源码包下的EditContactA ...

  6. nginx将http重定向到https

    1.rewrite server { listen 80; server_name test.com; rewrite ^(.*)$ https://$host$1 permanent; } 2. n ...

  7. flex-mp3

    Mp3Player.as package ddw.adobe { import flash.events.Event; import flash.events.IOErrorEvent; import ...

  8. Github上最全的APICloud开源前端框架效果盘点(转)

    1.微信网站几分钟变身“原生 App” 微信推出了微信JS-SDK,使微信公共号可以直接调用微信原生的接口,具备部分原生应用的能力.微信JS-SDK的推出,将大大提高微信公共号的 用户体验,但是如果存 ...

  9. C++第11周(春)项目2 - 职员有薪水了

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目2 - 职员有薪水了]定义一个名为CPe ...

  10. mysql 面试

    数据库的重要性是所有技术里最核心最需要掌握的(理解原理,并且被面试时能清晰的表达出来),直接决定运维人员薪水的高低! 所有题都要给出专业的解答方案,不是很水的那种泛泛的解答. 面试题001:什么是My ...