从景观的Andr​​oid移动为纵向(反之亦然),使用相机时速度太慢? [英] Android move from Landscape to Portrait (or vice versa) when using camera is too slow?

查看:141
本文介绍了从景观的Andr​​oid移动为纵向(反之亦然),使用相机时速度太慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DrawerLayout 包含一个的FrameLayout 的ListView 在我的应用程序,我必须展示在的FrameLayout ,我已经做了细相机(如下code)和照相机工作正常。从纵向转向(右横向方向或左横向),反之亦然时,它以移动很长的时间进行更改的问题是,从右键横向方向或向左移动时的问题不会出现-landscape方向,反之亦然。

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

此外,我已经把清单文件中的以下内容作为第一个<一个href=\"http://stackoverflow.com/questions/5726657/how-to-detect-orientation-change-in-layout-in-android\">answer这里提到:结果

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

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

Camera.open()是系统中的重操作。它只是触发时的配置变化正在发生,当你旋转你的设备从横向到纵向,反之亦然,如。
当翻转装置(从纵向到逆转肖像,或景观逆转横向)这不会触发配置更改,只有屏幕渲染翻转,因此,你不叫 Camera.open( )一次。

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.

所以,你将无法使其更快(尽管我建议你看一看的systrace工具来看看什么是真正发生的事情,当你旋转屏幕)。

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).

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

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

这篇关于从景观的Andr​​oid移动为纵向(反之亦然),使用相机时速度太慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆