最近在处理相机拍照的方向问题,在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. 破解NET的四大神器

     原本这篇文章可以更早一星期写出来与大家分享,由于某方面的原因耽搁到现在,心里竟有那么一点好像对不住大家的感觉.这当然与神器有关,因为我发现利用这四大神器我似乎觉得几乎所有的NET程序破解都不在话下了 ...

  2. C# 调用第三方DLL完整实例

    C# 调用第三方DLL完整实例 分类: C/C++ 以下代码为本人在实际项目中编写的调用第三方DLL接口程序的完整代码. public class ExecuteDLL : Form { ...//忽 ...

  3. java获取照片相关属性

    package test; import java.io.File; import java.util.Iterator; import com.drew.imaging.jpeg.JpegMetad ...

  4. 通过 监听器获取sessionId

    package shopping; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSess ...

  5. mysql全库备份数据库脚本

    #!/bin/sh DATE=`date +%Y%m%d` DEL_DATE=$(date -d '-30 days' "+%Y%m%d") HOST USER=admin PAS ...

  6. Python3爬虫学习

    学了几天python3,发现目前学到的与爬虫还是关系不大,所以现在准备爬虫和语言同步学习. 2016.8.9晚 先从最简单的开始,爬取指定url的所有内容: #encoding:UTF-8 impor ...

  7. windows 下实现函数打桩:拦截API方式

    windows 下实现函数打桩:拦截API方式            近期由于工作须要,開始研究函数打桩的方法. 由于不想对project做过多的改动,于是放弃了使用Google gmock的想法. ...

  8. 推荐图书-《SQL Server 2008商业智能完美解决方案》

    内容简介 <SQL Server 2008商业智能完美解决方案>介绍如何使用Microsoft SQL Server 2008开发商业智能(BI)解决方案.<SQL Server 2 ...

  9. 怎样拷贝整个目录并且忽略部分文件及目录(包括windows)

    http://www.dewen.org/q/2150 rsync -a --exclude dir1 --exclude dir2 ... source target

  10. [AngularJS] Javascript scope and AngularJS $scope

    Scope resolution of our Angular documents works exactly the same way scope resolution works in plain ...