1. Download our Official Android App: Forums for Android!

How could I resize the camera preview dynamically?

Discussion in 'Android Apps & Games' started by stereoMatching, Apr 29, 2012.

  1. stereoMatching

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Apr 29, 2012

    Apr 29, 2012
    2
    1
    5
    I want to resize the camera preview dynamically but always fail.

    I start my preview by setting the size as follow
    <SurfaceView
    android:id="@+id/mSurfaceView1"
    android:layout_gravity="center_vertical|center_horizontal"
    android:visibility="visible"
    android:layout_width="480px"
    android:layout_height="320px">
    </SurfaceView>

    and add a button to resize it to 320 * 240.
    Code (Text):
    1.  
    2. resizeButton.setOnClickListener(new Button.OnClickListener()
    3.         {
    4.             @Override
    5.             public void onClick(View v)
    6.             {
    7.                 // TODO Auto-generated method stub
    8.                 android.widget.FrameLayout.LayoutParams params = new android.widget.FrameLayout.LayoutParams(320, 240);
    9.                 surfaceView.setLayoutParams(params);                
    10.             }
    11.         });
    12.  
    But this can't work.What steps do I miss?
    Should I just start a new activity with different size of the surfaceView?
    Thanks a lot
     

    Advertisement

  2. stereoMatching

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Apr 29, 2012

    Apr 29, 2012
    2
    1
    5
    I solved it by myself after a lot of trials and errors

    Code (Text):
    1.  
    2.                resetCamera();  //reset the camera                        
    3.                
    4.                 ViewGroup.LayoutParams params = surfaceView.getLayoutParams();                    
    5.                 DisplayMetrics dm = new DisplayMetrics();
    6.                 getWindowManager().getDefaultDisplay().getMetrics(dm);              
    7.                 params.width =dm.widthPixels;
    8.                 params.height = dm.heightPixels;
    9.                 surfaceView.setLayoutParams(params);
    10.                                                
    11.                 initCamera(); //initiate the camera(open camera, set parameter, setPreviewDisplay, startPreview)
    12.  
     
    Yerevak likes this.

Share This Page

Loading...