Java 在 Android 手机上检查方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2795833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check orientation on Android phone
提问by Mohit Deshpande
How can I check if the Android phone is in Landscape or Portrait?
如何查看安卓手机是横屏还是竖屏?
采纳答案by hackbod
The current configuration, as used to determine which resources to retrieve, is available from the Resources' Configuration
object:
用于确定要检索哪些资源的当前配置可从 ResourcesConfiguration
对象获得:
getResources().getConfiguration().orientation;
You can check for orientation by looking at its value:
您可以通过查看其值来检查方向:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}
More information can be found in the Android Developer.
更多信息可以在Android Developer 中找到。
回答by neteinstein
Use getResources().getConfiguration().orientation
it's the right way.
使用getResources().getConfiguration().orientation
它是正确的方法。
You just have to watch out for different types of landscapes, the landscape that the device normally uses and the other.
您只需要注意不同类型的景观,设备通常使用的景观和其他景观。
Still don't understand how to manage that.
仍然不明白如何管理它。
回答by NguyenDat
Here is code snippet demo how to get screen orientation was recommend by hackbodand Martijn:
这是hackbod和Martijn推荐的如何获得屏幕方向的代码片段演示:
? Trigger when change Orientation:
? 改变方向时触发:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int nCurrentOrientation = _getScreenOrientation();
_doSomeThingWhenChangeOrientation(nCurrentOrientation);
}
? Get current orientation as hackbodrecommend:
? 按照hackbod 的建议获取当前方向:
private int _getScreenOrientation(){
return getResources().getConfiguration().orientation;
}
?There are alternative solution for get current screen orientation ? follow Martijnsolution:
?有获取当前屏幕方向的替代解决方案吗?遵循Martijn解决方案:
private int _getScreenOrientation(){
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
return display.getOrientation();
}
★Note: I was try both implement ? & ?, but on RealDevice (NexusOne SDK 2.3) Orientation it returns the wrong orientation.
★注:我是尝试两个工具?& ?,但在 RealDevice (NexusOne SDK 2.3) Orientation 上,它返回错误的方向。
★So i recommend to used solution? to get Screen orientation which have more advantage: clearly, simple and work like a charm.
★所以我推荐使用的解决方案?获得具有更多优势的屏幕方向:清晰,简单,工作就像一个魅力。
★Check carefully return of orientation to ensure correct as our expected (May be have limited depend on physical devices specification)
★仔细检查方向的返回以确保按我们的预期正确(可能会受到物理设备规格的限制)
Hope it help,
希望有帮助,
回答by Jarek Potiuk
If you use getResources().getConfiguration().orientation on some devices you will get it wrong. We used that approach initially in http://apphance.com. Thanks to remote logging of Apphance we could see it on different devices and we saw that fragmentation plays its role here. I saw weird cases: for example alternating portrait and square(?!) on HTC Desire HD:
如果你在某些设备上使用 getResources().getConfiguration().orientation 你会得到它的错误。我们最初在http://apphance.com 中使用了这种方法。感谢 Apphance 的远程日志记录,我们可以在不同的设备上看到它,我们看到碎片在这里发挥了作用。我看到了一些奇怪的情况:例如在 HTC Desire HD 上交替出现纵向和方形(?!):
CONDITION[17:37:10.345] screen: rotation: 270 orientation: square
CONDITION[17:37:12.774] screen: rotation: 0 orientation: portrait
CONDITION[17:37:15.898] screen: rotation: 90
CONDITION[17:37:21.451] screen: rotation: 0
CONDITION[17:38:42.120] screen: rotation: 270 orientation: square
or not changing orientation at all:
或根本不改变方向:
CONDITION[11:34:41.134] screen: rotation: 0
CONDITION[11:35:04.533] screen: rotation: 90
CONDITION[11:35:06.312] screen: rotation: 0
CONDITION[11:35:07.938] screen: rotation: 90
CONDITION[11:35:09.336] screen: rotation: 0
On the other hand, width() and height() is always correct (it is used by window manager, so it should better be). I'd say the best idea is to do the width/height checking ALWAYS. If you think about a moment, this is exactly what you want - to know if width is smaller than height (portrait), the opposite (landscape) or if they are the same (square).
另一方面, width() 和 height() 总是正确的(它被窗口管理器使用,所以最好是)。我想说最好的主意是始终检查宽度/高度。如果您考虑一下,这正是您想要的 - 知道宽度是否小于高度(纵向),相反(横向)或它们是否相同(正方形)。
Then it comes down to this simple code:
然后归结为这个简单的代码:
public int getScreenOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
} else{
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}
回答by anshul
int ot = getResources().getConfiguration().orientation;
switch(ot)
{
case Configuration.ORIENTATION_LANDSCAPE:
Log.d("my orient" ,"ORIENTATION_LANDSCAPE");
break;
case Configuration.ORIENTATION_PORTRAIT:
Log.d("my orient" ,"ORIENTATION_PORTRAIT");
break;
case Configuration.ORIENTATION_SQUARE:
Log.d("my orient" ,"ORIENTATION_SQUARE");
break;
case Configuration.ORIENTATION_UNDEFINED:
Log.d("my orient" ,"ORIENTATION_UNDEFINED");
break;
default:
Log.d("my orient", "default val");
break;
}
回答by maximus
There is one more way of doing it:
还有一种方法可以做到:
public int getOrientation()
{
if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().heightPixels)
{
Toast t = Toast.makeText(this,"LANDSCAPE",Toast.LENGTH_SHORT);
t.show();
return 1;
}
else
{
Toast t = Toast.makeText(this,"PORTRAIT",Toast.LENGTH_SHORT);
t.show();
return 2;
}
}
回答by Daniel
I think this code may work after orientation change has take effect
我认为此代码可能会在方向更改生效后起作用
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
override Activity.onConfigurationChanged(Configuration newConfig) function and use newConfig,orientation if you want to get notified about the new orientation before calling setContentView.
如果您想在调用 setContentView 之前获得有关新方向的通知,请覆盖 Activity.onConfigurationChanged(Configuration newConfig) 函数并使用 newConfig,orientation。
回答by Nguyen Minh Binh
A fully way to specify the current orientation of the phone:
指定手机当前方向的完整方法:
public String getRotation(Context context){
final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
switch (rotation) {
case Surface.ROTATION_0:
return "portrait";
case Surface.ROTATION_90:
return "landscape";
case Surface.ROTATION_180:
return "reverse portrait";
default:
return "reverse landscape";
}
}
Chear Binh Nguyen
Chear Binh Nguyen
回答by Single 'n Looking
The Android SDK can tell you this just fine:
Android SDK 可以很好地告诉您这一点:
getResources().getConfiguration().orientation
回答by steveh
i think using getRotationv() doesn't help because http://developer.android.com/reference/android/view/Display.html#getRotation%28%29getRotation() Returns the rotation of the screen from its "natural" orientation.
我认为使用 getRotationv() 没有帮助,因为 http://developer.android.com/reference/android/view/Display.html#getRotation%28%29getRotation() 从“自然”返回屏幕的旋转方向。
so unless you know the "natural" orientation, rotation is meaningless.
所以除非你知道“自然”方向,否则旋转是没有意义的。
i found an easier way,
我找到了一个更简单的方法,
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
if(width>height)
// its landscape
please tell me if there is a problem with this someone?
请告诉我这个人是否有问题?