Android Device Orientation
最近在处理相机拍照的方向问题,在Android Device的Orientation问题上有了些疑问,就顺便写个Demo了解下Android Device Orientation究竟是怎么个判断。
Android Device Orientation的使用场景其实最常见的就是视频播放软件了,它会随着你摆弄手机的方向,来调整一个最适合的画面旋转让用户观看。官方API文档里对Android Device Orientation有这么一句话:
public abstract void onOrientationChanged (int orientation)
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的更多相关文章
- LED notification in Android device
Code can control the LED notification in Android device, using android.app.Notification: 1 2 3 4 5 6 ...
- android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效的解决方法
Android横竖屏幕切换时注意4.0以上配置configChanges要加上screenSize,要不还会调用onCreate(). <activity android:name=" ...
- android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效
android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效 在之前的版本中都是在Man ...
- 解决Android Device Chooser 找不到设备问题
第一种情况: 已经启动了官方的模拟器也进入了Android手机界面,可是在Android Device Chooser 看不到设备,怎么办? 例如以下图所看到的,使用Reset adb 或者在adb所 ...
- Android Device Chooser中显示Target unknown解决方法
手机插在电脑上准备调试程序来着,通过eclipse运行时,弹出的Android Device Chooser中显示设备名是?????,Target未知,无法继续运行. 可以通过以下步骤解决(Ubunt ...
- 6.5、Android Studio的Android Device Monitor
Android Device Monitor是一个独立的工具,可以对Android应用进行调试和分析.Android Device Monitor无需安装整合在一个IDE中,比如像Android St ...
- Android Device Administration 设备管理器——实现一键锁屏
Android Device Administration 设备管理器--实现一键锁屏 最近研究了一下安全这一块的内容,当然,我是比较水的,所以也拿不出什么好知识点,但是有一些冷门的东西我还是可以聊聊 ...
- 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 ...
- 解决 Android Device Monitor 常见问题
Ø 简介 什么是 Android Device Monitor,中文意思就是安卓设备监视器,用于管理安装设备(手机.模拟器)用的.下面列出 Android Device Monitor 常见的一些问 ...
随机推荐
- NHibernate - ICriteria 查询
http://blog.knowsky.com/213234.htm http://blog.chinaunix.net/uid-20463341-id-1673509.html http://www ...
- 开源的读取Excel文件组件-ExcelDataReader
ExcelDataReader可以读取 Microsoft Excel 文件 ('97-2007),支持Windows .Net Framework 2 +. Windows Mobile with ...
- python常用字符串操作
#!/usr/bin/env python name='cunzhang' print(name.capitalize())#首字母大写 print(name.count('n'))#统计字符有几个 ...
- cocos2d-x 读取.plist文件
转自:http://blog.csdn.net/hgplan/article/details/8629904 在cocos2d-x中可以用.plist格式的文件来保存数据,它是XML文件格式的一种,在 ...
- Linux在向磁盘新建文件的时候在系统层面的四步操作
----------- 1.存储文件属性.内核先找到一个空的i节点,并把文件属性信息记录其中. 2.存储数据.先计算要多少个磁盘块,在内核自由块列表找到合适数量的磁盘块,并把数据从内核的缓冲区依次 ...
- Diskpart挂载/卸载VHD
#Diskpart挂载VHD $DriveLetter = "X"$VHD_File = "e:\vhd\test\win2008r2.vhdx"$Curren ...
- java foreach编辑讲解
foreach语句使用总结 foreach语句是java5的新特征之一,在遍历数组.集合方面,foreach为开发人员提供了极大的方便. foreach语句是for语句的特殊简化版本,但是foreac ...
- Codeforces Beta Round #85 (Div. 1 Only) A. Petya and Inequiations 贪心
A. Petya and Inequiations Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- URAL 1780 G - Gray Code 找规律
G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- 谷歌插件开发(实现CSDN快速登陆)
谷歌浏览器插件带来了很大的方便,于是就想着是不是也可以开发一个来用用.几经折腾下,开发了个CSDN快速 登陆的插件.下面简述一下开发的步骤. 1.开发工具:谷歌浏览器(我开发时用的是chrome 30 ...