Android move from Landscape to Portrait (or vice v

2019-08-04 04:16发布

问题:

I have a DrawerLayout which contains a FrameLayout and ListView in my app, I have to show the camera in the FrameLayout, I've done that fine (as following code) and the camera works correctly. The problem is when moving from portrait orientation to (right-Landscape orientation or left-landscape orientation), or vice versa, it take the mobile a long time to make changes, the problem does not appear when moving from right-Landscape orientation or left-landscape orientation or vice versa.

How could I make this operation as fast as I can?

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback{
    //This ShowCamera class is a helpful class
    private SurfaceHolder holdMe;
    private Camera theCamera;

    public ShowCamera(Context context,Camera camera) {
        super(context);
        theCamera = camera;
        holdMe = getHolder();
        holdMe.addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try  {
                theCamera.setPreviewDisplay(holder);
                theCamera.startPreview();
                synchronized (holder) {
                }
            } catch (Exception  e) {}
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        holdMe.removeCallback(this);
        theCamera.release();
    }

}
Now the original class is:

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    String[] options =  {"op1", "op2", "op3", "op4", "op5"}; //for the DrawerLayout
    int[] icons = { 0,R.drawable.hospital,R.drawable.education,R.drawable.police,R.drawable.food}; //for the DrawerLayout
    private Camera cameraObject;
    private ShowCamera showCamera;

    public static Camera getCamIfAvailable(){
        Camera cam = null;
        try { cam = Camera.open();}
        catch (Exception e){}
        return cam;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraObject = getCamIfAvailable();

        new Thread(new Runnable()
        {
            int rotation = getWindowManager().getDefaultDisplay().getRotation();

            @Override
            public void run()
            {
                switch(rotation){
                    case 0: // portrait
                        cameraObject.setDisplayOrientation(90);
                        break;
                    case 1: // left Landscape
                        cameraObject.setDisplayOrientation(0);
                        break;
                    case 3: //right Landscape
                        cameraObject.setDisplayOrientation(180);
                        break;
                }
            }            
        }).start();
        showCamera = new ShowCamera(this, cameraObject);
        FrameLayout preview = (FrameLayout) findViewById(R.id.content_frame);
        preview.addView(showCamera);

        //.
        //.
        //.
        //.
        //. Here, code for Drawerlayout no problrm 
        //.
        //.
        //.
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.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();
        }
    }
.
.
.
.
} // End of the class MainActivity

Also I have put in the manifest file the following as first answer mentioned here:

<activity android:name=".MyActivity"
      android:configChanges="orientation|screenSize"
      android:label="@string/app_name">

Any help will be appreciated.

回答1:

Camera.open() is an heavy operation for the system. It is only triggered when a configuration change is happening, such as when you rotate your device from landscape to portrait and vice-versa. When flipping the device (from portrait to reversed portrait, or from landscape to reversed landscape) this doesn't trigger a configuration change, only the screen rendering is flipped and therefore you don't call Camera.open() again.

So, you won't be able to make it faster (even though I recommend you to take a look at the systrace tool to see what's really happening when you rotate your screen).

But, I strongly encourage you to try calling the Camera.open() from a background Thread to avoid freezing the UI.