使用相机时,Android从横向移动到纵向(反之亦然)?

时间:2014-08-06 12:30:26

标签: android multithreading android-camera landscape-portrait

我的应用中有一个DrawerLayout,其中包含FrameLayoutListView,我必须在FrameLayout中显示相机,我已经完成了罚款(如下面的代码)和相机正常工作。问题是当从纵向移动到(右横向或左横向)时,反之亦然,移动需要很长时间才能进行更改,从右横向移动或向左移动时不会出现问题 - 景观取向,反之亦然。

我怎样才能尽可能快地完成这项操作?

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

}
现在原来的班级是:

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

此外,我已将清单文件作为第一个answer mentioned here放入以下内容:

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

Camera.open()对系统来说是一项繁重的操作。它仅在发生配置更改时触发,例如将设备从横向旋转为纵向,反之亦然。 翻转设备时(从纵向到纵向,或从横向到反向横向),这不会触发配置更改,只会翻转屏幕渲染,因此您不会再次调用Camera.open()

因此,您将无法加快速度(即使我建议您查看systrace工具以查看旋转屏幕时发生的情况)。

但是,我强烈建议您尝试从后台线程调用Camera.open()以避免冻结UI。