2013-06-04 72 views
1

我一直在研究一个由Redlaser的扫描程序库构建的条码扫描器应用程序,并且我试图在扫描仪叠加层上实现一个按钮来切换摄像头的方向。我一直在这个问题上花了很多时间,但我仍然找不到任何解决方案。以下是我尝试过的解决方案列表,但尚未设法开始工作。也许有人可以改进他们或想出不同的东西。摄像头方向变化

1.旋转UI - >保持原来的方向,只需旋转按钮。 无法接受:轮播不适用于Android 2.3,且该应用需要与2.3兼容。

2.使用android:configChanges="keyboardHidden|orientation|screenSize"setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);更改方向。 不起作用:此旋转视图,但图像失真,当我将手机向上移动时,相机向左移动。

3.使用camera.setDisplayOrientation(90);与解决方案2. 不可能:camera对象所在的RedLaser库代码中,我没有访问它。这就是说,在我的ScannerActivity.java中,我没有任何Camera对象。所以我不知道如何参考正在使用的那个。

4.使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);不确定:似乎工作,但这会重新开始活动,我需要保留数据。我熟悉this article,但我还没有找到使用它的好方法。我希望保存来自活动的所有数据,其中包括一些ArrayList<BarcodeResult>

任何想法?

+0

本文介绍如何保留重要数据。你到底有什么麻烦? – Fildor

+0

那么,如何在会话之间保存'ArrayList '项?或者任何其他类型的非原始的。 – Sebek

+0

那么,你可以将它序列化为一个文件或写入一个数据库。你认为什么是“一种好方法”?我想你必须将该文章与[数据持久性](http://developer.android.com/guide/topics/data/data-storage.html)结合起来。 – Fildor

回答

1

好吧,我明白了!我点了这个伟大的想法:

5.In布局XML复制粘贴整个UI,并使用原来的肖像和景观新一:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
    android:id="@+id/preview_frame_overlay" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" 
    android:orientation="vertical" 
    android:visibility="visible" > 

    <!-- here is the rest of the portait layout code --> 

</LinearLayout> 

<LinearLayout 
    android:id="@+id/preview_frame_overlay2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" 
    android:orientation="vertical" 
    android:rotation="90" 
    android:visibility="gone" > 

<!-- here is the rest of the landscape layout code --> 

</LinearLayout> 

当然,你应该使用android:configChanges="keyboardHidden|orientation|screenSize"

而在你的扫描活动,你应该改变什么按钮是可见的和可用取决于方向:

//in onCreate 
if (!ProductionActivity.settingLandscape) // if landscape set to portait 
       { 

        portraitView.setVisibility(View.VISIBLE); 
        landscapeView.setVisibility(View.GONE); 
        findViewById(R.id.view_finder).setVisibility(View.VISIBLE); 
        findViewById(R.id.view_finder2).setVisibility(View.GONE); 
        ChangeToLandscape(false); 

       } else 
       { 

        portraitView.setVisibility(View.GONE); 
        landscapeView.setVisibility(View.VISIBLE); 
        findViewById(R.id.view_finder).setVisibility(View.GONE); 
        findViewById(R.id.view_finder2).setVisibility(View.VISIBLE); 
        ChangeToLandscape(true); 

        android.view.ViewGroup.LayoutParams params1 = landscapeView.getLayoutParams(); //used to resize screen 
        params1.height = mDisplay.getWidth(); 
        landscapeView.setLayoutParams(params1); 

       } 

ChangeToLandscape方法,我们更新按钮引用和代表:

private void ChangeToLandscape(boolean landscape) 
     { 
      if (landscape) 
       { 
        hintTextView = (TextView) findViewById(R.id.hint_text2); 
        foundTextView = (TextView) findViewById(R.id.num_found_text2); 
        tvLastBarcode = (TextView) findViewById(R.id.tv_lastBarcode2); 
        doneButton = (Button) findViewById(R.id.button_done2); 
        bMultiscan = (Button) findViewById(R.id.bMultiscan2); 
        toggleTorchButton = (Button) findViewById(R.id.button_toggle_torch2); 
        toggleLineButton = (Button) findViewById(R.id.button_toggle_line2); 
        bRotate = (Button) findViewById(R.id.bRotate2); 
        viewfinderView = findViewById(R.id.view_finder2); 

       } else 
       { 
        hintTextView = (TextView) findViewById(R.id.hint_text); 
        foundTextView = (TextView) findViewById(R.id.num_found_text); 
        tvLastBarcode = (TextView) findViewById(R.id.tv_lastBarcode); 
        doneButton = (Button) findViewById(R.id.button_done); 
        bMultiscan = (Button) findViewById(R.id.bMultiscan); 
        toggleTorchButton = (Button) findViewById(R.id.button_toggle_torch); 
        toggleLineButton = (Button) findViewById(R.id.button_toggle_line); 
        bRotate = (Button) findViewById(R.id.bRotate); 
        viewfinderView = findViewById(R.id.view_finder); 
       } 
    //and then we re-assign the buttons delegates 
    doneButton.setOnClickListener(new OnClickListener() 
       { 
        public void onClick(View v) 
         { 
          doneScanning(); 
         } 
       }); //....etc 
    } 

使用这种想法的活动并不会重新启动,所以我的数据会保留下来,它的伟大工程。