1

我一直在开发基于 Redlaser 的扫描仪库构建的条形码扫描仪应用程序,并且我正在尝试在扫描仪覆盖层上实现一个按钮,该按钮将切换相机的方向。我已经解决这个问题很长时间了,但我仍然找不到任何解决方案。以下是我尝试过但未能开始工作的解决方案列表。也许有人可以改进它们或想出一些不同的东西。

1.Rotate 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.Use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); Unsure:似乎有效,但这会重新启动活动,我需要保留数据。我对这篇文章很熟悉,但我还没有找到一个好的使用方法。我希望保存活动中的所有数据,其中包括一些ArrayList<BarcodeResult>.

有任何想法吗?

4

1 回答 1

1

好,我知道了!我的下午有这个好主意:

5.在布局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
    }

使用这个想法,活动永远不会重新启动,因此我的数据被保留并且效果很好。

于 2013-06-06T06:54:30.327 回答