将屏幕方向锁定到其(自然)默认方向

时间:2012-03-10 04:07:09

标签: android

我想将屏幕的方向锁定为默认的orinetation。我在实现这个问题上遇到了问题。最初我将屏幕锁定为清单中的肖像。它适用于纵向defaultdevices。但是很多平板电脑都有默认情况,所以在锁定到肖像的这些设备中不适合,我想检测这个默认方向并将其锁定。我的意思是如果横向是默认方向我想要将方向锁定到横向,如果它的肖像然后将其锁定到端口。这该怎么做。我被困在这一部分。我不想同时支持两个方向(自动)。请帮忙

感谢。

3 个答案:

答案 0 :(得分:1)

不同设备有默认方向,例如,galaxy 10平板电脑的默认方向与nexus 7平板电脑不同。当您获得显示的方向时,您将获得以下值:

enter image description here

这么说,你在锁定方法中需要做的是:

public void mLockScreenRotation(FA_Padre actividad){
    int buildVersionSDK = Build.VERSION.SDK_INT;
    Display display = ((WindowManager) actividad.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int orientation=0;
    if(buildVersionSDK >= 8){
        orientation=display.getRotation();
    }
    /****************Phone*************************************/
    if(buildVersionSDK < 8){// older Android versions with only two orientations
        switch (actividad.getResources().getConfiguration().orientation){

        case Configuration.ORIENTATION_PORTRAIT:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Configuration.ORIENTATION_LANDSCAPE:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;

        }
    }else if((buildVersionSDK > 7 ) && (!GlobalInfo.isTablet())){// Newer Android phones with more than two orientations

        switch(orientation){

        case Surface.ROTATION_0:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Surface.ROTATION_90:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;
        case Surface.ROTATION_180:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Surface.ROTATION_270:
            actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            break;

        }
    /****************TABLET*************************************/
    }else{
        int width = 0;
        int height = 0;
        switch(orientation){
            /*If the default orientation of the device is landscape Rotation_0 and rotation_180 will be the case if the device is being held in landscape. if the default orientation of the device is portrait rotation_0 or 180 will only be the case if the device is in portrait mode*/ 
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:  
                width = display.getWidth();
                height = display.getHeight();
                break;
          /*the opposite in here*/
            case Surface.ROTATION_90: //      
            case Surface.ROTATION_270:
                width = display.getHeight();
                height = display.getWidth();
                break;
            default:
                break;
        }
        if(width > height){//Default ORIENTATION = LANDSCAPE, lock the screen in the current orientation
            switch(orientation){

            case Surface.ROTATION_0:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Surface.ROTATION_90:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case Surface.ROTATION_180:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            case Surface.ROTATION_270:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            } 

        } else {//Default ORIENTATION = PORTRAIT, lock the screen in the current orientation
            switch(orientation){

            case Surface.ROTATION_0:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            case Surface.ROTATION_90:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Surface.ROTATION_180:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case Surface.ROTATION_270:
                actividad.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            }

        } 

    }
}

答案 1 :(得分:0)

尝试将此锁定屏幕设为默认值

Lockorientationactivity

public class Lockorientationactivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int getConf=this.getResources().getConfiguration().orientation;

       if(getConf==Configuration.ORIENTATION_PORTRAIT)
       {
           this.setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
           Toast.makeText(getBaseContext(), "ORIENTATION_PORTRAIT", Toast.LENGTH_SHORT).show();
       }
       else
       {
           Toast.makeText(getBaseContext(), "ORIENTATION_LANDSCAPE", Toast.LENGTH_SHORT).show();
           this.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
       }

           setContentView(R.layout.main);
    }
}

但您必须为活动设置android:configChanges="orientation"

答案 2 :(得分:0)

要通过代码锁定屏幕,您必须使用屏幕的实际旋转(0,90,180,270),您必须知道它的自然位置,在智能手机中,自然位置将是纵向和平板电脑,它将是风景。

以下是代码(锁定和解锁方法),它已经在某些设备(智能手机和平板电脑)中进行了测试,效果很好。

public static void lockScreenOrientation(Activity activity)
{   
    WindowManager windowManager =  (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);   
    Configuration configuration = activity.getResources().getConfiguration();   
    int rotation = windowManager.getDefaultDisplay().getRotation(); 

    // Search for the natural position of the device    
    if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&  
       (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) ||  
       configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&   
       (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270))   
    {   
        // Natural position is Landscape    
        switch (rotation)   
        {   
            case Surface.ROTATION_0:    
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);    
                break;      
            case Surface.ROTATION_90:   
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
            break;      
            case Surface.ROTATION_180: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                break;          
            case Surface.ROTATION_270: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
                break;
        }
    }
    else
    {
        // Natural position is Portrait
        switch (rotation) 
        {
            case Surface.ROTATION_0: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
            break;   
            case Surface.ROTATION_90: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
            break;   
            case Surface.ROTATION_180: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
                break;          
            case Surface.ROTATION_270: 
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
                break;
        }
    }
}

public static void unlockScreenOrientation(Activity activity)
{
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}