android开发分享检查Android手机的方向

如何检查Android手机是否处于横向或纵向状态?

    当前配置用于确定要检索哪些资源等,可从“资源”配置对象中获取,如下所示:

    getResources().getConfiguration().orientation 

    如果你在一些设备上使用getResources()。getConfiguration()方向,你会错误的。 我们最初在; 感谢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 

    或根本不改变方向:

     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 

    另一方面,width()和height()总是正确的(窗口管理器使用它,所以最好是)。 我会说最好的想法是总是做宽度/高度检查。 如果你想一下,这正是你想要的 – 要知道宽度是否小于高度(人像),相反(风景)或如果他们是相同的(平方)。

    接下来是这个简单的代码:

     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; } 

    一个完整的方式来指定手机的当前方向:

      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

    解决这个问题的另一种方法是不依靠正确的显示返回值,而是依靠Android资源来解决。

    使用以下内容在文件夹res/values-landres/values-port创建文件layouts.xml

    RES /价值观土地/ layouts.xml:

     <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="is_landscape">true</bool> </resources> 

    RES /值端口/ layouts.xml:

     <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="is_landscape">false</bool> </resources> 

    在您的源代码中,您现在可以按如下方式访问当前的方向:

     context.getResources().getBoolean(R.bool.is_landscape) 

    这里是代码片段演示如何获得屏幕方向由hackbod和Martijn推荐:

    change改变时触发方向:

     @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); int nCurrentOrientation = _getScreenOrientation(); _doSomeThingWhenChangeOrientation(nCurrentOrientation); } 

    as获取当前的方向作为hackbod建议:

     private int _getScreenOrientation(){ return getResources().getConfiguration().orientation; } 

    ❸有获得当前屏幕方向的替代解决方案❷遵循Martijn解决方案:

     private int _getScreenOrientation(){ Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); return display.getOrientation(); } 

    注意 :我曾尝试过两种实现方法,但在RealDevice(NexusOne SDK 2.3)方向上返回错误的方向。

    ★所以我推荐使用解决方案 ❷获得更多优势的屏幕方向:清晰,简单,像魅力一样工作。

    ★仔细检查方向的返回,以确保正确无误(可能有限制取决于物理设备的规格)

    希望它有帮助,

     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; } 

    使用getResources().getConfiguration().orientation是正确的。

    你只需要注意不同类型的风景,设备通常使用的景观和其他景观。

    仍然不明白如何管理。

    自从这些答案中的大部分被发布以来,一些时间已经过去了,现在有些人已经使用了不赞成的方法和常量。

    我已经更新了Jarek的代码 ,不再使用这些方法和常量:

     protected int getScreenOrientation() { Display getOrient = getWindowManager().getDefaultDisplay(); Point size = new Point(); getOrient.getSize(size); int orientation; if (size.x < size.y) { orientation = Configuration.ORIENTATION_PORTRAIT; } else { orientation = Configuration.ORIENTATION_LANDSCAPE; } return orientation; } 

    请注意, Configuration.ORIENTATION_SQUARE模式不再受支持。

    我发现这在我测试过的所有设备上都是可靠的,而不是建议使用getResources().getConfiguration().orientation

    在运行时检查屏幕方向。

     @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Checks the orientation of the screen if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); } } 

    还有一种方法:

     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; } } 

    Android SDK可以告诉你这很好:

     getResources().getConfiguration().orientation 

    我认为这个代码可能会在方向更改生效后生效

     Display getOrient = getWindowManager().getDefaultDisplay(); int orientation = getOrient.getOrientation(); 

    重写Activity.onConfigurationChanged(Configuration newConfig)函数,如果想在调用setContentView之前得到有关新方向的通知,请使用newConfig,orientation。

    我认为使用getRotationv()没有帮助,因为 getRotation()返回从其“自然”方向。

    所以除非你知道“自然”的方向,否则旋转是没有意义的。

    我发现一个更简单的方法,

      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 

    请告诉我这个人是否有问题?

    简单而简单:)

    老职位我知道。 无论方向是什么或交换等,我设计了这个功能,用于设置在正确的方向设备,而不需要知道如何在设备上组织纵向和横向功能。

      private void initActivityScreenOrientPortrait() { // Avoid screen rotations (use the manifests android:screenOrientation setting) // Set this to nosensor or potrait // Set window fullscreen this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); DisplayMetrics metrics = new DisplayMetrics(); this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); // Test if it is VISUAL in portrait mode by simply checking it's size boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); if( !bIsVisualPortrait ) { // Swap the orientation to match the VISUAL portrait mode if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT ) { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); } } else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); } } 

    奇迹般有效!

    用这种方式,

      int orientation = getResources().getConfiguration().orientation; String Orintaion = ""; switch (orientation) { case Configuration.ORIENTATION_UNDEFINED: Orintaion = "Undefined"; break; case Configuration.ORIENTATION_LANDSCAPE: Orintaion = "Landscrape"; break; case Configuration.ORIENTATION_PORTRAIT: Orintaion = "Portrait"; break; default: Orintaion = "Square";break; } 

    在字符串你有Oriantion

    有很多方法可以做到这一点,这段代码适合我

      if (this.getWindow().getWindowManager().getDefaultDisplay() .getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { // portrait mode } else if (this.getWindow().getWindowManager().getDefaultDisplay() .getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { // landscape } 

      以上就是android开发分享检查Android手机的方向相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/524142.html

      (0)
      上一篇 2020年12月13日
      下一篇 2020年12月13日

      精彩推荐