1. 程式人生 > >AndroidPad設定只支援三個方向旋轉(正方向豎屏0度方向,90度和270度橫屏方向)

AndroidPad設定只支援三個方向旋轉(正方向豎屏0度方向,90度和270度橫屏方向)

開發過程中遇到此效果,在此記錄,手機預設狀態下只只支援這三個方向的旋轉,而平板支援4個方向可以旋轉。現在實現平板和手機一樣只支援三個方向。程式碼如下:

1.自定義監聽extends OrientationEventListener

package com.yhy.myapplication22;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.OrientationEventListener;

/**
 * Created by ${yinhaiyang} on 2018/4/3.
 * android的螢幕旋轉只能一次旋轉90度,如果你突然一下子旋轉180度,
 * onConfigurationChanged函式不會被呼叫。
 * 所以此時可以通過呼叫這個監聽方法實現
 */

public class MyOrientationDetector extends OrientationEventListener {
    MyOrientationDetector detector;
    Activity mContext;
    int rate;




    public MyOrientationDetector(Context context) {
        super(context);
        this.mContext = (Activity) context;
    }


    public MyOrientationDetector(Context context, int rate) {
        super(context, rate);
        this.mContext = (Activity) context;
        this.rate = rate;


    }

    @Override

    public void onOrientationChanged(int orientation) {

        Log.i("MyOrientationDetector ", "onOrientationChanged:" + orientation);


        final int rotation = mContext.getWindowManager().getDefaultDisplay().getOrientation();


        if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {

            return;  //手機平放時,檢測不到有效的角度

        }

        //只檢測是否有四個角度的改變

        if (orientation > 350 || orientation < 10) { //0度

            orientation = 0;

        } else if (orientation > 80 && orientation < 100) { //90度

            orientation = 90;

        } else if (orientation > 170 && orientation < 190) { //180度

            orientation = 180;

        } else if (orientation > 260 && orientation < 280) { //270度

            orientation = 270;

        } else {

            return;

        }


        if (orientation == 0)

        {
            mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//豎屏


        } else if (orientation == 90)

        {
            mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);//橫屏


        } else if (orientation == 180)

        {
            mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//豎屏


        } else if (orientation == 270)

        {

            mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);//橫屏

        }


    }
}

2.在Activity中呼叫

        myOrientationDetector=new MyOrientationDetector(this,SensorManager.SENSOR_DELAY_NORMAL);//在oncreate中呼叫。

    @Override
    protected void onResume() {
        super.onResume();
        //開啟螢幕旋轉方向監聽
        myOrientationDetector.enable();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //關閉監聽
        myOrientationDetector.disable();
    }

3.Manifest中配置如下程式碼:保證螢幕旋轉的時候不銷燬Activity

原因:預設狀態下,Activity每次橫豎屏切換(包括用setRequestedOrientation呼叫)都會重新呼叫一輪onPause-> onStop-> onDestory-> onCreate->onStart->onResume操作,從而銷燬原來的Activity物件,建立新的Activity物件,這是因為通常情況下軟體在橫豎屏之間切換,介面的高寬會發生轉換,從而可能會要求有不同的佈局。因此我們加入如下設定,可以有效避免橫豎屏切換的時候,activity的銷燬重建。

android:configChanges="keyboardHidden|orientation|screensize"

<activity android:name=".MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

4.manifest中不設定configchages屬性,讓acticity 重建,通過onSaveInstanceState()來儲存,activity銷燬前的狀態和資料。

說明:當螢幕旋轉時,這個Configuration就發生了改變,因此當前顯示的Activity需要被重建,Activity物件會被終止,它的onPause()、onStop()和onDestroy()方法依次觸發,然後一個新的Activity物件被建立,onCreate()方法被觸發。假設螢幕旋轉前,使用者正在手機上填寫一個登錄檔單,如果處理不當,使用者會發現旋轉後的表單變成空白的了,嚴重影響使用體驗。

//旋轉後,恢復資料
----------
package com.example.scareenchange;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText editText1;
    private int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1=(EditText) findViewById(R.id.editText1);

        //注意一定要加if語句,不然程式異常
        if(savedInstanceState!=null) {
            //通過Bundle物件取出
            int i=savedInstanceState.getInt("info");
        }

    }

    public void button1(View v) {

        editText1.setText((i++)+"");
    }
    /*
     *這個方法會在重新建立Activity之前呼叫
     *我們在這個方法裡儲存物件,以解決Activity重新建立問題
     *
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        Toast.makeText(getApplicationContext(), i+"", 1000).show();
        outState.putInt("info", i);
    }
}

注:方法3、4要達到的目的一樣,任選其一,推薦第4種。

demo參考地址:點選開啟連結https://download.csdn.net/download/yhy123456q/10325174