图片与导航抽屉重叠

时间:2015-12-21 13:54:15

标签: android android-fragments android-camera navigation-drawer

我在前置摄像头预览(片段中)上有图片(帽子),因此您可以在购买之前试戴帽子。但问题是,在此预览中,我正在尝试打开导航抽屉,我的帽子与此抽屉重叠,前置摄像头位于导航抽屉下方。如何将图片(帽子)放在导航抽屉下方?

MirrorFragment.java:

public class MirrorFragment extends Fragment implements SurfaceHolder.Callback {

    private Camera camera = null;

    private SurfaceHolder cameraSurfaceHolder = null;
    private boolean previewing = false;
    private static int wid = 0, hgt = 0;
    private View cameraViewControl;
    private Button btnCapture;
    private Activity activity;

    public MirrorFragment() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity = (Activity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        SurfaceView cameraSurfaceView;
        Display display;


        View view = inflater.inflate(R.layout.fragment_mirror, container, false);
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        int toolbarHeight = ((MainActivity) activity).getToolbar().getHeight();


        display = getActivity().getWindowManager().getDefaultDisplay();

        wid = display.getWidth();
        hgt = display.getHeight();


        activity.getWindow().setFormat(PixelFormat.TRANSLUCENT);
        activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        cameraSurfaceView = (SurfaceView) view.findViewById(R.id.cameraSurfaceViewFragment);
        cameraSurfaceHolder = cameraSurfaceView.getHolder();

        cameraSurfaceHolder.addCallback(this);

        cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);



        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

        RelativeLayout.LayoutParams layoutParamsControl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);

        cameraViewControl = layoutInflater.inflate(R.layout.cambutton, null);

        btnCapture = (Button) cameraViewControl.findViewById(R.id.btnCapture);

        setMarginTopForHat(toolbarHeight);


        activity.addContentView(cameraViewControl, layoutParamsControl);

        return view;
    }




    public void setMarginTopForHat(int height) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnCapture.getLayoutParams();
        layoutParams.setMargins(0, height + 30, 0, 0);
        btnCapture.setLayoutParams(layoutParams);
    }    


    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        try {
            camera = Camera.open(1);
            camera.setDisplayOrientation(90);
        } catch (RuntimeException e) {
            Toast.makeText(getContext(), "Device camera is not working properly, please try after sometime.",
                    Toast.LENGTH_LONG).show();
        }
    }



    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        ((ViewGroup) cameraViewControl.getParent()).removeView(cameraViewControl);
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }
}

fragment_mirror.xml:

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

    <SurfaceView
        android:id="@+id/cameraSurfaceViewFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您的帽子View出现在抽屉上,因为您正在使用活动的addContentView()方法,即将其添加为活动中的最后一个孩子内容ViewGroup,使其在其余内容之上绘制。如果您ViewGroup加载的MirrorFragmentFrameLayout,则可以将其添加到fragment_mirror。但是,可能更好的解决方案是将其直接添加到NSTimer布局,无论是以编程方式还是在布局XML中。