如何对齐中心水平曲面FrameLayout?

时间:2014-09-29 21:48:02

标签: android camera surfaceview android-framelayout camera-view

  

我想对齐水平居中的表面相机视图。我尝试了很多   技术,但我找不到正确的解决方案。

Result Screen

  • 我不想scretch cameraview。因为cameraview必须显示正确的宽高比,如实际尺寸。
  • 我不想拍摄相机视图。我需要自定义相机活动。

这些是我的代码:

布局xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="0dp"
        android:layout_weight="1" >        
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/footer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000"
        android:gravity="center" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#abc"
            android:padding="2dp" >

            <Button
                android:id="@+id/button_capture"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Capture" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/id_reklam"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

CameraPreviev类:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;

static private CameraPreview instance;

private static final String TAG = "CameraPreview.java";
private Camera.Size size;

@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
    super(context);
    //bu satır eklenmez ise
    setWillNotDraw(false);
    mCamera = camera;

    // Install a SurfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        
}

public void surfaceCreated(SurfaceHolder holder) {      

    Log.d("Variable", "surfaceCreated");

    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        mCamera.setPreviewDisplay(holder);

        Camera.Parameters parameters = mCamera.getParameters();

        if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            parameters.set("orientation", "portrait");
            mCamera.setDisplayOrientation(90);
          }
        parameters.set("jpeg-quality", 100);       
        mCamera.setParameters(parameters);

        setVideoSize();

        mCamera.startPreview();
    } catch (IOException e) {
        Log.d("Method.surfaceCreated", "Error setting camera preview: " + e.getMessage());
    }
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    Paint paint = new Paint();

    canvas.drawLine(100, 0, 400, 0, paint);
    canvas.drawLine(100, 500, 400, 500, paint);
    canvas.drawLine(100, 0, 100, 500, paint);
    canvas.drawLine(400, 0, 400, 500, paint);

    Path path = new Path();  
    path.moveTo(18,10);  
    path.lineTo(1, 0);  
    path.lineTo(0, 20);  

    paint.setColor(Color.RED);  
    paint.setStrokeWidth(2);  
    paint.setStyle(Paint.Style.STROKE);  

    canvas.drawPath(path, paint);  
}

private void setVideoSize() {
    size = mCamera.getParameters().getPreviewSize();

    Camera.Size s = mCamera.getParameters().getPictureSize();

    // // Get the dimensions of the video
    int videoWidth = size.height;
    int videoHeight = size.width;
    float videoProportion = (float) videoWidth / (float) videoHeight;

    // Get the width of the screen
    int screenWidth = this.getWidth(); //getWindowManager().getDefaultDisplay().getWidth();
    int screenHeight = this.getHeight(); //getWindowManager().getDefaultDisplay().getHeight();
    float screenProportion = (float) screenWidth / (float) screenHeight;

    Log.d("Variable", "video -> Width:" + videoWidth + ", Heigh:" + videoHeight);
    Log.d("Variable", "screen -> Width:" + screenWidth + ", Heigh:" + screenHeight);

    // Get the SurfaceView layout parameters
    FrameLayout.LayoutParams lp = (LayoutParams) this.getLayoutParams();

    if (videoProportion > screenProportion) {
        lp.width = screenWidth;
        lp.height = (int) ((float) screenWidth / videoProportion);
    } else {
        lp.width = (int) (videoProportion * (float) screenHeight);
        lp.height = screenHeight;
    }
    // Commit the layout parameters
    this.setLayoutParams(lp);    
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.

    if (mHolder.getSurface() == null){
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
        mCamera.stopPreview();
    } catch (Exception e){
      // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
        Log.d("Method.surfaceChanged", "Error starting camera preview: " + e.getMessage());
    }
}    

}

0 个答案:

没有答案