在布局Android中一个SurfaceView [英] Layout over a SurfaceView in Android

查看:132
本文介绍了在布局Android中一个SurfaceView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 ViewPager 工作,我有两个片段。第一个片段显示出相机preVIEW。我想包括此片段在另一个上面的布局,但我只可以添加此布局的活性(它显示在上面的第一个片段两个片段,并不仅仅是布局)。

I'm working with a ViewPager and I have two fragments. The first fragment is showing a CameraPreview. I want to include another layout above this Fragment but I only can add this layout to the activity (It shows the layout above two fragments and not only in the first fragment).

这是我有什么。但按钮和TextView中也示出了在父活动的另一片段。

This is what I have. But the Button and the TextView are also showing in the another fragment of the parent Activity.

我使用这个code以添加叠加布局(R.layout.control)

I'm using this code to add the overlay layout (R.layout.control)

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        autoFocusHandler = new Handler();
        mCamera = getCameraInstance();

        /* Instance barcode scanner */
        scanner = new ImageScanner();
        scanner.setConfig(0, Config.X_DENSITY, 3);
        scanner.setConfig(0, Config.Y_DENSITY, 3);

        mPreview = new CameraPreview(getActivity(), mCamera, previewCb, autoFocusCB);

        dialogProduct = new Dialog(getActivity(), R.style.DialogSlideAnim);
        dialogProduct.setContentView(R.layout.dialog_register);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_scan, container, false);

        FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.cameraPreviewFrame);
        preview.addView(mPreview);

        scanText = (TextView) rootView.findViewById(R.id.scanText);

        scanButton = (Button) rootView.findViewById(R.id.ScanButton);

        scanButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (barcodeScanned) {
                    barcodeScanned = false;
                    scanText.setText("Scanning...");
                    mCamera.setPreviewCallback(previewCb);
                    mCamera.startPreview();
                    previewing = true;
                    mCamera.autoFocus(autoFocusCB);
                }
            }
        });

        //Add overlay layout
        View headerView = View.inflate(getActivity(), R.layout.control, null);
        LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        getActivity().addContentView(headerView,layoutParamsControl);

        return rootView;

    }

这是片段的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/totalText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/total_text"
        android:textSize="30sp" >
    </TextView>

    <FrameLayout
        android:id="@+id/cameraPreviewFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <TextView
        android:id="@+id/scanText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/escaneando" >
    </TextView>

    <Button
        android:id="@+id/ScanButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/btn_scan" />

</LinearLayout>

问题


  1. 我一直在使用该方法尝试 addView()后添加米preVIEW (相机preVIEW)但是我得到一个错误,因为我不删除previous视图。 ¿是否有在片段类如 addContentView A方法(不要删除previous视图)?

  1. I have tried using the method addView() after add the mPreview (CameraPreview) but I got an error because I don't remove the previous view. ¿Is there a method like addContentView (which don't remove the previous view) in the Fragment Class?

¿你有什么其他的建议,以解决这个问题?

¿Do you have any other suggestion to solve this problem?

我真的AP preciate你的帮助

I'd really appreciate your help

感谢您

推荐答案

我解决我的问题与这个修改例如的。我修改我的相机preVIEW 观点与构造,让我到里面添加一个实例的FrameLayout 相机preVIEW(上下文,AttributeSet中))。然后我tag.Now我没有使用 addContentView 方法,因为我可以用findViewById从父布局得到这个观点添加另一叠加观看相机preVIEW后。我希望这个帮助别人。

I solved my problem with a modification of this example . I modify my CameraPreview view with a constructor that allow me to add an instance inside the FrameLayout (CameraPreview(Context,AttributeSet)). Then I just add another overlay view after the CameraPreview tag.Now I'm not using the addContentView method because I can get this view using findViewById from the parent layout. I hope this help somebody.

我)片段布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/cameraPreviewFrame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <com.easyshopping.activities.CameraPreview
            android:id="@+id/cameraSurfaceView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </com.easyshopping.activities.CameraPreview>

        <Button
            android:id="@+id/btnTestSurface"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingTop="10dp"
            android:text="@string/test_button" />
    </FrameLayout>

    <TextView
        android:id="@+id/scanText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/escaneando" >
    </TextView>

    <Button
        android:id="@+id/ScanButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/btn_scan" />

</LinearLayout>

ⅱ)OnCreateView()中的片段

ii) OnCreateView() in the Fragment

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_scan, container, false);

        mPreview = (CameraPreview) rootView.findViewById(R.id.cameraSurfaceView);
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mPreview.getHolder();
        mHolder.addCallback(this);

        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        // FrameLayout preview = (FrameLayout)
        // rootView.findViewById(R.id.cameraPreviewFrame);
        // preview.addView(mPreview);

        scanText = (TextView) rootView.findViewById(R.id.scanText);

        scanButton = (Button) rootView.findViewById(R.id.ScanButton);

        scanButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (barcodeScanned) {
                    barcodeScanned = false;
                    scanText.setText("Scanning...");
                    mCamera.setPreviewCallback(previewCb);
                    mCamera.startPreview();
                    previewing = true;
                    mCamera.autoFocus(autoFocusCB);
                }
            }
        });

        return rootView;
}

这篇关于在布局Android中一个SurfaceView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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